Skip to content

Commit 672455a

Browse files
authored
Merge pull request #8 from byu-oit/vpc-optional
Vpc optional
2 parents 6c72f7a + 49fc2b8 commit 672455a

File tree

10 files changed

+125
-24
lines changed

10 files changed

+125
-24
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Also Note: CodePipeline and CodeDeploy cannot be used together to deploy a Lambd
2626
## Usage
2727
```hcl
2828
module "lambda_api" {
29-
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.0.1"
29+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.1.0"
3030
app_name = "my-lambda"
3131
env = "dev"
3232
codedeploy_service_role_arn = module.acs.power_builder_role.arn
@@ -37,13 +37,17 @@ module "lambda_api" {
3737
https_certificate_arn = module.acs.certificate.arn
3838
vpc_id = module.acs.vpc.id
3939
public_subnet_ids = module.acs.public_subnet_ids
40-
private_subnet_ids = module.acs.private_subnet_ids
4140
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
4241
codedeploy_test_listener_port = 4443
4342
use_codedeploy = true
4443
timeout = 3
4544
memory_size = 128
4645
46+
lambda_vpc_config = {
47+
subnet_ids = module.acs.private_subnet_ids
48+
security_group_ids = ["sg-3asdfadsfasdfas"]
49+
}
50+
4751
codedeploy_lifecycle_hooks = {
4852
BeforeAllowTraffic = aws_lambda_function.test_lambda.function_name
4953
AfterAllowTraffic = null
@@ -81,23 +85,29 @@ module "lambda_api" {
8185
| lambda_zip_file | string | File that contains your compiled or zipped source code. |
8286
| handler | string | Lambda event handler |
8387
| runtime | string | Lambda runtime |
88+
| lambda_vpc_config | [object](#lambda_vpc_config) | Lambda VPC object. Used if lambda requires to run inside a VPC | null
8489
| environment_variables | map(string) | A map that defines environment variables for the Lambda function. |
8590
| hosted_zone | [object](#hosted_zone) | Hosted Zone object to redirect to ALB. (Can pass in the aws_hosted_zone object). A and AAAA records created in this hosted zone. |
8691
| https_certificate_arn | string | ARN of the HTTPS certificate of the hosted zone/domain. |
8792
| codedeploy_lifecycle_hooks | [object](#codedeploy_lifecycle_hooks) | Define Lambda Functions for CodeDeploy lifecycle event hooks. Or set this variable to null to not have any lifecycle hooks invoked. Defaults to null | null
8893
| codedeploy_test_listener_port | number | 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 | null
89-
| vpc_id | string | VPC ID to deploy ECS fargate service. |
94+
| vpc_id | string | VPC ID to deploy ALB and Lambda (If specified). |
9095
| public_subnet_ids | list(string) | List of subnet IDs for the ALB. |
91-
| private_subnet_ids | list(string) | List of subnet IDs for the Lambda service. |
9296
| tags | map(string) | A map of AWS Tags to attach to each resource created | {}
9397
| role_permissions_boundary_arn | string | IAM Role Permissions Boundary ARN |
9498
| log_retention_in_days | number | CloudWatch log group retention in days. Defaults to 7. | 7
95-
| lambda_policies | list(string) | List of IAM Policy ARNs to attach to the lambda role. | []
96-
| security_groups | list(string) | List of extra security group IDs to attach to the lambda. | []
99+
| lambda_policies | list(string) | List of IAM Policy ARNs to attach to the lambda role. | []'
97100
| use_codedeploy | bool | If true, CodeDeploy App and Deployment Group will be created and TF will not update alias to point to new versions of the Lambda (becuase CodeDeploy will do that). | false
98101
| timeout | number | How long the lambda will run (in seconds) before timing out | 3 (same as terraform default)
99102
| memory_size | number | Size of the memory of the lambda. CPU will scale along with it | 128 (same as terraform default)
100103

104+
#### lambda_vpc_config
105+
106+
This variable is used when the lambda needs to be run from within a VPC.
107+
108+
* **`subnet_ids`** - List of subnet IDs for the Lambda service.
109+
* **`security_group_ids`** - List of extra security group IDs to attach to the lambda.
110+
101111
#### codedeploy_lifecycle_hooks
102112

103113
This variable is used when generating the [appspec.json](#appspec) file. This will define what Lambda Functions to invoke

examples/no-codedeploy/example.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module "acs" {
99

1010
module "lambda_api" {
1111
# source = "../../"
12-
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.0.1"
12+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.1.0"
1313
app_name = "my-lambda"
1414
env = "dev"
1515
lambda_zip_file = "./src/lambda.zip"
@@ -19,7 +19,6 @@ module "lambda_api" {
1919
https_certificate_arn = module.acs.certificate.arn
2020
vpc_id = module.acs.vpc.id
2121
public_subnet_ids = module.acs.public_subnet_ids
22-
private_subnet_ids = module.acs.private_subnet_ids
2322
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
2423
}
2524

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
provider "aws" {
2+
version = "~> 2.56"
3+
region = "us-west-2"
4+
}
5+
6+
module "acs" {
7+
source = "github.com/byu-oit/terraform-aws-acs-info?ref=v2.1.0"
8+
}
9+
10+
module "lambda_api" {
11+
# source = "../../"
12+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.1.0"
13+
app_name = "my-lambda"
14+
env = "dev"
15+
lambda_zip_file = "./src/lambda.zip"
16+
handler = "index.handler"
17+
runtime = "nodejs12.x"
18+
hosted_zone = module.acs.route53_zone
19+
https_certificate_arn = module.acs.certificate.arn
20+
vpc_id = module.acs.vpc.id
21+
public_subnet_ids = module.acs.public_subnet_ids
22+
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
23+
24+
lambda_vpc_config = {
25+
subnet_ids = module.acs.private_subnet_ids
26+
security_group_ids = []
27+
}
28+
}
29+
30+
output "url" {
31+
value = module.lambda_api.dns_record.fqdn
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
exports.handler = async function (event, context) {
2+
/*
3+
const event = {
4+
'requestContext': {
5+
'elb': {
6+
'targetGroupArn': 'arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-target-group/6d0ecf831eec9f09'
7+
}
8+
},
9+
'httpMethod': 'GET',
10+
'path': '/',
11+
'queryStringParameters': { some_query: 'blah' },
12+
'headers': {
13+
'accept': 'text/html,application/xhtml+xml',
14+
'accept-language': 'en-US,en;q=0.8',
15+
'content-type': 'text/plain',
16+
'cookie': 'cookies',
17+
'host': 'lambda-846800462-us-east-2.elb.amazonaws.com',
18+
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)',
19+
'x-amzn-trace-id': 'Root=1-5bdb40ca-556d8b0c50dc66f0511bf520',
20+
'x-forwarded-for': '72.21.198.66',
21+
'x-forwarded-port': '443',
22+
'x-forwarded-proto': 'https'
23+
},
24+
'isBase64Encoded': false,
25+
'body': 'request_body' // This is a string - If you want an object, you'll need to parse it
26+
}
27+
*/
28+
29+
console.log(event)
30+
console.log(context)
31+
32+
return {
33+
'isBase64Encoded': false,
34+
'statusCode': 200,
35+
'statusDescription': '200 OK',
36+
'headers': {
37+
'Set-cookie': 'cookies',
38+
'Content-Type': 'application/json'
39+
},
40+
'body': '{"message":"Hello, World! ... Yo!"}' // This needs to be a string - If you want to return JSON, you'll need to stringify it
41+
}
42+
}

examples/simple-lambda-in-vpc/src/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "handler",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\""
8+
},
9+
"author": "",
10+
"license": "Apache-2.0"
11+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module "acs" {
99

1010
module "lambda_api" {
1111
# source = "../../"
12-
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.0.1"
12+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v1.1.0"
1313
app_name = "my-lambda-codedeploy"
1414
env = "dev"
1515
codedeploy_service_role_arn = module.acs.power_builder_role.arn
@@ -20,7 +20,6 @@ module "lambda_api" {
2020
https_certificate_arn = module.acs.certificate.arn
2121
vpc_id = module.acs.vpc.id
2222
public_subnet_ids = module.acs.public_subnet_ids
23-
private_subnet_ids = module.acs.private_subnet_ids
2423
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
2524
codedeploy_test_listener_port = 4443
2625
use_codedeploy = true

main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ resource "aws_iam_role_policy_attachment" "lambda_policy_attach" {
224224
}
225225

226226
resource "aws_security_group" "lambda_sg" {
227+
count = var.lambda_vpc_config != null ? 1 : 0
227228
name = "${local.long_name}-lambda-sg"
228229
description = "Controls access to the Lambda"
229230
vpc_id = var.vpc_id
@@ -257,9 +258,12 @@ resource "aws_lambda_function" "api_lambda" {
257258
}
258259
}
259260

260-
vpc_config {
261-
subnet_ids = var.private_subnet_ids
262-
security_group_ids = concat([aws_security_group.lambda_sg.id], var.security_groups)
261+
dynamic "vpc_config" {
262+
for_each = var.lambda_vpc_config == null ? [] : [var.lambda_vpc_config]
263+
content {
264+
subnet_ids = var.lambda_vpc_config.subnet_ids
265+
security_group_ids = concat([aws_security_group.lambda_sg[0].id], var.lambda_vpc_config.security_group_ids)
266+
}
263267
}
264268
}
265269

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ output "lambda" {
33
}
44

55
output "lambda_security_group" {
6-
value = aws_security_group.lambda_sg
6+
value = length(aws_security_group.lambda_sg) > 0 ? aws_security_group.lambda_sg[0] : null
77
}
88

99
output "lambda_live_alias" {

variables.tf

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ variable "environment_variables" {
3535
default = null
3636
}
3737

38+
variable "lambda_vpc_config" {
39+
default = null
40+
description = "Provide this to allow your function to access your VPC."
41+
type = object({
42+
security_group_ids = list(string)
43+
subnet_ids = list(string)
44+
})
45+
}
46+
3847
variable "hosted_zone" {
3948
type = object({
4049
name = string,
@@ -71,10 +80,6 @@ variable "public_subnet_ids" {
7180
type = list(string)
7281
description = "List of subnet IDs for the ALB."
7382
}
74-
variable "private_subnet_ids" {
75-
type = list(string)
76-
description = "List of subnet IDs for the Lambda service."
77-
}
7883

7984
variable "tags" {
8085
type = map(string)
@@ -99,12 +104,6 @@ variable "lambda_policies" {
99104
default = []
100105
}
101106

102-
variable "security_groups" {
103-
type = list(string)
104-
description = "List of extra security group IDs to attach to the lambda."
105-
default = []
106-
}
107-
108107
variable "use_codedeploy" {
109108
type = bool
110109
description = "If true, CodeDeploy App and Deployment Group will be created and TF will not update alias to point to new versions of the Lambda (becuase CodeDeploy will do that)."

0 commit comments

Comments
 (0)