11# Define the directory containing the Docker image and calculate its SHA-256 hash for triggering redeployments
22locals {
3- redis_sync_lambda_dir = abspath (" ${ path . root } /../redis_sync" )
4- redis_sync_lambda_files = fileset (local. redis_sync_lambda_dir , " **" )
5- redis_sync_lambda_dir_sha = sha1 (join (" " , [for f in local . redis_sync_lambda_files : filesha1 (" ${ local . redis_sync_lambda_dir } /${ f } " )]))
3+ id_sync_lambda_dir = abspath (" ${ path . root } /../redis_sync" )
4+ id_sync_lambda_files = fileset (local. id_sync_lambda_dir , " **" )
5+ id_sync_lambda_dir_sha = sha1 (join (" " , [for f in local . id_sync_lambda_files : filesha1 (" ${ local . id_sync_lambda_dir } /${ f } " )]))
66}
77
8- resource "aws_ecr_repository" "redis_sync_lambda_repository " {
8+ resource "aws_ecr_repository" "id_sync_lambda_repository " {
99 image_scanning_configuration {
1010 scan_on_push = true
1111 }
12- name = " ${ local . short_prefix } -redis -sync-repo"
12+ name = " ${ local . short_prefix } -id -sync-repo"
1313 force_delete = local. is_temp
1414}
1515
1616# Module for building and pushing Docker image to ECR
17- module "redis_sync_docker_image " {
17+ module "id_sync_docker_image " {
1818 source = " terraform-aws-modules/lambda/aws//modules/docker-build"
1919 version = " 8.0.1"
2020
2121 create_ecr_repo = false
22- ecr_repo = aws_ecr_repository. redis_sync_lambda_repository . name
22+ ecr_repo = aws_ecr_repository. id_sync_lambda_repository . name
2323 ecr_repo_lifecycle_policy = jsonencode ({
2424 " rules" : [
2525 {
@@ -39,15 +39,15 @@ module "redis_sync_docker_image" {
3939
4040 platform = " linux/amd64"
4141 use_image_tag = false
42- source_path = local. redis_sync_lambda_dir
42+ source_path = local. id_sync_lambda_dir
4343 triggers = {
44- dir_sha = local.redis_sync_lambda_dir_sha
44+ dir_sha = local.id_sync_lambda_dir_sha
4545 }
4646}
4747
4848# Define the lambdaECRImageRetreival policy
49- resource "aws_ecr_repository_policy" "redis_sync_lambda_ECRImageRetreival_policy " {
50- repository = aws_ecr_repository. redis_sync_lambda_repository . name
49+ resource "aws_ecr_repository_policy" "id_sync_lambda_ECRImageRetreival_policy " {
50+ repository = aws_ecr_repository. id_sync_lambda_repository . name
5151
5252 policy = jsonencode ({
5353 Version = " 2012-10-17"
@@ -67,7 +67,7 @@ resource "aws_ecr_repository_policy" "redis_sync_lambda_ECRImageRetreival_policy
6767 ],
6868 Condition : {
6969 StringLike : {
70- " aws:sourceArn" : aws_lambda_function.redis_sync_lambda .arn
70+ " aws:sourceArn" : aws_lambda_function.id_sync_lambda .arn
7171 }
7272 }
7373 }
@@ -76,8 +76,8 @@ resource "aws_ecr_repository_policy" "redis_sync_lambda_ECRImageRetreival_policy
7676}
7777
7878# IAM Role for Lambda
79- resource "aws_iam_role" "redis_sync_lambda_exec_role " {
80- name = " ${ local . short_prefix } -redis -sync-lambda-exec-role"
79+ resource "aws_iam_role" "id_sync_lambda_exec_role " {
80+ name = " ${ local . short_prefix } -id -sync-lambda-exec-role"
8181 assume_role_policy = jsonencode ({
8282 Version = " 2012-10-17" ,
8383 Statement = [{
@@ -92,8 +92,8 @@ resource "aws_iam_role" "redis_sync_lambda_exec_role" {
9292}
9393
9494# Policy for Lambda execution role
95- resource "aws_iam_policy" "redis_sync_lambda_exec_policy " {
96- name = " ${ local . short_prefix } -redis -sync-lambda-exec-policy"
95+ resource "aws_iam_policy" "id_sync_lambda_exec_policy " {
96+ name = " ${ local . short_prefix } -id -sync-lambda-exec-policy"
9797 policy = jsonencode ({
9898 Version = " 2012-10-17" ,
9999 Statement = [
@@ -104,7 +104,7 @@ resource "aws_iam_policy" "redis_sync_lambda_exec_policy" {
104104 " logs:CreateLogStream" ,
105105 " logs:PutLogEvents"
106106 ]
107- Resource = " arn:aws:logs:${ var . aws_region } :${ var . immunisation_account_id } :log-group:/aws/lambda/${ local . short_prefix } -redis_sync_lambda :*"
107+ Resource = " arn:aws:logs:${ var . aws_region } :${ var . immunisation_account_id } :log-group:/aws/lambda/${ local . short_prefix } -id_sync_lambda :*"
108108 },
109109 {
110110 Effect = " Allow"
@@ -165,10 +165,11 @@ resource "aws_iam_policy" "redis_sync_lambda_exec_policy" {
165165 Effect = " Allow"
166166 Action = " lambda:InvokeFunction"
167167 Resource = [
168- " arn:aws:lambda:${ var . aws_region } :${ var . immunisation_account_id } :function:imms-${ var . sub_environment } -redis_sync_lambda " ,
168+ " arn:aws:lambda:${ var . aws_region } :${ var . immunisation_account_id } :function:imms-${ var . sub_environment } -id_sync_lambda " ,
169169 ]
170170 },
171171 # NEW
172+ # NB anomaly: do we want this in "id_sync_lambda_sqs_access_policy"?
172173 {
173174 Effect = " Allow" ,
174175 Action = [
@@ -178,6 +179,7 @@ resource "aws_iam_policy" "redis_sync_lambda_exec_policy" {
178179 ],
179180 Resource = " arn:aws:sqs:eu-west-2:${ var . immunisation_account_id } :${ local . short_prefix } -id-sync-queue"
180181 },
182+ # NB anomaly: in redis_sync this appears in "redis_sync_lambda_kms_access_policy"
181183 {
182184 Effect = " Allow" ,
183185 Action = [
@@ -190,8 +192,8 @@ resource "aws_iam_policy" "redis_sync_lambda_exec_policy" {
190192 })
191193}
192194
193- resource "aws_iam_policy" "redis_sync_lambda_kms_access_policy " {
194- name = " ${ local . short_prefix } -redis -sync-lambda-kms-policy"
195+ resource "aws_iam_policy" "id_sync_lambda_kms_access_policy " {
196+ name = " ${ local . short_prefix } -id -sync-lambda-kms-policy"
195197 description = " Allow Lambda to decrypt environment variables"
196198
197199 policy = jsonencode ({
@@ -220,23 +222,23 @@ resource "aws_iam_policy" "redis_sync_lambda_kms_access_policy" {
220222}
221223
222224# Attach the execution policy to the Lambda role
223- resource "aws_iam_role_policy_attachment" "redis_sync_lambda_exec_policy_attachment " {
224- role = aws_iam_role. redis_sync_lambda_exec_role . name
225- policy_arn = aws_iam_policy. redis_sync_lambda_exec_policy . arn
225+ resource "aws_iam_role_policy_attachment" "id_sync_lambda_exec_policy_attachment " {
226+ role = aws_iam_role. id_sync_lambda_exec_role . name
227+ policy_arn = aws_iam_policy. id_sync_lambda_exec_policy . arn
226228}
227229
228230# Attach the kms policy to the Lambda role
229- resource "aws_iam_role_policy_attachment" "redis_sync_lambda_kms_policy_attachment " {
230- role = aws_iam_role. redis_sync_lambda_exec_role . name
231- policy_arn = aws_iam_policy. redis_sync_lambda_kms_access_policy . arn
231+ resource "aws_iam_role_policy_attachment" "id_sync_lambda_kms_policy_attachment " {
232+ role = aws_iam_role. id_sync_lambda_exec_role . name
233+ policy_arn = aws_iam_policy. id_sync_lambda_kms_access_policy . arn
232234}
233235
234236# Lambda Function with Security Group and VPC.
235- resource "aws_lambda_function" "redis_sync_lambda " {
236- function_name = " ${ local . short_prefix } -redis_sync_lambda "
237- role = aws_iam_role. redis_sync_lambda_exec_role . arn
237+ resource "aws_lambda_function" "id_sync_lambda " {
238+ function_name = " ${ local . short_prefix } -id_sync_lambda "
239+ role = aws_iam_role. id_sync_lambda_exec_role . arn
238240 package_type = " Image"
239- image_uri = module. redis_sync_docker_image . image_uri
241+ image_uri = module. id_sync_docker_image . image_uri
240242 architectures = [" x86_64" ]
241243 timeout = 360
242244
@@ -250,7 +252,7 @@ resource "aws_lambda_function" "redis_sync_lambda" {
250252 CONFIG_BUCKET_NAME = local.config_bucket_name
251253 REDIS_HOST = data.aws_elasticache_cluster.existing_redis.cache_nodes[0 ].address
252254 REDIS_PORT = data.aws_elasticache_cluster.existing_redis.cache_nodes[0 ].port
253- REDIS_SYNC_PROC_LAMBDA_NAME = " imms-${ var . sub_environment } -redis_sync_lambda "
255+ ID_SYNC_PROC_LAMBDA_NAME = " imms-${ var . sub_environment } -id_sync_lambda "
254256 # NEW
255257 DELTA_TABLE_NAME = aws_dynamodb_table.delta- dynamodb- table.name
256258 PDS_ENV = var.pds_environment
@@ -260,13 +262,13 @@ resource "aws_lambda_function" "redis_sync_lambda" {
260262 kms_key_arn = data. aws_kms_key . existing_lambda_encryption_key . arn
261263
262264 depends_on = [
263- aws_cloudwatch_log_group . redis_sync_log_group ,
264- aws_iam_policy . redis_sync_lambda_exec_policy
265+ aws_cloudwatch_log_group . id_sync_log_group ,
266+ aws_iam_policy . id_sync_lambda_exec_policy
265267 ]
266268}
267269
268- resource "aws_cloudwatch_log_group" "redis_sync_log_group " {
269- name = " /aws/lambda/${ local . short_prefix } -redis_sync_lambda "
270+ resource "aws_cloudwatch_log_group" "id_sync_log_group " {
271+ name = " /aws/lambda/${ local . short_prefix } -id_sync_lambda "
270272 retention_in_days = 30
271273}
272274
@@ -276,7 +278,7 @@ resource "aws_s3_bucket_notification" "config_lambda_notification" {
276278 bucket = aws_s3_bucket. batch_config_bucket . bucket
277279
278280 lambda_function {
279- lambda_function_arn = aws_lambda_function. redis_sync_lambda . arn
281+ lambda_function_arn = aws_lambda_function. id_sync_lambda . arn
280282 events = [" s3:ObjectCreated:*" ]
281283 }
282284}
@@ -286,15 +288,15 @@ resource "aws_lambda_permission" "new_s3_invoke_permission" {
286288
287289 statement_id = " AllowExecutionFromNewS3"
288290 action = " lambda:InvokeFunction"
289- function_name = aws_lambda_function. redis_sync_lambda . function_name
291+ function_name = aws_lambda_function. id_sync_lambda . function_name
290292 principal = " s3.amazonaws.com"
291293 source_arn = local. config_bucket_arn
292294}
293295
294296# NEW
295297resource "aws_lambda_event_source_mapping" "id_sync_sqs_trigger" {
296298 event_source_arn = " arn:aws:sqs:eu-west-2:${ local . immunisation_account_id } :${ local . short_prefix } -id-sync-queue"
297- function_name = aws_lambda_function. redis_sync_lambda . arn # TODO
299+ function_name = aws_lambda_function. id_sync_lambda . arn # TODO
298300
299301 # Optional: Configure batch size and other settings
300302 batch_size = 10
0 commit comments