Skip to content

Commit 6d2a3f4

Browse files
committed
refactor test port. fix lambda deploy in vpc.
1 parent 55d93b0 commit 6d2a3f4

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

examples/simple-lambda-with-deploy-test/example.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module "lambda_api" {
1919
public_subnet_ids = module.acs.public_subnet_ids
2020
private_subnet_ids = module.acs.private_subnet_ids
2121
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
22+
codedeploy_test_listener_port = 4443
2223

2324
codedeploy_lifecycle_hooks = {
2425
BeforeAllowTraffic = aws_lambda_function.test_lambda.function_name

main.tf

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ resource "aws_security_group" "alb-sg" {
5656
protocol = "tcp"
5757
cidr_blocks = ["0.0.0.0/0"]
5858
}
59-
ingress {
60-
from_port = 4443
61-
to_port = 4443
62-
protocol = "tcp"
63-
cidr_blocks = ["0.0.0.0/0"]
59+
// if test listner port is specified, allow traffic
60+
dynamic "ingress" {
61+
for_each = var.codedeploy_test_listener_port != null ? [1] : []
62+
content {
63+
from_port = var.codedeploy_test_listener_port
64+
to_port = var.codedeploy_test_listener_port
65+
protocol = "tcp"
66+
cidr_blocks = ["0.0.0.0/0"]
67+
}
6468
}
6569
// allow any outgoing traffic
6670
egress {
@@ -208,6 +212,11 @@ resource "aws_iam_role" "iam_for_lambda" {
208212
EOF
209213
}
210214

215+
resource "aws_iam_role_policy_attachment" "lambda_eni_attach" {
216+
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaENIManagementAccess"
217+
role = aws_iam_role.iam_for_lambda.name
218+
}
219+
211220
resource "aws_iam_role_policy_attachment" "lambda_policy_attach" {
212221
count = length(var.lambda_policies)
213222
policy_arn = element(var.lambda_policies, count.index)
@@ -220,6 +229,22 @@ data "archive_file" "cleanup_lambda_zip" {
220229
type = "zip"
221230
}
222231

232+
resource "aws_security_group" "lambda_sg" {
233+
name = "${local.long_name}-lambda-sg"
234+
description = "Controls access to the Lambda"
235+
vpc_id = var.vpc_id
236+
237+
# ingress not needed as ALB invokes Lambda via AWS API, not direct network traffic
238+
239+
egress {
240+
from_port = 0
241+
to_port = 0
242+
protocol = "-1"
243+
cidr_blocks = ["0.0.0.0/0"]
244+
}
245+
tags = var.tags
246+
}
247+
223248
resource "aws_lambda_function" "api_lambda" {
224249
filename = data.archive_file.cleanup_lambda_zip.output_path
225250
source_code_hash = data.archive_file.cleanup_lambda_zip.output_base64sha256
@@ -229,9 +254,9 @@ resource "aws_lambda_function" "api_lambda" {
229254
runtime = "nodejs12.x"
230255
publish = true
231256

232-
vpc_config = {
257+
vpc_config {
233258
subnet_ids = var.private_subnet_ids
234-
security_group_ids = var.lambda_security_groups
259+
security_group_ids = concat([aws_security_group.lambda_sg.id], var.security_groups)
235260
}
236261

237262
# environment {

variables.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ variable "codedeploy_lifecycle_hooks" {
4646
default = null
4747
}
4848

49+
variable "codedeploy_test_listener_port" {
50+
type = number
51+
description = "The port for a codedeploy test listener. If provided CodeDeploy will use this port for test traffic on the new replacement set during the blue-green deployment process before shifting production traffic to the replacement set. Defaults to null"
52+
default = null
53+
}
54+
4955
variable "vpc_id" {
5056
type = string
5157
description = "VPC ID to deploy ECS fargate service."
@@ -56,7 +62,7 @@ variable "public_subnet_ids" {
5662
}
5763
variable "private_subnet_ids" {
5864
type = list(string)
59-
description = "List of subnet IDs for the fargate service."
65+
description = "List of subnet IDs for the Lambda service."
6066
}
6167

6268
variable "tags" {
@@ -84,6 +90,6 @@ variable "lambda_policies" {
8490

8591
variable "security_groups" {
8692
type = list(string)
87-
description = "List of extra security group IDs to attach to the fargate task."
93+
description = "List of extra security group IDs to attach to the lambda."
8894
default = []
8995
}

0 commit comments

Comments
 (0)