11# Define the directory containing the Docker image and calculate its SHA-256 hash for triggering redeployments
22locals {
3+ create_mesh_processor = local. environment == " int" || local. environment == " prod"
34 mesh_processor_lambda_dir = abspath (" ${ path . root } /../mesh_processor" )
45 mesh_processor_lambda_files = fileset (local. mesh_processor_lambda_dir , " **" )
56 mesh_processor_lambda_dir_sha = sha1 (join (" " , [for f in local . mesh_processor_lambda_files : filesha1 (" ${ local . mesh_processor_lambda_dir } /${ f } " )]))
7+ # This should match the prefix used in the infra Terraform
8+ mesh_module_prefix = " imms-${ local . config_env } "
69}
710
11+ data "aws_s3_bucket" "mesh" {
12+ count = local. create_mesh_processor ? 1 : 0
13+
14+ bucket = " ${ local . mesh_module_prefix } -mesh"
15+ }
16+
17+ data "aws_kms_key" "mesh" {
18+ count = local. create_mesh_processor ? 1 : 0
19+
20+ key_id = " alias/${ local . mesh_module_prefix } -mesh"
21+ }
822
923resource "aws_ecr_repository" "mesh_file_converter_lambda_repository" {
24+ count = local. create_mesh_processor ? 1 : 0
25+
1026 image_scanning_configuration {
1127 scan_on_push = true
1228 }
@@ -16,11 +32,13 @@ resource "aws_ecr_repository" "mesh_file_converter_lambda_repository" {
1632
1733# Module for building and pushing Docker image to ECR
1834module "mesh_processor_docker_image" {
35+ count = local. create_mesh_processor ? 1 : 0
36+
1937 source = " terraform-aws-modules/lambda/aws//modules/docker-build"
2038 version = " 8.0.1"
2139
2240 create_ecr_repo = false
23- ecr_repo = aws_ecr_repository. mesh_file_converter_lambda_repository . name
41+ ecr_repo = aws_ecr_repository. mesh_file_converter_lambda_repository [ 0 ] . name
2442 ecr_repo_lifecycle_policy = jsonencode ({
2543 " rules" : [
2644 {
@@ -48,7 +66,9 @@ module "mesh_processor_docker_image" {
4866
4967# Define the lambdaECRImageRetreival policy
5068resource "aws_ecr_repository_policy" "mesh_processor_lambda_ECRImageRetreival_policy" {
51- repository = aws_ecr_repository. mesh_file_converter_lambda_repository . name
69+ count = local. create_mesh_processor ? 1 : 0
70+
71+ repository = aws_ecr_repository. mesh_file_converter_lambda_repository [0 ]. name
5272
5373 policy = jsonencode ({
5474 Version = " 2012-10-17"
@@ -78,6 +98,8 @@ resource "aws_ecr_repository_policy" "mesh_processor_lambda_ECRImageRetreival_po
7898
7999# IAM Role for Lambda
80100resource "aws_iam_role" "mesh_processor_lambda_exec_role" {
101+ count = local. create_mesh_processor ? 1 : 0
102+
81103 name = " ${ local . short_prefix } -mesh_processor-lambda-exec-role"
82104 assume_role_policy = jsonencode ({
83105 Version = " 2012-10-17" ,
@@ -94,6 +116,8 @@ resource "aws_iam_role" "mesh_processor_lambda_exec_role" {
94116
95117# Policy for Lambda execution role
96118resource "aws_iam_policy" "mesh_processor_lambda_exec_policy" {
119+ count = local. create_mesh_processor ? 1 : 0
120+
97121 name = " ${ local . short_prefix } -mesh_processor-lambda-exec-policy"
98122 policy = jsonencode ({
99123 Version = " 2012-10-17" ,
@@ -130,16 +154,17 @@ resource "aws_iam_policy" "mesh_processor_lambda_exec_policy" {
130154 " s3:DeleteObject"
131155 ]
132156 Resource = [
133- " arn:aws:s3:::local-immunisation-mesh" ,
134- " arn:aws:s3:::local-immunisation-mesh/*" ,
135- " arn:aws:s3:::local-immunisation-mesh-s3logs/*"
157+ data.aws_s3_bucket.mesh[0 ].arn,
158+ " ${ data . aws_s3_bucket . mesh [0 ]. arn } /*"
136159 ]
137160 }
138161 ]
139162 })
140163}
141164
142165resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
166+ count = local. create_mesh_processor ? 1 : 0
167+
143168 name = " ${ local . short_prefix } -mesh_processor-lambda-kms-policy"
144169 description = " Allow Lambda to decrypt environment variables"
145170
@@ -154,8 +179,7 @@ resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
154179 " kms:GenerateDataKey*"
155180 ]
156181 Resource = [
157- data.aws_kms_key.mesh_s3_encryption_key.arn
158- # "arn:aws:kms:eu-west-2:345594581768:key/9b756762-bc6f-42fb-ba56-2c0c00c15289"
182+ data.aws_kms_key.mesh[0 ].arn
159183 ]
160184 }
161185 ]
@@ -164,59 +188,64 @@ resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
164188
165189# Attach the execution policy to the Lambda role
166190resource "aws_iam_role_policy_attachment" "mesh_processor_lambda_exec_policy_attachment" {
167- role = aws_iam_role. mesh_processor_lambda_exec_role . name
168- policy_arn = aws_iam_policy. mesh_processor_lambda_exec_policy . arn
191+ count = local. create_mesh_processor ? 1 : 0
192+
193+ role = aws_iam_role. mesh_processor_lambda_exec_role [0 ]. name
194+ policy_arn = aws_iam_policy. mesh_processor_lambda_exec_policy [0 ]. arn
169195}
170196
171197
172198# Attach the kms policy to the Lambda role
173199resource "aws_iam_role_policy_attachment" "mesh_processor_lambda_kms_policy_attachment" {
174- role = aws_iam_role. mesh_processor_lambda_exec_role . name
175- policy_arn = aws_iam_policy. mesh_processor_lambda_kms_access_policy . arn
200+ count = local. create_mesh_processor ? 1 : 0
201+
202+ role = aws_iam_role. mesh_processor_lambda_exec_role [0 ]. name
203+ policy_arn = aws_iam_policy. mesh_processor_lambda_kms_access_policy [0 ]. arn
176204}
177205
178206# Lambda Function with Security Group and VPC.
179207resource "aws_lambda_function" "mesh_file_converter_lambda" {
208+ count = local. create_mesh_processor ? 1 : 0
209+
180210 function_name = " ${ local . short_prefix } -mesh_processor_lambda"
181- role = aws_iam_role. mesh_processor_lambda_exec_role . arn
211+ role = aws_iam_role. mesh_processor_lambda_exec_role [ 0 ] . arn
182212 package_type = " Image"
183- image_uri = module. mesh_processor_docker_image . image_uri
213+ image_uri = module. mesh_processor_docker_image [ 0 ] . image_uri
184214 architectures = [" x86_64" ]
185215 timeout = 360
186216
187217 environment {
188218 variables = {
189- Destination_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
190- MESH_FILE_PROC_LAMBDA_NAME = " imms-${ local . env } -meshfileproc_lambda"
219+ Destination_BUCKET_NAME = aws_s3_bucket.batch_data_source_bucket.bucket
191220 }
192221 }
193-
194222}
195223
196224# Permission for S3 to invoke Lambda function
197225resource "aws_lambda_permission" "mesh_s3_invoke_permission" {
226+ count = local. create_mesh_processor ? 1 : 0
227+
198228 statement_id = " AllowExecutionFromS3"
199229 action = " lambda:InvokeFunction"
200- function_name = aws_lambda_function. mesh_file_converter_lambda . function_name
230+ function_name = aws_lambda_function. mesh_file_converter_lambda [ 0 ] . function_name
201231 principal = " s3.amazonaws.com"
202232 source_arn = " arn:aws:s3:::local-immunisation-mesh"
203233}
204234
205- # TODO - This is scoped to the bucket, so is overwritten by each deployment
206- # That might be intentional in prod, to switch between blue and green, but surely isn't in non-prod
207- # S3 Bucket notification to trigger Lambda function
208235resource "aws_s3_bucket_notification" "mesh_datasources_lambda_notification" {
209- # TODO - what is this bucket and why isn't it managed by Terraform?
210- bucket = " local-immunisation-mesh"
236+ count = local. create_mesh_processor ? 1 : 0
237+
238+ bucket = data. aws_s3_bucket . mesh [0 ]. bucket
211239
212240 lambda_function {
213- lambda_function_arn = aws_lambda_function. mesh_file_converter_lambda . arn
241+ lambda_function_arn = aws_lambda_function. mesh_file_converter_lambda [ 0 ] . arn
214242 events = [" s3:ObjectCreated:*" ]
215- # filter_prefix =""
216243 }
217244}
218245
219246resource "aws_cloudwatch_log_group" "mesh_file_converter_log_group" {
247+ count = local. create_mesh_processor ? 1 : 0
248+
220249 name = " /aws/lambda/${ local . short_prefix } -mesh_processor_lambda"
221250 retention_in_days = 30
222251}
0 commit comments