-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
230 lines (189 loc) · 5.97 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
provider "aws" {
region = var.region
profile = var.aws_profile
}
data aws_caller_identity current {}
locals {
prefix = var.prefix
account_id = data.aws_caller_identity.current.account_id
}
resource "aws_ecr_repository" "netcdf_to_zarr" {
name = "${var.prefix}_netcdf_to_zarr"
image_scanning_configuration {
scan_on_push = false
}
tags = {
"name" = "netCDF4 to Zarr"
}
}
resource "aws_ecr_repository" "zarr_to_cog" {
name = "${var.prefix}_zarr_to_cog"
image_scanning_configuration {
scan_on_push = false
}
tags = {
"name" = "netCDF4 to Zarr"
}
}
resource "aws_iam_role_policy" "lambda_policy" {
name = "lambda_policy"
role = aws_iam_role.lambda_role.id
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer",
]
Effect = "Allow"
Resource = "arn:aws:ecr:${var.region}:${local.account_id}:repository/innovation*"
},
{
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Effect = "Allow"
Resource = "arn:aws:logs:*:*:*"
},
{
Action = [
"s3:*"
]
Effect = "Allow"
Resource = [
"arn:aws:s3:::${var.prefix}*",
"arn:aws:s3:::${var.prefix}*/*"
]
},
]
})
}
resource "aws_iam_role" "lambda_role" {
name = "processing_role"
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = "LambdaECRImageRetrievalPolicy"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
tags = {
name = "${var.prefix}-lambda-role"
}
}
resource "null_resource" "netcdf_to_zarr_ecr_image" {
triggers = {
python_file = md5(file("${path.module}/netcdf_to_zarr/handler.py"))
python_file = md5(file("${path.module}/netcdf_to_zarr/convert.py"))
docker_file = md5(file("${path.module}/Dockerfile"))
}
provisioner "local-exec" {
command = <<EOF
aws ecr get-login-password --region ${var.region} | docker login --username AWS --password-stdin ${local.account_id}.dkr.ecr.${var.region}.amazonaws.com
docker build -t ${aws_ecr_repository.netcdf_to_zarr.repository_url}:latest .
docker push ${aws_ecr_repository.netcdf_to_zarr.repository_url}:latest
EOF
}
}
resource "null_resource" "zarr_to_cog_ecr_image" {
triggers = {
python_file = md5(file("${path.module}/zarr_to_cog/main.py"))
docker_file = md5(file("${path.module}/Dockerfile-zarr-cog"))
}
provisioner "local-exec" {
command = <<EOF
aws ecr get-login-password --region ${var.region} | docker login --username AWS --password-stdin ${local.account_id}.dkr.ecr.${var.region}.amazonaws.com
docker build -f Dockerfile-zarr-cog -t ${aws_ecr_repository.zarr_to_cog.repository_url}:latest .
docker push ${aws_ecr_repository.zarr_to_cog.repository_url}:latest
EOF
}
}
data "aws_ecr_image" "lambda_image_ecr" {
depends_on = [
aws_ecr_repository.netcdf_to_zarr
]
repository_name = aws_ecr_repository.netcdf_to_zarr.name
image_tag = "latest"
}
data "aws_ecr_image" "lambda_image_ecr_cog" {
depends_on = [
aws_ecr_repository.zarr_to_cog
]
repository_name = aws_ecr_repository.zarr_to_cog.name
image_tag = "latest"
}
resource "aws_lambda_function" "netcdf_to_zarr" {
depends_on = [
null_resource.netcdf_to_zarr_ecr_image
]
function_name = "${var.prefix}-netcdf_to_zarr"
role = aws_iam_role.lambda_role.arn
timeout = 300
image_uri = "${aws_ecr_repository.netcdf_to_zarr.repository_url}@${data.aws_ecr_image.lambda_image_ecr.id}"
package_type = "Image"
}
resource "aws_lambda_function" "zarr_to_cog" {
depends_on = [
null_resource.zarr_to_cog_ecr_image
]
function_name = "${var.prefix}-zarr_to_cog"
role = aws_iam_role.lambda_role.arn
timeout = 300
memory_size = 10000
image_uri = "${aws_ecr_repository.zarr_to_cog.repository_url}@${data.aws_ecr_image.lambda_image_ecr_cog.id}"
package_type = "Image"
}
resource "aws_s3_bucket" "innovation_s3_bucket" {
bucket = "${var.prefix}-netcdfs"
}
resource "aws_lambda_permission" "allow_bucket" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.netcdf_to_zarr.arn
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.innovation_s3_bucket.arn
}
resource "aws_lambda_permission" "allow_bucket_cog" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.zarr_to_cog.arn
principal = "s3.amazonaws.com"
source_arn = aws_s3_bucket.innovation_s3_bucket.arn
}
resource "aws_s3_bucket_notification" "bucket_notification" {
bucket = aws_s3_bucket.innovation_s3_bucket.id
lambda_function {
lambda_function_arn = aws_lambda_function.netcdf_to_zarr.arn
events = ["s3:ObjectCreated:*"]
filter_prefix = "ncs/"
filter_suffix = ".nc"
}
lambda_function {
lambda_function_arn = aws_lambda_function.zarr_to_cog.arn
events = ["s3:ObjectCreated:*"]
filter_prefix = "zarrs/"
filter_suffix = ".invoked"
}
depends_on = [aws_lambda_permission.allow_bucket, aws_lambda_permission.allow_bucket_cog]
}
resource "aws_cloudwatch_log_group" "lambda_log_group" {
name = "/aws/lambda/${aws_lambda_function.netcdf_to_zarr.function_name}"
retention_in_days = 14
}
resource "aws_cloudwatch_log_group" "lambda_log_group_cog" {
name = "/aws/lambda/${aws_lambda_function.zarr_to_cog.function_name}"
retention_in_days = 14
}