Skip to content

Commit 51c87b9

Browse files
committed
refactor example. refactor vars and outputs. add env. add policies. add subnets. add security groups.
1 parent 79f7933 commit 51c87b9

File tree

11 files changed

+128
-98
lines changed

11 files changed

+128
-98
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ module "acs" {
88
}
99

1010
module "lambda_api" {
11-
source = "../"
11+
source = "../../"
1212
app_name = "my-lambda"
13+
env = "dev"
1314
codedeploy_service_role_arn = module.acs.power_builder_role.arn
14-
lambda_src_dir = "./my-lambda"
15+
lambda_src_dir = "./src"
1516
hosted_zone = module.acs.route53_zone
1617
https_certificate_arn = module.acs.certificate.arn
1718
vpc_id = module.acs.vpc.id
@@ -88,3 +89,7 @@ resource "aws_iam_role_policy" "test_lambda" {
8889
}
8990
EOF
9091
}
92+
93+
output "url" {
94+
value = module.lambda_api.dns_record.fqdn
95+
}

main.tf

Lines changed: 19 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,12 @@ terraform {
55
}
66
}
77

8-
data "aws_caller_identity" "current" {}
9-
data "aws_region" "current" {}
10-
11-
# ==================== Variables ====================
12-
13-
variable "app_name" {
14-
type = string
15-
description = "application name"
16-
}
17-
18-
variable "codedeploy_service_role_arn" {
19-
type = string
20-
description = "ARN of the IAM Role for the CodeDeploy to use to initiate new deployments. (usually the PowerBuilder Role)"
21-
}
22-
23-
variable "codedeploy_termination_wait_time" {
24-
type = number
25-
description = "The number of minutes to wait after a successful blue/green deployment before terminating instances from the original environment. Defaults to 15"
26-
default = 15
27-
}
28-
29-
variable "lambda_src_dir" {
30-
type = string
31-
description = "Directory that contains your lambda source code"
32-
}
33-
34-
variable "hosted_zone" {
35-
type = object({
36-
name = string,
37-
id = string
38-
})
39-
description = "Hosted Zone object to redirect to ALB. (Can pass in the aws_hosted_zone object). A and AAAA records created in this hosted zone."
40-
}
41-
42-
variable "https_certificate_arn" {
43-
type = string
44-
description = "ARN of the HTTPS certificate of the hosted zone/domain."
45-
}
46-
47-
variable "codedeploy_lifecycle_hooks" {
48-
type = object({
49-
BeforeAllowTraffic = string
50-
AfterAllowTraffic = string
51-
})
52-
description = "Define Lambda Functions for CodeDeploy lifecycle event hooks. Or set this variable to null to not have any lifecycle hooks invoked. Defaults to null"
53-
default = null
54-
}
55-
56-
variable "vpc_id" {
57-
type = string
58-
description = "VPC ID to deploy ECS fargate service."
59-
}
60-
variable "public_subnet_ids" {
61-
type = list(string)
62-
description = "List of subnet IDs for the ALB."
63-
}
64-
variable "private_subnet_ids" {
65-
type = list(string)
66-
description = "List of subnet IDs for the fargate service."
67-
}
68-
69-
variable "tags" {
70-
type = map(string)
71-
description = "A map of AWS Tags to attach to each resource created"
72-
default = {}
73-
}
74-
75-
variable "role_permissions_boundary_arn" {
76-
type = string
77-
description = "IAM Role Permissions Boundary ARN"
78-
}
79-
80-
variable "log_retention_in_days" {
81-
type = number
82-
description = "CloudWatch log group retention in days. Defaults to 7."
83-
default = 7
84-
}
85-
86-
//TODO: Add policies variable for additional policies to attach to the lambda role
87-
88-
# ==================== Outputs ====================
89-
90-
# output "appspec" {
91-
# value = local_file.appspec_json.content
92-
# }
93-
94-
output "dns_record" {
95-
value = aws_route53_record.a_record
96-
}
978
# ==================== Locals ====================
989

9910
locals {
100-
alb_name = "${var.app_name}-alb" // ALB name has a restriction of 32 characters max
101-
app_domain_url = "${var.app_name}.${var.hosted_zone.name}" // Route53 A record name
11+
long_name = "${var.app_name}-${var.env}"
12+
alb_name = "${local.long_name}-alb" // ALB name has a restriction of 32 characters max
13+
app_domain_url = "${local.long_name}.${var.hosted_zone.name}" // Route53 A record name
10214

10315
hooks = var.codedeploy_lifecycle_hooks != null ? setsubtract([
10416
for hook in keys(var.codedeploy_lifecycle_hooks) :
@@ -161,14 +73,14 @@ resource "aws_security_group" "alb-sg" {
16173
}
16274

16375
resource "aws_alb_target_group" "tg" {
164-
name = "${var.app_name}-tg"
76+
name = "${local.long_name}-tg"
16577
target_type = "lambda"
16678
tags = var.tags
16779
depends_on = [aws_alb.alb]
16880
}
16981

17082
resource "aws_alb_target_group" "tst_tg" {
171-
name = "${var.app_name}-tst"
83+
name = "${local.long_name}-tst"
17284
target_type = "lambda"
17385
tags = var.tags
17486
depends_on = [aws_alb.alb]
@@ -296,6 +208,12 @@ resource "aws_iam_role" "iam_for_lambda" {
296208
EOF
297209
}
298210

211+
resource "aws_iam_role_policy_attachment" "lambda_policy_attach" {
212+
count = length(var.lambda_policies)
213+
policy_arn = element(var.lambda_policies, count.index)
214+
role = aws_iam_role.iam_for_lambda.name
215+
}
216+
299217
data "archive_file" "cleanup_lambda_zip" {
300218
source_dir = var.lambda_src_dir
301219
output_path = "lambda_function_payload.zip"
@@ -311,6 +229,11 @@ resource "aws_lambda_function" "api_lambda" {
311229
runtime = "nodejs12.x"
312230
publish = true
313231

232+
vpc_config = {
233+
subnet_ids = var.private_subnet_ids
234+
security_group_ids = var.lambda_security_groups
235+
}
236+
314237
# environment {
315238
# variables = {
316239
# netid = "jvisker"
@@ -350,11 +273,11 @@ resource "aws_lambda_alias" "live" {
350273

351274
resource "aws_codedeploy_app" "app" {
352275
compute_platform = "Lambda"
353-
name = "${var.app_name}-cd"
276+
name = "${local.long_name}-cd"
354277
}
355278

356279
# resource "aws_codedeploy_deployment_config" "config" {
357-
# deployment_config_name = "${var.app_name}-cfg"
280+
# deployment_config_name = "${local.long_name}-cfg"
358281
# compute_platform = "Lambda"
359282

360283
# //TODO: There are other ways to configure this
@@ -370,7 +293,7 @@ resource "aws_codedeploy_app" "app" {
370293

371294
resource "aws_codedeploy_deployment_group" "deployment_group" {
372295
app_name = aws_codedeploy_app.app.name
373-
deployment_group_name = "${var.app_name}-dg"
296+
deployment_group_name = "${local.long_name}-dg"
374297
service_role_arn = var.codedeploy_service_role_arn
375298
deployment_config_name = "CodeDeployDefault.LambdaAllAtOnce"
376299
deployment_style {

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# output "appspec" {
2+
# value = local_file.appspec_json.content
3+
# }
4+
5+
output "dns_record" {
6+
value = aws_route53_record.a_record
7+
}

0 commit comments

Comments
 (0)