-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmesh_processor.tf
More file actions
219 lines (198 loc) · 6.54 KB
/
mesh_processor.tf
File metadata and controls
219 lines (198 loc) · 6.54 KB
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
# Define the directory containing the Docker image and calculate its SHA-256 hash for triggering redeployments
locals {
mesh_processor_lambda_dir = abspath("${path.root}/../mesh_processor")
mesh_processor_lambda_files = fileset(local.mesh_processor_lambda_dir, "**")
mesh_processor_lambda_dir_sha = sha1(join("", [for f in local.mesh_processor_lambda_files : filesha1("${local.mesh_processor_lambda_dir}/${f}")]))
}
resource "aws_ecr_repository" "mesh_file_converter_lambda_repository" {
image_scanning_configuration {
scan_on_push = true
}
name = "${local.short_prefix}-mesh_processor-repo"
force_delete = local.is_temp
}
# Module for building and pushing Docker image to ECR
module "mesh_processor_docker_image" {
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
version = "7.20.2"
create_ecr_repo = false
ecr_repo = aws_ecr_repository.mesh_file_converter_lambda_repository.name
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the last 2 images",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 2
},
"action" : {
"type" : "expire"
}
}
]
})
platform = "linux/amd64"
use_image_tag = false
source_path = local.mesh_processor_lambda_dir
triggers = {
dir_sha = local.mesh_processor_lambda_dir_sha
}
}
# Define the lambdaECRImageRetreival policy
resource "aws_ecr_repository_policy" "mesh_processor_lambda_ECRImageRetreival_policy" {
repository = aws_ecr_repository.mesh_file_converter_lambda_repository.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Sid" : "LambdaECRImageRetrievalPolicy",
"Effect" : "Allow",
"Principal" : {
"Service" : "lambda.amazonaws.com"
},
"Action" : [
"ecr:BatchGetImage",
"ecr:DeleteRepositoryPolicy",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:SetRepositoryPolicy"
],
"Condition" : {
"StringLike" : {
"aws:sourceArn" : "arn:aws:lambda:eu-west-2:${local.local_account_id}:function:${local.short_prefix}-mesh_processor_lambda"
}
}
}
]
})
}
# IAM Role for Lambda
resource "aws_iam_role" "mesh_processor_lambda_exec_role" {
name = "${local.short_prefix}-mesh_processor-lambda-exec-role"
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow",
Sid = "",
Principal = {
Service = "lambda.amazonaws.com"
},
Action = "sts:AssumeRole"
}]
})
}
# Policy for Lambda execution role
resource "aws_iam_policy" "mesh_processor_lambda_exec_policy" {
name = "${local.short_prefix}-mesh_processor-lambda-exec-policy"
policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
Resource = "arn:aws:logs:${var.aws_region}:${local.local_account_id}:log-group:/aws/lambda/${local.short_prefix}-mesh_processor_lambda:*"
},
{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:CopyObject"
]
Resource = [
"arn:aws:s3:::${local.batch_prefix}-data-sources",
"arn:aws:s3:::${local.batch_prefix}-data-sources/*"
]
},
{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:CopyObject",
"s3:DeleteObject"
]
Resource = [
"arn:aws:s3:::local-immunisation-mesh",
"arn:aws:s3:::local-immunisation-mesh/*",
"arn:aws:s3:::local-immunisation-mesh-s3logs/*"
]
}
]
})
}
resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
name = "${local.short_prefix}-mesh_processor-lambda-kms-policy"
description = "Allow Lambda to decrypt environment variables"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey*"
]
Resource = [
data.aws_kms_key.mesh_s3_encryption_key.arn
# "arn:aws:kms:eu-west-2:345594581768:key/9b756762-bc6f-42fb-ba56-2c0c00c15289"
]
}
]
})
}
# Attach the execution policy to the Lambda role
resource "aws_iam_role_policy_attachment" "mesh_processor_lambda_exec_policy_attachment" {
role = aws_iam_role.mesh_processor_lambda_exec_role.name
policy_arn = aws_iam_policy.mesh_processor_lambda_exec_policy.arn
}
# Attach the kms policy to the Lambda role
resource "aws_iam_role_policy_attachment" "mesh_processor_lambda_kms_policy_attachment" {
role = aws_iam_role.mesh_processor_lambda_exec_role.name
policy_arn = aws_iam_policy.mesh_processor_lambda_kms_access_policy.arn
}
# Lambda Function with Security Group and VPC.
resource "aws_lambda_function" "mesh_file_converter_lambda" {
function_name = "${local.short_prefix}-mesh_processor_lambda"
role = aws_iam_role.mesh_processor_lambda_exec_role.arn
package_type = "Image"
image_uri = module.mesh_processor_docker_image.image_uri
architectures = ["x86_64"]
timeout = 360
environment {
variables = {
Destination_BUCKET_NAME = "${local.batch_prefix}-data-sources"
MESH_FILE_PROC_LAMBDA_NAME = "imms-${local.env}-meshfileproc_lambda"
}
}
}
# Permission for S3 to invoke Lambda function
resource "aws_lambda_permission" "mesh_s3_invoke_permission" {
statement_id = "AllowExecutionFromS3"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.mesh_file_converter_lambda.function_name
principal = "s3.amazonaws.com"
source_arn = "arn:aws:s3:::local-immunisation-mesh"
}
# S3 Bucket notification to trigger Lambda function
resource "aws_s3_bucket_notification" "mesh_datasources_lambda_notification" {
bucket = "local-immunisation-mesh"
lambda_function {
lambda_function_arn = aws_lambda_function.mesh_file_converter_lambda.arn
events = ["s3:ObjectCreated:*"]
#filter_prefix =""
}
}
resource "aws_cloudwatch_log_group" "mesh_file_converter_log_group" {
name = "/aws/lambda/${local.short_prefix}-mesh_processor_lambda"
retention_in_days = 30
}