Skip to content

Commit 0c02a23

Browse files
authored
fix: Correct stickiness syntax and ensure that security group is not created for network load balancers (#277)
Resolves undefined
1 parent ffc7c9b commit 0c02a23

File tree

8 files changed

+110
-98
lines changed

8 files changed

+110
-98
lines changed

examples/complete-alb/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ Note that this example may create resources which cost money. Run `terraform des
5757

5858
## Inputs
5959

60-
No inputs.
60+
| Name | Description | Type | Default | Required |
61+
|------|-------------|------|---------|:--------:|
62+
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name for which the certificate should be issued | `string` | `"terraform-aws-modules.modules.tf"` | no |
6163

6264
## Outputs
6365

examples/complete-alb/main.tf

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ locals {
1111
vpc_cidr = "10.0.0.0/16"
1212
azs = slice(data.aws_availability_zones.available.names, 0, 3)
1313

14-
domain_name = "terraform-aws-modules.modules.tf"
15-
1614
tags = {
1715
Example = local.name
1816
GithubRepo = "terraform-aws-alb"
@@ -41,7 +39,7 @@ module "alb" {
4139
type = "ingress"
4240
from_port = 80
4341
to_port = 80
44-
protocol = "http"
42+
protocol = "tcp"
4543
description = "HTTP web traffic"
4644
cidr_blocks = ["0.0.0.0/0"]
4745
}
@@ -62,10 +60,10 @@ module "alb" {
6260
}
6361
}
6462

65-
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
66-
# access_logs = {
67-
# bucket = module.log_bucket.s3_bucket_id
68-
# }
63+
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
64+
# access_logs = {
65+
# bucket = module.log_bucket.s3_bucket_id
66+
# }
6967

7068
http_tcp_listeners = [
7169
# Forward action is default, either when defined or undefined
@@ -135,12 +133,12 @@ module "alb" {
135133
display = "page"
136134
prompt = "login"
137135
}
138-
authorization_endpoint = "https://${local.domain_name}/auth"
136+
authorization_endpoint = "https://${var.domain_name}/auth"
139137
client_id = "client_id"
140138
client_secret = "client_secret"
141-
issuer = "https://${local.domain_name}"
142-
token_endpoint = "https://${local.domain_name}/token"
143-
user_info_endpoint = "https://${local.domain_name}/user_info"
139+
issuer = "https://${var.domain_name}"
140+
token_endpoint = "https://${var.domain_name}/token"
141+
user_info_endpoint = "https://${var.domain_name}/user_info"
144142
}
145143
},
146144
]
@@ -189,12 +187,12 @@ module "alb" {
189187
display = "page"
190188
prompt = "login"
191189
}
192-
authorization_endpoint = "https://${local.domain_name}/auth"
190+
authorization_endpoint = "https://${var.domain_name}/auth"
193191
client_id = "client_id"
194192
client_secret = "client_secret"
195-
issuer = "https://${local.domain_name}"
196-
token_endpoint = "https://${local.domain_name}/token"
197-
user_info_endpoint = "https://${local.domain_name}/user_info"
193+
issuer = "https://${var.domain_name}"
194+
token_endpoint = "https://${var.domain_name}/token"
195+
user_info_endpoint = "https://${var.domain_name}/user_info"
198196
},
199197
{
200198
type = "forward"
@@ -460,6 +458,7 @@ data "aws_ami" "amazon_linux" {
460458
resource "aws_instance" "this" {
461459
ami = data.aws_ami.amazon_linux.id
462460
instance_type = "t3.nano"
461+
subnet_id = element(module.vpc.private_subnets, 0)
463462
}
464463

465464
#############################################
@@ -525,9 +524,9 @@ module "lambda_without_allowed_triggers" {
525524
depends_on = [null_resource.download_package]
526525
}
527526

528-
##################################################################
529-
# Data sources to get VPC and subnets
530-
##################################################################
527+
################################################################################
528+
# Supporting resources
529+
################################################################################
531530

532531
module "vpc" {
533532
source = "terraform-aws-modules/vpc/aws"
@@ -548,22 +547,22 @@ module "vpc" {
548547
}
549548

550549
data "aws_route53_zone" "this" {
551-
name = local.domain_name
550+
name = var.domain_name
552551
}
553552

554553
module "acm" {
555554
source = "terraform-aws-modules/acm/aws"
556555
version = "~> 3.0"
557556

558-
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
557+
domain_name = var.domain_name
559558
zone_id = data.aws_route53_zone.this.id
560559
}
561560

562561
module "wildcard_cert" {
563562
source = "terraform-aws-modules/acm/aws"
564563
version = "~> 3.0"
565564

566-
domain_name = "*.${local.domain_name}" # trimsuffix(data.aws_route53_zone.this.name, ".")
565+
domain_name = "*.${var.domain_name}"
567566
zone_id = data.aws_route53_zone.this.id
568567
}
569568

@@ -580,7 +579,7 @@ resource "aws_cognito_user_pool_client" "this" {
580579
user_pool_id = aws_cognito_user_pool.this.id
581580
generate_secret = true
582581
allowed_oauth_flows = ["code", "implicit"]
583-
callback_urls = ["https://${local.domain_name}/callback"]
582+
callback_urls = ["https://${var.domain_name}/callback"]
584583
allowed_oauth_scopes = ["email", "openid"]
585584
allowed_oauth_flows_user_pool_client = true
586585
}

examples/complete-alb/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "domain_name" {
2+
description = "The domain name for which the certificate should be issued"
3+
type = string
4+
default = "terraform-aws-modules.modules.tf"
5+
}

examples/complete-nlb/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,34 @@ Note that this example may create resources which cost money. Run `terraform des
2121
|------|---------|
2222
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
2323
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.27 |
24-
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2524

2625
## Providers
2726

2827
| Name | Version |
2928
|------|---------|
3029
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.27 |
31-
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
3230

3331
## Modules
3432

3533
| Name | Source | Version |
3634
|------|--------|---------|
3735
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
3836
| <a name="module_nlb"></a> [nlb](#module\_nlb) | ../../ | n/a |
37+
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
3938

4039
## Resources
4140

4241
| Name | Type |
4342
|------|------|
4443
| [aws_eip.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource |
45-
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
44+
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
4645
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
47-
| [aws_subnets.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
48-
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
4946

5047
## Inputs
5148

52-
No inputs.
49+
| Name | Description | Type | Default | Required |
50+
|------|-------------|------|---------|:--------:|
51+
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | The domain name for which the certificate should be issued | `string` | `"terraform-aws-modules.modules.tf"` | no |
5352

5453
## Outputs
5554

examples/complete-nlb/main.tf

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,47 @@
11
provider "aws" {
2-
region = "eu-west-1"
2+
region = local.region
33
}
44

5+
data "aws_availability_zones" "available" {}
6+
57
locals {
6-
domain_name = "terraform-aws-modules.modules.tf"
7-
}
8+
name = "ex-${basename(path.cwd)}"
9+
region = "eu-west-1"
810

9-
##################################################################
10-
# Data sources to get VPC and subnets
11-
##################################################################
12-
data "aws_vpc" "default" {
13-
default = true
14-
}
11+
vpc_cidr = "10.0.0.0/16"
12+
azs = slice(data.aws_availability_zones.available.names, 0, 3)
1513

16-
data "aws_subnets" "all" {
17-
filter {
18-
name = "vpc-id"
19-
values = [data.aws_vpc.default.id]
14+
tags = {
15+
Example = local.name
16+
GithubRepo = "terraform-aws-alb"
17+
GithubOrg = "terraform-aws-modules"
2018
}
2119
}
2220

23-
resource "random_pet" "this" {
24-
length = 2
25-
}
26-
27-
data "aws_route53_zone" "this" {
28-
name = local.domain_name
29-
}
30-
31-
# module "log_bucket" {
32-
# source = "terraform-aws-modules/s3-bucket/aws"
33-
# version = "~> 3.0"
34-
#
35-
# bucket = "logs-${random_pet.this.id}"
36-
# acl = "log-delivery-write"
37-
# force_destroy = true
38-
# attach_elb_log_delivery_policy = true
39-
# }
40-
41-
module "acm" {
42-
source = "terraform-aws-modules/acm/aws"
43-
version = "~> 3.0"
44-
45-
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
46-
zone_id = data.aws_route53_zone.this.id
47-
}
48-
49-
resource "aws_eip" "this" {
50-
count = length(data.aws_subnets.all.ids)
51-
52-
vpc = true
53-
}
54-
5521
##################################################################
56-
# Network Load Balancer with Elastic IPs attached
22+
# Network Load Balancer
5723
##################################################################
24+
5825
module "nlb" {
5926
source = "../../"
6027

61-
name = "complete-nlb-${random_pet.this.id}"
28+
name = local.name
6229

6330
load_balancer_type = "network"
31+
vpc_id = module.vpc.vpc_id
6432

65-
vpc_id = data.aws_vpc.default.id
33+
# Use `subnets` if you don't want to attach EIPs
34+
# subnets = module.vpc.private_subnets
6635

67-
# Use `subnets` if you don't want to attach EIPs
68-
# subnets = tolist(data.aws_subnet_ids.all.ids)
36+
# Use `subnet_mapping` to attach EIPs
37+
subnet_mapping = [for i, eip in aws_eip.this : { allocation_id : eip.id, subnet_id : module.vpc.private_subnets[i] }]
6938

70-
# Use `subnet_mapping` to attach EIPs
71-
subnet_mapping = [for i, eip in aws_eip.this : { allocation_id : eip.id, subnet_id : tolist(data.aws_subnets.all.ids)[i] }]
39+
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
40+
# access_logs = {
41+
# bucket = module.log_bucket.s3_bucket_id
42+
# }
7243

73-
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
74-
# access_logs = {
75-
# bucket = module.log_bucket.s3_bucket_id
76-
# }
77-
78-
79-
# TCP_UDP, UDP, TCP
44+
# TCP_UDP, UDP, TCP
8045
http_tcp_listeners = [
8146
{
8247
port = 81
@@ -150,4 +115,45 @@ module "nlb" {
150115
target_type = "instance"
151116
},
152117
]
118+
119+
tags = local.tags
120+
}
121+
122+
################################################################################
123+
# Supporting resources
124+
################################################################################
125+
126+
module "vpc" {
127+
source = "terraform-aws-modules/vpc/aws"
128+
version = "~> 3.0"
129+
130+
name = local.name
131+
cidr = local.vpc_cidr
132+
133+
azs = local.azs
134+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 4, k)]
135+
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 48)]
136+
137+
enable_nat_gateway = true
138+
single_nat_gateway = true
139+
enable_dns_hostnames = true
140+
141+
tags = local.tags
142+
}
143+
144+
data "aws_route53_zone" "this" {
145+
name = var.domain_name
146+
}
147+
148+
module "acm" {
149+
source = "terraform-aws-modules/acm/aws"
150+
version = "~> 3.0"
151+
152+
domain_name = var.domain_name
153+
zone_id = data.aws_route53_zone.this.id
154+
}
155+
156+
resource "aws_eip" "this" {
157+
count = length(local.azs)
158+
vpc = true
153159
}

examples/complete-nlb/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "domain_name" {
2+
description = "The domain name for which the certificate should be issued"
3+
type = string
4+
default = "terraform-aws-modules.modules.tf"
5+
}

examples/complete-nlb/versions.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@ terraform {
66
source = "hashicorp/aws"
77
version = ">= 4.27"
88
}
9-
random = {
10-
source = "hashicorp/random"
11-
version = ">= 2.0"
12-
}
139
}
1410
}

