Skip to content

Commit 46b775c

Browse files
committed
wip
1 parent a8b773b commit 46b775c

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

terraform/configs.tf

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,20 @@ locals {
33
is_temp = length(regexall("[a-z]{2,4}-?[0-9]+", local.env)) > 0
44
dspp_core_account_id = local.environment == "prod" ? 232116723729 : 603871901111
55
immunisation_account_id = local.environment == "prod" ? 664418956997 : 345594581768
6-
}
6+
7+
// MESH Mailbox IDs by environment
8+
prod_mesh_mailbox_id = "X26HC138"
9+
int_mesh_mailbox_id = "X26OT303"
10+
int_mesh_dlq_mailbox_id = "X26OT304"
11+
12+
// Environment-specific MESH configuration
13+
mesh_mailbox_id = local.environment == "prod" ? local.prod_mesh_mailbox_id : (
14+
local.environment == "int" ? local.int_mesh_mailbox_id : var.dev_mesh_mailbox_id
15+
)
16+
17+
// DLQ Mailbox for int only - Not currently implemented TODO
18+
mesh_dlq_mailbox_id = local.environment == "int" ? local.int_mesh_dlq_mailbox_id : null
19+
20+
// MESH enabled if mailbox ID not null
21+
is_mesh_enabled = local.mesh_mailbox_id != null
22+
}

infra/mesh.tf renamed to terraform/mesh.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ module "mesh" {
66
subnet_ids = data.aws_subnets.default.ids
77

88
mailbox_ids = [local.mesh_mailbox_id]
9+
dlq_mailbox_id = local.mesh_dlq_mailbox_id
910
verify_ssl = "true"
1011
get_message_max_concurrency = 10
1112
compress_threshold = 1 * 1024 * 1024
1213
handshake_schedule = "rate(24 hours)"
1314

14-
account_id = 345594581768
15+
account_id = local.immunisation_account_id
1516
}

terraform/mesh_processor.tf

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
# Only create MESH processor resources if MESH is configured for this environment
2+
locals {
3+
create_mesh_processor = local.is_mesh_enabled
4+
}
5+
16
# Define the directory containing the Docker image and calculate its SHA-256 hash for triggering redeployments
27
locals {
38
mesh_processor_lambda_dir = abspath("${path.root}/../mesh_processor")
49
mesh_processor_lambda_files = fileset(local.mesh_processor_lambda_dir, "**")
510
mesh_processor_lambda_dir_sha = sha1(join("", [for f in local.mesh_processor_lambda_files : filesha1("${local.mesh_processor_lambda_dir}/${f}")]))
611
}
712

