Skip to content

Commit 052183b

Browse files
committed
VED-26: Attach Lambda functions and ECS tasks to private subnets only.
1 parent a0f63e8 commit 052183b

File tree

6 files changed

+39
-21
lines changed

6 files changed

+39
-21
lines changed

terraform/.terraform.lock.hcl

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/ecs_batch_processor_config.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ resource "aws_pipes_pipe" "fifo_pipe" {
334334
launch_type = "FARGATE"
335335
network_configuration {
336336
aws_vpc_configuration {
337-
subnets = data.aws_subnets.default.ids
337+
subnets = local.private_subnet_ids
338338
assign_public_ip = "ENABLED"
339339
}
340340
}

terraform/endpoints.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module "imms_event_endpoint_lambdas" {
6666
image_uri = module.docker_image.image_uri
6767
policy_json = data.aws_iam_policy_document.imms_policy_document.json
6868
environments = local.imms_lambda_env_vars
69-
vpc_subnet_ids = data.aws_subnets.default.ids
69+
vpc_subnet_ids = local.private_subnet_ids
7070
vpc_security_group_ids = [data.aws_security_group.existing_securitygroup.id]
7171
}
7272

terraform/file_name_processor.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ resource "aws_lambda_function" "file_processor_lambda" {
276276
timeout = 360
277277

278278
vpc_config {
279-
subnet_ids = data.aws_subnets.default.ids
279+
subnet_ids = local.private_subnet_ids
280280
security_group_ids = [data.aws_security_group.existing_securitygroup.id]
281281
}
282282

terraform/redis_sync_lambda.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ resource "aws_lambda_function" "redis_sync_lambda" {
224224
timeout = 360
225225

226226
vpc_config {
227-
subnet_ids = data.aws_subnets.default.ids
227+
subnet_ids = local.private_subnet_ids
228228
security_group_ids = [data.aws_security_group.existing_securitygroup.id]
229229
}
230230

terraform/variables.tf

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,37 @@ locals {
3131
create_config_bucket = local.environment == local.config_bucket_env
3232
config_bucket_arn = local.create_config_bucket ? aws_s3_bucket.batch_config_bucket[0].arn : data.aws_s3_bucket.existing_config_bucket[0].arn
3333
config_bucket_name = local.create_config_bucket ? aws_s3_bucket.batch_config_bucket[0].bucket : data.aws_s3_bucket.existing_config_bucket[0].bucket
34+
35+
# Public subnet - The subnet has a direct route to an internet gateway. Resources in a public subnet can access the public internet.
36+
# public_subnet_ids = [for k, v in data.aws_route.internet_traffic_route_by_subnet : k if length(v.gateway_id) > 0]
37+
# Private subnet - The subnet does not have a direct route to an internet gateway. Resources in a private subnet require a NAT device to access the public internet.
38+
private_subnet_ids = [for k, v in data.aws_route.internet_traffic_route_by_subnet : k if length(v.nat_gateway_id) > 0]
3439
}
3540

3641
data "aws_vpc" "default" {
3742
default = true
3843
}
3944

40-
data "aws_subnets" "default" {
45+
data "aws_subnets" "all" {
4146
filter {
4247
name = "vpc-id"
4348
values = [data.aws_vpc.default.id]
4449
}
4550
}
4651

52+
data "aws_route_table" "route_table_by_subnet" {
53+
for_each = toset(data.aws_subnets.all.ids)
54+
55+
subnet_id = each.value
56+
}
57+
58+
data "aws_route" "internet_traffic_route_by_subnet" {
59+
for_each = data.aws_route_table.route_table_by_subnet
60+
61+
route_table_id = each.value.id
62+
destination_cidr_block = "0.0.0.0/0"
63+
}
64+
4765
data "aws_kms_key" "existing_s3_encryption_key" {
4866
key_id = "alias/imms-batch-s3-shared-key"
4967
}

0 commit comments

Comments
 (0)