Skip to content

Commit fda8193

Browse files
committed
[NDR-234] Move lambda-document-upload-check into VPC
1 parent a866b9c commit fda8193

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

infrastructure/lambda-document-upload-check.tf

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module "document_upload_check_lambda" {
1010
aws_iam_policy.ssm_access_policy.policy,
1111
module.lloyd_george_reference_dynamodb_table.dynamodb_read_policy_document,
1212
module.lloyd_george_reference_dynamodb_table.dynamodb_write_policy_document,
13+
data.aws_iam_policy.aws_lambda_vpc_access_execution_role.policy
1314
]
1415
rest_api_id = null
1516
http_methods = null
@@ -25,6 +26,8 @@ module "document_upload_check_lambda" {
2526
lambda_timeout = 900
2627
is_gateway_integration_needed = false
2728
is_invoked_from_gateway = false
29+
vpc_subnet_ids = length(data.aws_security_groups.virus_scanner_api.ids) == 1 ? module.ndr-vpc-ui.private_subnets : []
30+
vpc_security_group_ids = length(data.aws_security_groups.virus_scanner_api.ids) == 1 ? [data.aws_security_groups.virus_scanner_api.ids[0]] : []
2831
depends_on = [
2932
aws_api_gateway_rest_api.ndr_doc_store_api,
3033
module.ndr-bulk-staging-store,
@@ -33,6 +36,16 @@ module "document_upload_check_lambda" {
3336
]
3437
}
3538

39+
data "aws_iam_policy" "aws_lambda_vpc_access_execution_role" {
40+
arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
41+
}
42+
43+
data "aws_security_groups" "virus_scanner_api" {
44+
filter {
45+
name = "description"
46+
values = ["Security Group for CloudStorageSec Api Agent"]
47+
}
48+
}
3649

3750
resource "aws_s3_bucket_notification" "document_upload_check_lambda_trigger" {
3851
count = 1
@@ -51,4 +64,5 @@ resource "aws_lambda_permission" "document_upload_check_lambda" {
5164
function_name = module.document_upload_check_lambda[0].function_name
5265
principal = "s3.amazonaws.com"
5366
source_arn = "arn:aws:s3:::${module.ndr-bulk-staging-store.bucket_id}"
54-
}
67+
}
68+

infrastructure/modules/lambda/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,34 @@ resource "aws_lambda_function" "lambda" {
1010
timeout = var.lambda_timeout
1111
memory_size = var.memory_size
1212
reserved_concurrent_executions = var.reserved_concurrent_executions
13+
1314
ephemeral_storage {
1415
size = var.lambda_ephemeral_storage
1516
}
17+
1618
environment {
1719
variables = var.lambda_environment_variables
1820
}
21+
22+
vpc_config {
23+
subnet_ids = var.vpc_subnet_ids
24+
security_group_ids = var.vpc_security_group_ids
25+
}
26+
1927
layers = local.lambda_layers
28+
2029
lifecycle {
2130
ignore_changes = [
2231
# These are required as Lambdas are deployed via the CI/CD pipelines
2332
source_code_hash,
2433
layers
2534
]
2635
}
36+
37+
depends_on = [
38+
aws_iam_role_policy_attachment.default_policies,
39+
aws_iam_role_policy_attachment.lambda_execution_policy
40+
]
2741
}
2842

2943
resource "aws_cloudwatch_log_group" "lambda_logs" {

infrastructure/modules/lambda/variable.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,16 @@ variable "extra_lambda_layers" {
111111
type = list(string)
112112
default = ["arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:53"]
113113
}
114+
115+
variable "vpc_subnet_ids" {
116+
description = "List of subnet IDs associated with the Lambda function, if it sits within a VPC."
117+
type = list(string)
118+
default = []
119+
}
120+
121+
variable "vpc_security_group_ids" {
122+
description = "List of security group IDs associated with the Lambda function, if it sits within a VPC."
123+
type = list(string)
124+
default = []
125+
}
126+

0 commit comments

Comments
 (0)