8-
913
resource "aws_ecr_repository" "mesh_file_converter_lambda_repository" {
14+
count = local.create_mesh_processor ? 1 : 0
15+
1016
image_scanning_configuration {
1117
scan_on_push = true
1218
}
@@ -16,11 +22,12 @@ resource "aws_ecr_repository" "mesh_file_converter_lambda_repository" {
1622

1723
# Module for building and pushing Docker image to ECR
1824
module "mesh_processor_docker_image" {
25+
count = local.create_mesh_processor ? 1 : 0
1926
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
2027
version = "8.0.1"
2128

2229
create_ecr_repo = false
23-
ecr_repo = aws_ecr_repository.mesh_file_converter_lambda_repository.name
30+
ecr_repo = aws_ecr_repository.mesh_file_converter_lambda_repository[0].name
2431
ecr_repo_lifecycle_policy = jsonencode({
2532
"rules" : [
2633
{
@@ -48,7 +55,8 @@ module "mesh_processor_docker_image" {
4855

4956
# Define the lambdaECRImageRetreival policy
5057
resource "aws_ecr_repository_policy" "mesh_processor_lambda_ECRImageRetreival_policy" {
51-
repository = aws_ecr_repository.mesh_file_converter_lambda_repository.name
58+
count = local.create_mesh_processor ? 1 : 0
59+
repository = aws_ecr_repository.mesh_file_converter_lambda_repository[0].name
5260

5361
policy = jsonencode({
5462
Version = "2012-10-17"
@@ -78,7 +86,8 @@ resource "aws_ecr_repository_policy" "mesh_processor_lambda_ECRImageRetreival_po
7886

7987
# IAM Role for Lambda
8088
resource "aws_iam_role" "mesh_processor_lambda_exec_role" {
81-
name = "${local.short_prefix}-mesh_processor-lambda-exec-role"
89+
count = local.create_mesh_processor ? 1 : 0
90+
name = "${local.short_prefix}-mesh_processor-lambda-exec-role"
8291
assume_role_policy = jsonencode({
8392
Version = "2012-10-17",
8493
Statement = [{
@@ -94,7 +103,8 @@ resource "aws_iam_role" "mesh_processor_lambda_exec_role" {
94103

95104
# Policy for Lambda execution role
96105
resource "aws_iam_policy" "mesh_processor_lambda_exec_policy" {
97-
name = "${local.short_prefix}-mesh_processor-lambda-exec-policy"
106+
count = local.create_mesh_processor ? 1 : 0
107+
name = "${local.short_prefix}-mesh_processor-lambda-exec-policy"
98108
policy = jsonencode({
99109
Version = "2012-10-17",
100110
Statement = [
@@ -140,6 +150,7 @@ resource "aws_iam_policy" "mesh_processor_lambda_exec_policy" {
140150
}
141151

142152
resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
153+
count = local.create_mesh_processor ? 1 : 0
143154
name = "${local.short_prefix}-mesh_processor-lambda-kms-policy"
144155
description = "Allow Lambda to decrypt environment variables"
145156

@@ -155,7 +166,6 @@ resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
155166
]
156167
Resource = [
157168
data.aws_kms_key.mesh_s3_encryption_key.arn
158-
# "arn:aws:kms:eu-west-2:345594581768:key/9b756762-bc6f-42fb-ba56-2c0c00c15289"
159169
]
160170
}
161171
]
@@ -164,23 +174,25 @@ resource "aws_iam_policy" "mesh_processor_lambda_kms_access_policy" {
164174

165175
# Attach the execution policy to the Lambda role
166176
resource "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
177+
count = local.create_mesh_processor ? 1 : 0
178+
role = aws_iam_role.mesh_processor_lambda_exec_role[0].name
179+
policy_arn = aws_iam_policy.mesh_processor_lambda_exec_policy[0].arn
169180
}
170181

171-
172182
# Attach the kms policy to the Lambda role
173183
resource "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
184+
count = local.create_mesh_processor ? 1 : 0
185+
role = aws_iam_role.mesh_processor_lambda_exec_role[0].name
186+
policy_arn = aws_iam_policy.mesh_processor_lambda_kms_access_policy[0].arn
176187
}
177188

178189
# Lambda Function with Security Group and VPC.
179190
resource "aws_lambda_function" "mesh_file_converter_lambda" {
191+
count = local.create_mesh_processor ? 1 : 0
180192
function_name = "${local.short_prefix}-mesh_processor_lambda"
181-
role = aws_iam_role.mesh_processor_lambda_exec_role.arn
193+
role = aws_iam_role.mesh_processor_lambda_exec_role[0].arn
182194
package_type = "Image"
183-
image_uri = module.mesh_processor_docker_image.image_uri
195+
image_uri = module.mesh_processor_docker_image[0].image_uri
184196
architectures = ["x86_64"]
185197
timeout = 360
186198

@@ -190,33 +202,33 @@ resource "aws_lambda_function" "mesh_file_converter_lambda" {
190202
MESH_FILE_PROC_LAMBDA_NAME = "imms-${local.env}-meshfileproc_lambda"
191203
}
192204
}
193-
194205
}
195206

196207
# Permission for S3 to invoke Lambda function
197208
resource "aws_lambda_permission" "mesh_s3_invoke_permission" {
209+
count = local.create_mesh_processor ? 1 : 0
198210
statement_id = "AllowExecutionFromS3"
199211
action = "lambda:InvokeFunction"
200-
function_name = aws_lambda_function.mesh_file_converter_lambda.function_name
212+
function_name = aws_lambda_function.mesh_file_converter_lambda[0].function_name
201213
principal = "s3.amazonaws.com"
202-
source_arn = "arn:aws:s3:::local-immunisation-mesh"
214+
source_arn = "arn:aws:s3:::${local.mesh_s3_bucket_name}"
203215
}
204216

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
207217
# S3 Bucket notification to trigger Lambda function
208218
resource "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"
219+
count = local.create_mesh_processor ? 1 : 0
220+
bucket = local.mesh_s3_bucket_name
211221

212222
lambda_function {
213-
lambda_function_arn = aws_lambda_function.mesh_file_converter_lambda.arn
223+
lambda_function_arn = aws_lambda_function.mesh_file_converter_lambda[0].arn
214224
events = ["s3:ObjectCreated:*"]
215-
#filter_prefix =""
216225
}
226+
227+
depends_on = [aws_lambda_permission.mesh_s3_invoke_permission]
217228
}
218229

219230
resource "aws_cloudwatch_log_group" "mesh_file_converter_log_group" {
231+
count = local.create_mesh_processor ? 1 : 0
220232
name = "/aws/lambda/${local.short_prefix}-mesh_processor_lambda"
221233
retention_in_days = 30
222-
}
234+
}

terraform/variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,15 @@ data "aws_kms_key" "existing_kinesis_encryption_key" {
9797
data "aws_kms_key" "mesh_s3_encryption_key" {
9898
key_id = "alias/local-immunisation-mesh"
9999
}
100+
101+
variable "dev_mesh_mailbox_id" {
102+
description = "MESH mailbox ID for dev environment. If null, MESH is disabled. If set, MESH is enabled."
103+
type = string
104+
default = null
105+
}
106+
# Dev DLQ only used if dev mesh mailbox is set
107+
variable "dev_mesh_dlq_mailbox_id" {
108+
description = "MESH DLQ mailbox ID for dev environment. If null, MESH is disabled. If set, MESH is enabled."
109+
type = string
110+
default = null
111+
}

0 commit comments

Comments
 (0)