Skip to content

Commit af83c3a

Browse files
allow optional extra ssl certs (#54)
* added the option for extra SSL certs * simplification of a variable name * upgraded awspec, terraform, and kitchen terraform
1 parent 9bcc0f2 commit af83c3a

File tree

9 files changed

+130
-66
lines changed

9 files changed

+130
-66
lines changed

.travis.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ dist: trusty
44
rvm:
55
- 2.4.2
66

7-
services:
8-
- docker
9-
107
before_install:
118
- echo "before_install"
129

@@ -20,18 +17,20 @@ before_script:
2017
# Get a random region from the awscli and use it through the remainder of the test cycle.
2118
- export AWS_REGION=$(docker run --env AWS_DEFAULT_REGION=us-east-2 --env AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} --env AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} garland/aws-cli-docker aws ec2 describe-regions --query 'Regions[].{Name:RegionName}' --output text | shuf | head -n1)
2219
- export TF_VAR_region=${AWS_REGION}
20+
- export TF_WARN_OUTPUT_ERRORS=1
2321
- echo "using AWS_REGION=${AWS_REGION}"
24-
- curl --silent --output terraform.zip https://releases.hashicorp.com/terraform/0.11.3/terraform_0.11.3_linux_amd64.zip
25-
- sha256sum terraform.zip | grep "6b8a7b83954597d36bbed23913dd51bc253906c612a070a21db373eab71b277b" -q
22+
- curl --silent --output terraform.zip https://releases.hashicorp.com/terraform/0.11.5/terraform_0.11.5_linux_amd64.zip
23+
- sha256sum terraform.zip | grep "131c440263382c79c7f783b70ff35cd1d03eb31c44f7738d153d95a0b8436ac9" -q
2624
- unzip terraform.zip ; rm -f terraform.zip; chmod +x terraform
2725
- mkdir -p ${HOME}/bin ; export PATH=${PATH}:${HOME}/bin; mv terraform ${HOME}/bin/
2826
- terraform -v
2927

3028
script:
3129
- echo 'script'
30+
-
3231
- terraform init
3332
- terraform fmt -check=true
34-
- terraform validate -var "region=${AWS_REGION}" -var "subnets=[]" -var "vpc_id=vpc-abcde012" -var "load_balancer_name=my-lb" -var "log_bucket_name=my-log-bucket" -var "load_balancer_security_groups=[]"
33+
- terraform validate -var "region=${AWS_REGION}" -var "subnets=[]" -var "vpc_id=vpc-abcde012" -var "load_balancer_name=my-lb" -var "log_bucket_name=my-log-bucket" -var "security_groups=[]"
3534
- docker run --rm -v $(pwd):/app/ --workdir=/app/ -t wata727/tflint --error-with-issues
3635
- cd examples/alb_test_fixture
3736
- terraform init