main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource "aws_lb" "this" {
1010

1111
load_balancer_type = var.load_balancer_type
1212
internal = var.internal
13-
security_groups = var.create_security_group ? concat([aws_security_group.this[0].id], var.security_groups) : var.security_groups
13+
security_groups = var.create_security_group && var.load_balancer_type == "application" ? concat([aws_security_group.this[0].id], var.security_groups) : var.security_groups
1414
subnets = var.subnets
1515

1616
idle_timeout = var.idle_timeout
@@ -100,10 +100,10 @@ resource "aws_lb_target_group" "main" {
100100
for_each = try([var.target_groups[count.index].stickiness], [])
101101

102102
content {
103-
enabled = lookup(stickiness.value.enabled, null)
104-
cookie_duration = lookup(stickiness.value.cookie_duration, null)
105-
type = lookup(stickiness.value.type, null)
106-
cookie_name = lookup(stickiness.value.cookie_name, null)
103+
enabled = try(stickiness.value.enabled, null)
104+
cookie_duration = try(stickiness.value.cookie_duration, null)
105+
type = try(stickiness.value.type, null)
106+
cookie_name = try(stickiness.value.cookie_name, null)
107107
}
108108
}
109109

@@ -770,7 +770,7 @@ resource "aws_lb_listener_certificate" "https_listener" {
770770
################################################################################
771771

772772
locals {
773-
create_security_group = local.create_lb && var.create_security_group
773+
create_security_group = local.create_lb && var.create_security_group && var.load_balancer_type == "application"
774774
security_group_name = try(coalesce(var.security_group_name, var.name, var.name_prefix), "")
775775
}
776776

0 commit comments

Comments
 (0)