Skip to content

Commit 81d25b9

Browse files
authored
Merge pull request #3 from byu-oit/no-codedeploy
allow module to be used without codedeploy
2 parents 31c45f6 + 184304b commit 81d25b9

File tree

10 files changed

+122
-15
lines changed

10 files changed

+122
-15
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This is done by:
1818
## Usage
1919
```hcl
2020
module "lambda_api" {
21-
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v0.0.2"
21+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v0.1.0"
2222
app_name = "my-lambda"
2323
env = "dev"
2424
codedeploy_service_role_arn = module.acs.power_builder_role.arn
@@ -85,6 +85,7 @@ module "lambda_api" {
8585
| log_retention_in_days | number | CloudWatch log group retention in days. Defaults to 7. | 7
8686
| lambda_policies | list(string) | List of IAM Policy ARNs to attach to the lambda role. | []
8787
| security_groups | list(string) | List of extra security group IDs to attach to the lambda. | []
88+
| 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
8889

8990
#### codedeploy_lifecycle_hooks
9091

examples/no-codedeploy/example.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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=v0.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+
private_subnet_ids = module.acs.private_subnet_ids
23+
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
24+
}
25+
26+
output "url" {
27+
value = module.lambda_api.dns_record.fqdn
28+
}

examples/no-codedeploy/src/index.js

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/no-codedeploy/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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ module "acs" {
88
}
99

1010
module "lambda_api" {
11-
// source = "../../"
12-
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v0.0.2"
13-
app_name = "my-lambda"
11+
# source = "../../"
12+
source = "github.com/byu-oit/terraform-aws-lambda-api?ref=v0.1.0"
13+
app_name = "my-lambda-codedeploy"
1414
env = "dev"
1515
codedeploy_service_role_arn = module.acs.power_builder_role.arn
1616
lambda_zip_file = "./src/lambda.zip"
@@ -23,6 +23,7 @@ module "lambda_api" {
2323
private_subnet_ids = module.acs.private_subnet_ids
2424
role_permissions_boundary_arn = module.acs.role_permissions_boundary.arn
2525
codedeploy_test_listener_port = 4443
26+
use_codedeploy = true
2627

2728
codedeploy_lifecycle_hooks = {
2829
BeforeAllowTraffic = aws_lambda_function.test_lambda.function_name

examples/simple-lambda-with-deploy-test/tst/my-lambda.postman_collection.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
"raw": ""
3030
},
3131
"url": {
32-
"raw": "https://my-lambda.byu-oit-terraform-dev.amazon.byu.edu:4443/",
32+
"raw": "https://my-lambda-codedeploy-dev.byu-oit-terraform-dev.amazon.byu.edu:4443/",
3333
"protocol": "https",
3434
"host": [
35-
"my-lambda",
35+
"my-lambda-codedeploy-dev",
3636
"byu-oit-terraform-dev",
3737
"amazon",
3838
"byu",

main.tf

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ resource "aws_lambda_permission" "with_lb" {
140140
function_name = aws_lambda_function.api_lambda.arn
141141
principal = "elasticloadbalancing.amazonaws.com"
142142
source_arn = aws_alb_target_group.tg.arn
143-
qualifier = aws_lambda_alias.live.name
143+
qualifier = var.use_codedeploy ? aws_lambda_alias.live_codedeploy[0].name : aws_lambda_alias.live[0].name
144144
}
145145

146146
resource "aws_lambda_permission" "with_tst_lb" {
@@ -153,7 +153,7 @@ resource "aws_lambda_permission" "with_tst_lb" {
153153

154154
resource "aws_alb_target_group_attachment" "live_attachment" {
155155
target_group_arn = aws_alb_target_group.tg.arn
156-
target_id = aws_lambda_alias.live.arn
156+
target_id = var.use_codedeploy ? aws_lambda_alias.live_codedeploy[0].arn : aws_lambda_alias.live[0].arn
157157
depends_on = [aws_lambda_permission.with_lb]
158158
}
159159

@@ -189,7 +189,7 @@ resource "aws_route53_record" "aaaa_record" {
189189
# ==================== Lambda ====================
190190

191191
resource "aws_iam_role" "iam_for_lambda" {
192-
name = "iam_for_lambda"
192+
name = "${local.long_name}-role"
193193
permissions_boundary = var.role_permissions_boundary_arn
194194
assume_role_policy = <<EOF
195195
{
@@ -259,6 +259,15 @@ resource "aws_lambda_function" "api_lambda" {
259259
}
260260

261261
resource "aws_lambda_alias" "live" {
262+
count = !var.use_codedeploy ? 1 : 0
263+
name = "live"
264+
description = "ALB sends traffic to this version"
265+
function_name = aws_lambda_function.api_lambda.arn
266+
function_version = aws_lambda_function.api_lambda.version
267+
}
268+
269+
resource "aws_lambda_alias" "live_codedeploy" {
270+
count = var.use_codedeploy ? 1 : 0
262271
name = "live"
263272
description = "ALB sends traffic to this version"
264273
function_name = aws_lambda_function.api_lambda.arn
@@ -275,12 +284,14 @@ resource "aws_lambda_alias" "live" {
275284
# ==================== CodeDeploy ====================
276285

277286
resource "aws_codedeploy_app" "app" {
287+
count = var.use_codedeploy ? 1 : 0
278288
compute_platform = "Lambda"
279289
name = "${local.long_name}-cd"
280290
}
281291

282292
resource "aws_codedeploy_deployment_group" "deployment_group" {
283-
app_name = aws_codedeploy_app.app.name
293+
count = var.use_codedeploy ? 1 : 0
294+
app_name = aws_codedeploy_app.app[0].name
284295
deployment_group_name = "${local.long_name}-dg"
285296
service_role_arn = var.codedeploy_service_role_arn
286297
deployment_config_name = "CodeDeployDefault.LambdaAllAtOnce"
@@ -311,6 +322,7 @@ resource "aws_iam_role_policy_attachment" "lambda_cloudwatch_attach" {
311322
# ==================== AppSpec file ====================
312323

313324
resource "local_file" "appspec_json" {
325+
count = var.use_codedeploy ? 1 : 0
314326
filename = "${path.cwd}/appspec.json"
315327
content = jsonencode({
316328
version = 1
@@ -319,8 +331,8 @@ resource "local_file" "appspec_json" {
319331
Type = "AWS::Lambda::Function"
320332
Properties = {
321333
Name = aws_lambda_function.api_lambda.function_name
322-
Alias = aws_lambda_alias.live.name
323-
# CurrentVersion = local.is_initial ? aws_lambda_alias.initial.function_version : data.aws_lambda_alias.alias_for_old_version[0].function_version
334+
Alias = aws_lambda_alias.live_codedeploy[0].name
335+
CurrentVersion = aws_lambda_function.api_lambda.version # TODO: figure out how to get previous version for rollback
324336
TargetVersion = aws_lambda_function.api_lambda.version
325337
}
326338
}

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ output "lambda_security_group" {
77
}
88

99
output "lambda_live_alias" {
10-
value = aws_lambda_alias.live
10+
value = var.use_codedeploy ? aws_lambda_alias.live_codedeploy[0] : aws_lambda_alias.live[0]
1111
}
1212

1313
output "codedeploy_deployment_group" {
14-
value = aws_codedeploy_deployment_group.deployment_group
14+
value = var.use_codedeploy ? aws_codedeploy_deployment_group.deployment_group[0] : null
1515
}
1616

1717
output "codedeploy_appspec_json_file" {
18-
value = local_file.appspec_json.filename
18+
value = var.use_codedeploy ? local_file.appspec_json[0].filename : null
1919
}
2020

2121
output "alb" {

variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ variable "env" {
1111
variable "codedeploy_service_role_arn" {
1212
type = string
1313
description = "ARN of the IAM Role for the CodeDeploy to use to initiate new deployments. (usually the PowerBuilder Role)"
14+
default = null
1415
}
1516

1617
variable "lambda_zip_file" {
@@ -103,3 +104,9 @@ variable "security_groups" {
103104
description = "List of extra security group IDs to attach to the lambda."
104105
default = []
105106
}
107+
108+
variable "use_codedeploy" {
109+
type = bool
110+
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)."
111+
default = false
112+
}

0 commit comments

Comments
 (0)