-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.tf
150 lines (133 loc) · 3.64 KB
/
app.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
resource "aws_dynamodb_table" "visa_bulletin_data" {
name = "VisaBulletinData"
billing_mode = "PAY_PER_REQUEST"
hash_key = "pk"
range_key = "sk"
attribute {
name = "pk"
type = "S"
}
attribute {
name = "sk"
type = "S"
}
}
resource "aws_dynamodb_table" "processed_urls" {
name = "ProcessedURLs"
billing_mode = "PAY_PER_REQUEST"
hash_key = "url"
attribute {
name = "url"
type = "S"
}
}
# Install dependencies and create the Lambda layer package
resource "null_resource" "pip_install" {
triggers = {
shell_hash = "${sha256(file("${path.cwd}/src/requirements.txt"))}"
}
provisioner "local-exec" {
command = <<EOF
cd src
echo "Create and activate venv"
python3 -m venv package
source package/bin/activate
mkdir -p ${path.cwd}/layer/python
echo "Install dependencies to ${path.cwd}/layer/python"
pip3 install -r requirements.txt -t ${path.cwd}/layer/python
deactivate
cd ..
EOF
}
}
# Zip up the app to deploy as a layer
data "archive_file" "layer" {
type = "zip"
source_dir = "${path.cwd}/layer"
output_path = "${path.cwd}/layer.zip"
depends_on = [null_resource.pip_install]
}
# Create the Lambda layer with the dependencies
resource "aws_lambda_layer_version" "layer" {
layer_name = "dependencies-layer"
filename = data.archive_file.layer.output_path
source_code_hash = data.archive_file.layer.output_base64sha256
compatible_runtimes = ["python3.12", "python3.11"]
}
# Zip of the application code
data "archive_file" "app" {
type = "zip"
source_dir = "${path.cwd}/src"
output_path = "${path.cwd}/app.zip"
}
# Define the Lambda function
resource "aws_lambda_function" "visa_bulletin_scraper" {
function_name = "visa-bulletin-scraper"
handler = "lambda_function.lambda_handler"
runtime = "python3.12"
filename = data.archive_file.app.output_path
source_code_hash = data.archive_file.app.output_base64sha256
role = aws_iam_role.lambda_role.arn
layers = [aws_lambda_layer_version.layer.arn]
environment {
variables = {
DYNAMODB_TABLE = aws_dynamodb_table.visa_bulletin_data.name
}
}
}
# Define the IAM role for the Lambda function
resource "aws_iam_role" "lambda_role" {
name = "visa-bulletin-scraper-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
# Attach the necessary IAM policies to the role
resource "aws_iam_policy_attachment" "lambda_basic_execution" {
name = "lambda_basic_execution"
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
roles = [aws_iam_role.lambda_role.name]
}
resource "aws_iam_policy_attachment" "dynamodb_access" {
name = "dynamodb_access"
policy_arn = aws_iam_policy.dynamodb_access_policy.arn
roles = [aws_iam_role.lambda_role.name]
}
# Define the IAM policy for DynamoDB access
resource "aws_iam_policy" "dynamodb_access_policy" {
name = "visa-bulletin-scraper-dynamodb-access"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:PutItem"
],
"Resource": "${aws_dynamodb_table.processed_urls.arn}"
},
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:Query"
],
"Resource": "${aws_dynamodb_table.visa_bulletin_data.arn}"
}
]
}
EOF
}