CHANGELOG.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,32 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this
66
project adheres to [Semantic Versioning](http://semver.org/).
77

8-
## [v3.0.0] - 2018-03-
8+
## [v3.1.0] - 2018-03-22
9+
10+
### Added
11+
12+
* extra certs can now be applied to HTTPS listeners via the `extra_ssl_certs` list variable and corresponding `extra_ssl_certs_count`.
13+
14+
### Changed
15+
16+
* `load_balancer_security_groups` moved to simpler `security_groups`.
17+
* `name_prefix` changed back to `name` as the inflexibility of a 6 character prefix is overly restricting. Name conflicts must be dealt with by the developer. (cheers, @michaelmoussa 🎉)
18+
* upgraded terraform and kitchen terraform. terraform 0.11.4 and above errors out on warnings but has been mitigated with `TF_WARN_OUTPUT_ERRORS=1` per [this issue](https://github.com/hashicorp/terraform/issues/17655).
19+
20+
## [v3.0.0] - 2018-03-20
921

1022
### Added
1123

1224
* default values added for most target group and listener attributes.
1325
* new application load balancer attributes added as variables with defaults.
14-
* tests now covering listeners.
26+
* tests now cover listeners.
1527

1628
### Changed
1729

1830
* listeners and target groups now defined by lists of maps allowing many-to-many relationships of those resources.
1931
* listeners and target groups creation is now data driven through variables giving greater flexibility.
2032
* `name_prefix` used where possible to avoid naming conflicts in resource testing.
21-
* logging to S3 now made manditory and done outside the module as this is better practice.
33+
* logging to S3 now made manditory and done outside the module as this is better practice. (thanks, @alexjurkiewicz 🥂)
2234
* terraform 0.11.3 now used in CI. 0.11.4 seems to have warnings on plan that become errors in CI.
2335

2436
## [v2.5.0] - 2018-03-07

Gemfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
ruby '2.4.2'
44

55
source 'https://rubygems.org/' do
6-
gem 'awspec', '~> 1.0.0'
7-
gem 'kitchen-terraform', '~> 3.1'
6+
gem 'awspec', '~> 1.4.2'
7+
gem 'kitchen-terraform', '~> 3.2'
88
gem 'kitchen-verifier-awspec', '~> 0.1.1'
9-
gem 'rhcl', '~> 0.1.0'
109
end
Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,83 @@
11
locals {
2-
tags = "${map("Environment", "test", "GithubRepo", "tf-aws-alb", "GithubOrg", "terraform-aws-modules", "Workspace", "${terraform.workspace}")}"
3-
log_bucket_name = "${var.log_bucket_name}-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}"
2+
tags = "${map("Environment", "test",
3+
"GithubRepo", "tf-aws-alb",
4+
"GithubOrg", "terraform-aws-modules",
5+
"Workspace", "${terraform.workspace}",
6+
)}"
7+
8+
log_bucket_name = "${var.log_bucket_name}-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}"
9+
410
https_listeners_count = 2
511

612
https_listeners = "${list(
7-
map(
8-
"certificate_arn", aws_iam_server_certificate.fixture_cert.arn,
9-
"port", 443
10-
),
11-
map(
12-
"certificate_arn", aws_iam_server_certificate.fixture_cert.arn,
13-
"port", 8443,
14-
"ssl_policy", "ELBSecurityPolicy-TLS-1-2-2017-01",
15-
"target_group_index", 1
16-
)
13+
map(
14+
"certificate_arn", aws_iam_server_certificate.fixture_cert.0.arn,
15+
"port", 443,
16+
),
17+
map(
18+
"certificate_arn", aws_iam_server_certificate.fixture_cert.1.arn,
19+
"port", 8443,
20+
"ssl_policy", "ELBSecurityPolicy-TLS-1-2-2017-01",
21+
"target_group_index", 1,
22+
),
1723
)}"
1824

1925
http_tcp_listeners_count = 3
2026

2127
http_tcp_listeners = "${list(
2228
map(
2329
"port", 80,
24-
"protocol", "HTTP"
30+
"protocol", "HTTP",
2531
),
2632
map(
2733
"port", 8080,
2834
"protocol", "HTTP",
29-
"target_group_index", 0
35+
"target_group_index", 0,
3036
),
3137
map(
3238
"port", 8081,
3339
"protocol", "HTTP",
34-
"target_group_index", 1
35-
)
36-
)}"
40+
"target_group_index", 1,
41+
),
42+
)}"
3743

3844
target_groups_count = 2
3945

4046
target_groups = "${list(
4147
map("name", "foo",
4248
"backend_protocol", "HTTP",
43-
"backend_port", 80
49+
"backend_port", 80,
4450
),
4551
map("name", "bar",
4652
"backend_protocol", "HTTP",
47-
"backend_port", 8080
48-
)
53+
"backend_port", 8080,
54+
),
55+
)}"
56+
57+
extra_ssl_certs_count = 4
58+
59+
extra_ssl_certs = "${list(
60+
map("certificate_arn", aws_iam_server_certificate.fixture_cert.0.arn,
61+
"https_listener_index","1",
62+
),
63+
map("certificate_arn", aws_iam_server_certificate.fixture_cert.1.arn,
64+
"https_listener_index","0",
65+
),
66+
map("certificate_arn", aws_iam_server_certificate.fixture_cert.2.arn,
67+
"https_listener_index","0",
68+
),
69+
map("certificate_arn", aws_iam_server_certificate.fixture_cert.3.arn,
70+
"https_listener_index","0",
71+
),
4972
)}"
73+
74+
# helpful for debugging
75+
# https_listeners_count = 0
76+
# https_listeners = "${list()}"
77+
# http_tcp_listeners_count = 0
78+
# http_tcp_listeners = "${list()}"
79+
# target_groups_count = 0
80+
# target_groups = "${list()}"
81+
# extra_ssl_certs_count = 0
82+
# extra_ssl_certs = "${list()}"
5083
}

examples/alb_test_fixture/main.tf

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
terraform {
2-
required_version = ">= 0.11.3"
2+
required_version = "= 0.11.5"
33
}
44

55
provider "aws" {
66
version = ">= 1.10.0"
77
region = "${var.region}"
88
}
99

10-
resource "random_id" "alb_name_suffix" {
11-
byte_length = 16
10+
provider "random" {
11+
version = "= 1.1.0"
12+
}
13+
14+
resource "random_string" "suffix" {
15+
length = 8
16+
special = false
1217
}
1318

1419
resource "aws_iam_server_certificate" "fixture_cert" {
15-
name = "test_cert-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}"
20+
name_prefix = "test_cert-${data.aws_caller_identity.current.account_id}-${data.aws_region.current.name}"
1621
certificate_body = "${file("${path.module}/../../examples/alb_test_fixture/certs/example.crt.pem")}"
1722
private_key = "${file("${path.module}/../../examples/alb_test_fixture/certs/example.key.pem")}"
1823

1924
lifecycle {
2025
create_before_destroy = true
2126
}
27+
28+
count = 4
2229
}
2330

2431
resource "aws_s3_bucket" "log_bucket" {
@@ -59,18 +66,20 @@ module "security_group" {
5966
}
6067

6168
module "alb" {
62-
source = "../.."
63-
load_balancer_name = "test-alb-${random_id.alb_name_suffix.hex}"
64-
load_balancer_security_groups = ["${module.security_group.this_security_group_id}"]
65-
log_bucket_name = "${aws_s3_bucket.log_bucket.id}"
66-
log_location_prefix = "${var.log_location_prefix}"
67-
subnets = "${module.vpc.public_subnets}"
68-
tags = "${local.tags}"
69-
vpc_id = "${module.vpc.vpc_id}"
70-
https_listeners = "${local.https_listeners}"
71-
https_listeners_count = "${local.https_listeners_count}"
72-
http_tcp_listeners = "${local.http_tcp_listeners}"
73-
http_tcp_listeners_count = "${local.http_tcp_listeners_count}"
74-
target_groups = "${local.target_groups}"
75-
target_groups_count = "${local.target_groups_count}"
69+
source = "../.."
70+
load_balancer_name = "test-alb-${random_string.suffix.result}"
71+
security_groups = ["${module.security_group.this_security_group_id}"]
72+
log_bucket_name = "${aws_s3_bucket.log_bucket.id}"
73+
log_location_prefix = "${var.log_location_prefix}"
74+
subnets = "${module.vpc.public_subnets}"
75+
tags = "${local.tags}"
76+
vpc_id = "${module.vpc.vpc_id}"
77+
https_listeners = "${local.https_listeners}"
78+
https_listeners_count = "${local.https_listeners_count}"
79+
http_tcp_listeners = "${local.http_tcp_listeners}"
80+
http_tcp_listeners_count = "${local.http_tcp_listeners_count}"
81+
target_groups = "${local.target_groups}"
82+
target_groups_count = "${local.target_groups_count}"
83+
extra_ssl_certs = "${local.extra_ssl_certs}"
84+
extra_ssl_certs_count = "${local.extra_ssl_certs_count}"
7685
}

main.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ resource "aws_lb" "application" {
22
load_balancer_type = "application"
33
name = "${var.load_balancer_name}"
44
internal = "${var.load_balancer_is_internal}"
5-
security_groups = ["${var.load_balancer_security_groups}"]
5+
security_groups = ["${var.security_groups}"]
66
subnets = ["${var.subnets}"]
77
idle_timeout = "${var.idle_timeout}"
88
enable_deletion_protection = "${var.enable_deletion_protection}"
@@ -79,3 +79,9 @@ resource "aws_lb_listener" "frontend_https" {
7979
type = "forward"
8080
}
8181
}
82+
83+
resource "aws_lb_listener_certificate" "https_listener" {
84+
listener_arn = "${aws_lb_listener.frontend_https.*.arn[lookup(var.extra_ssl_certs[count.index], "https_listener_index")]}"
85+
certificate_arn = "${lookup(var.extra_ssl_certs[count.index], "certificate_arn")}"
86+
count = "${var.extra_ssl_certs_count}"
87+
}

test/integration/default/test_alb.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'awspec'
4-
require 'rhcl'
5-
6-
module_vars = Rhcl.parse(File.open('examples/alb_test_fixture/variables.tf'))
7-
log_location_prefix = module_vars['variable']['log_location_prefix']['default']
84

95
# rubocop:disable LineLength
106
state_file = 'terraform.tfstate.d/kitchen-terraform-default-aws/terraform.tfstate'
@@ -15,7 +11,6 @@
1511
# rubocop:enable LineLength
1612
alb_arn = tf_state['modules'][0]['outputs']['alb_id']['value']
1713
alb_name = alb_arn.split('/')[-2]
18-
account_id = tf_state['modules'][0]['outputs']['account_id']['value']
1914
region = tf_state['modules'][0]['outputs']['region']['value']
2015
ENV['AWS_REGION'] = region
2116
vpc_id = tf_state['modules'][0]['outputs']['vpc_id']['value']

variables.tf

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ variable "enable_http2" {
88
default = true
99
}
1010

11+
variable "extra_ssl_certs" {
12+
description = "A list of maps describing any extra SSL certificates to apply to the HTTPS listeners. Required key/values: certificate_arn, https_listener_index (the index of the listener within https_listeners which the cert applies toward)."
13+
type = "list"
14+
default = []
15+
}
16+
17+
variable "extra_ssl_certs_count" {
18+
description = "A manually provided count/length of the extra_ssl_certs list of maps since the list cannot be computed."
19+
default = 0
20+
}
21+
1122
variable "https_listeners" {
12-
description = "A list of maps describing the HTTPS listeners for this ALB. Required keys: port, certificate_arn. Optional keys: ssl_policy (defaults to ELBSecurityPolicy-2016-08), target_group_index (defaults to 0)"
23+
description = "A list of maps describing the HTTPS listeners for this ALB. Required key/values: port, certificate_arn. Optional key/values: ssl_policy (defaults to ELBSecurityPolicy-2016-08), target_group_index (defaults to 0)"
1324
type = "list"
1425
default = []
1526
}
@@ -20,7 +31,7 @@ variable "https_listeners_count" {
2031
}
2132

2233
variable "http_tcp_listeners" {
23-
description = "A list of maps describing the HTTPS listeners for this ALB. Required keys: port, protocol. Optional keys: target_group_index (defaults to 0)"
34+
description = "A list of maps describing the HTTPS listeners for this ALB. Required key/values: port, protocol. Optional key/values: target_group_index (defaults to 0)"
2435
type = "list"
2536
default = []
2637
}
@@ -61,19 +72,14 @@ variable "load_balancer_delete_timeout" {
6172
}
6273

6374
variable "load_balancer_name" {
64-
description = "The name prefix and name tag of the load balancer."
75+
description = "The resource name and Name tag of the load balancer."
6576
}
6677

6778
variable "load_balancer_update_timeout" {
6879
description = "Timeout value when updating the ALB."
6980
default = "10m"
7081
}
7182

72-
variable "load_balancer_security_groups" {
73-
description = "The security groups to attach to the load balancer. e.g. [\"sg-edcd9784\",\"sg-edcd9785\"]"
74-
type = "list"
75-
}
76-
7783
variable "log_bucket_name" {
7884
description = "S3 bucket (externally created) for storing load balancer access logs."
7985
}
@@ -93,8 +99,13 @@ variable "tags" {
9399
default = {}
94100
}
95101

102+
variable "security_groups" {
103+
description = "The security groups to attach to the load balancer. e.g. [\"sg-edcd9784\",\"sg-edcd9785\"]"
104+
type = "list"
105+
}
106+
96107
variable "target_groups" {
97-
description = "A list of maps containing key/value pairs that define the target groups to be created. Order of these maps is important and the index of these are to be referenced in listener definitions. Required map values: name, backend_protocol, backend_port. Optional key/values found in the target_groups_defaults variable."
108+
description = "A list of maps containing key/value pairs that define the target groups to be created. Order of these maps is important and the index of these are to be referenced in listener definitions. Required key/values: name, backend_protocol, backend_port. Optional key/values are in the target_groups_defaults variable."
98109
type = "list"
99110
default = []
100111
}

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v3.0.0
1+
v3.1.0

0 commit comments

Comments
 (0)