Skip to content

Commit 8b3a738

Browse files
colincolemanKristian
authored andcommitted
Update to terraform 0.12.x (#18)
* Updated to terraform 0.12.x * remove random extra line (#1) * remove random extra line (#2) * Remove unnecessary quotes from example (#3)
1 parent eb1a320 commit 8b3a738

File tree

9 files changed

+155
-97
lines changed

9 files changed

+155
-97
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ dist: trusty
22
sudo: false
33

44
before_install:
5-
- curl -fSL "https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip" -o terraform.zip
5+
- curl -fSL "https://releases.hashicorp.com/terraform/0.12.6/terraform_0.12.6_linux_amd64.zip" -o terraform.zip
66
- sudo unzip terraform.zip -d /opt/terraform
77
- sudo ln -s /opt/terraform/terraform /usr/bin/terraform
88
- rm -f terraform.zip

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ default: test
55

66
test:
77
@echo "== Test =="
8-
@if ! terraform fmt -write=false -check=true >> /dev/null; then \
8+
@if ! terraform fmt -recursive -write=false -check=true >> /dev/null; then \
99
echo "✗ terraform fmt (Some files need to be formatted, run 'terraform fmt' to fix.)"; \
1010
exit 1; \
1111
else \

examples/default/example.tf

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,51 @@ data "aws_vpc" "main" {
1515
}
1616

1717
data "aws_subnet_ids" "main" {
18-
vpc_id = "${data.aws_vpc.main.id}"
18+
vpc_id = data.aws_vpc.main.id
1919
}
2020

2121
module "fargate_alb" {
2222
source = "telia-oss/loadbalancer/aws"
23-
version = "0.1.0"
23+
version = "3.0.0"
2424

2525
name_prefix = "example-ecs-cluster"
2626
type = "application"
2727
internal = "false"
28-
vpc_id = "${data.aws_vpc.main.id}"
29-
subnet_ids = ["${data.aws_subnet_ids.main.ids}"]
28+
vpc_id = data.aws_vpc.main.id
29+
subnet_ids = data.aws_subnet_ids.main.ids
3030

31-
tags {
31+
tags = {
3232
environment = "test"
3333
terraform = "true"
3434
}
3535
}
3636

3737
resource "aws_lb_listener" "alb" {
38-
load_balancer_arn = "${module.fargate_alb.arn}"
39-
port = "80"
38+
load_balancer_arn = module.fargate_alb.arn
39+
port = 80
4040
protocol = "HTTP"
4141

4242
default_action {
43-
target_group_arn = "${module.fargate.target_group_arn}"
43+
target_group_arn = module.fargate.target_group_arn
4444
type = "forward"
4545
}
4646
}
4747

4848
resource "aws_security_group_rule" "task_ingress_8000" {
49-
security_group_id = "${module.fargate.service_sg_id}"
49+
security_group_id = module.fargate.service_sg_id
5050
type = "ingress"
5151
protocol = "tcp"
52-
from_port = "8000"
53-
to_port = "8000"
54-
source_security_group_id = "${module.fargate_alb.security_group_id}"
52+
from_port = 8000
53+
to_port = 8000
54+
source_security_group_id = module.fargate_alb.security_group_id
5555
}
5656

5757
resource "aws_security_group_rule" "alb_ingress_80" {
58-
security_group_id = "${module.fargate_alb.security_group_id}"
58+
security_group_id = module.fargate_alb.security_group_id
5959
type = "ingress"
6060
protocol = "tcp"
61-
from_port = "80"
62-
to_port = "80"
61+
from_port = 80
62+
to_port = 80
6363
cidr_blocks = ["0.0.0.0/0"]
6464
ipv6_cidr_blocks = ["::/0"]
6565
}
@@ -68,26 +68,27 @@ module "fargate" {
6868
source = "../../"
6969

7070
name_prefix = "example-app"
71-
vpc_id = "${data.aws_vpc.main.id}"
72-
private_subnet_ids = "${data.aws_subnet_ids.main.ids}"
73-
cluster_id = "${aws_ecs_cluster.cluster.id}"
71+
vpc_id = data.aws_vpc.main.id
72+
private_subnet_ids = data.aws_subnet_ids.main.ids
73+
cluster_id = aws_ecs_cluster.cluster.id
7474
task_container_image = "crccheck/hello-world:latest"
7575

7676
// public ip is needed for default vpc, default is false
77-
task_container_assign_public_ip = "true"
77+
task_container_assign_public_ip = true
7878

7979
// port, default protocol is HTTP
80-
task_container_port = "8000"
80+
task_container_port = 8000
8181

82-
health_check {
82+
health_check = {
8383
port = "traffic-port"
8484
path = "/"
8585
}
8686

87-
tags {
87+
tags = {
8888
environment = "test"
8989
terraform = "true"
9090
}
9191

92-
lb_arn = "${module.fargate_alb.arn}"
92+
lb_arn = module.fargate_alb.arn
9393
}
94+

examples/default/versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

main.tf

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,30 @@ data "aws_region" "current" {}
77
# Cloudwatch
88
# ------------------------------------------------------------------------------
99
resource "aws_cloudwatch_log_group" "main" {
10-
name = "${var.name_prefix}"
11-
retention_in_days = "${var.log_retention_in_days}"
12-
tags = "${var.tags}"
10+
name = var.name_prefix
11+
retention_in_days = var.log_retention_in_days
12+
tags = var.tags
1313
}
1414

1515
# ------------------------------------------------------------------------------
1616
# IAM - Task execution role, needed to pull ECR images etc.
1717
# ------------------------------------------------------------------------------
1818
resource "aws_iam_role" "execution" {
1919
name = "${var.name_prefix}-task-execution-role"
20-
assume_role_policy = "${data.aws_iam_policy_document.task_assume.json}"
20+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
2121
}
2222

2323
resource "aws_iam_role_policy" "task_execution" {
2424
name = "${var.name_prefix}-task-execution"
25-
role = "${aws_iam_role.execution.id}"
26-
policy = "${data.aws_iam_policy_document.task_execution_permissions.json}"
25+
role = aws_iam_role.execution.id
26+
policy = data.aws_iam_policy_document.task_execution_permissions.json
2727
}
2828

2929
resource "aws_iam_role_policy" "read_repository_credentials" {
30-
count = "${length(var.repository_credentials) != 0 ? 1 : 0}"
30+
count = length(var.repository_credentials) != 0 ? 1 : 0
3131
name = "${var.name_prefix}-read-repository-credentials"
32-
role = "${aws_iam_role.execution.id}"
33-
policy = "${data.aws_iam_policy_document.read_repository_credentials.json}"
32+
role = aws_iam_role.execution.id
33+
policy = data.aws_iam_policy_document.read_repository_credentials.json
3434
}
3535

3636
# ------------------------------------------------------------------------------
@@ -39,27 +39,32 @@ resource "aws_iam_role_policy" "read_repository_credentials" {
3939
# ------------------------------------------------------------------------------
4040
resource "aws_iam_role" "task" {
4141
name = "${var.name_prefix}-task-role"
42-
assume_role_policy = "${data.aws_iam_policy_document.task_assume.json}"
42+
assume_role_policy = data.aws_iam_policy_document.task_assume.json
4343
}
4444

4545
resource "aws_iam_role_policy" "log_agent" {
4646
name = "${var.name_prefix}-log-permissions"
47-
role = "${aws_iam_role.task.id}"
48-
policy = "${data.aws_iam_policy_document.task_permissions.json}"
47+
role = aws_iam_role.task.id
48+
policy = data.aws_iam_policy_document.task_permissions.json
4949
}
5050

5151
# ------------------------------------------------------------------------------
5252
# Security groups
5353
# ------------------------------------------------------------------------------
5454
resource "aws_security_group" "ecs_service" {
55-
vpc_id = "${var.vpc_id}"
55+
vpc_id = var.vpc_id
5656
name = "${var.name_prefix}-ecs-service-sg"
5757
description = "Fargate service security group"
58-
tags = "${merge(var.tags, map("Name", "${var.name_prefix}-sg"))}"
58+
tags = merge(
59+
var.tags,
60+
{
61+
Name = "${var.name_prefix}-sg"
62+
},
63+
)
5964
}
6065

6166
resource "aws_security_group_rule" "egress_service" {
62-
security_group_id = "${aws_security_group.ecs_service.id}"
67+
security_group_id = aws_security_group.ecs_service.id
6368
type = "egress"
6469
protocol = "-1"
6570
from_port = 0
@@ -72,11 +77,24 @@ resource "aws_security_group_rule" "egress_service" {
7277
# LB Target group
7378
# ------------------------------------------------------------------------------
7479
resource "aws_lb_target_group" "task" {
75-
vpc_id = "${var.vpc_id}"
76-
protocol = "${var.task_container_protocol}"
77-
port = "${var.task_container_port}"
78-
target_type = "ip"
79-
health_check = ["${var.health_check}"]
80+
vpc_id = var.vpc_id
81+
protocol = var.task_container_protocol
82+
port = var.task_container_port
83+
target_type = "ip"
84+
dynamic "health_check" {
85+
for_each = [var.health_check]
86+
content {
87+
enabled = lookup(health_check.value, "enabled", null)
88+
healthy_threshold = lookup(health_check.value, "healthy_threshold", null)
89+
interval = lookup(health_check.value, "interval", null)
90+
matcher = lookup(health_check.value, "matcher", null)
91+
path = lookup(health_check.value, "path", null)
92+
port = lookup(health_check.value, "port", null)
93+
protocol = lookup(health_check.value, "protocol", null)
94+
timeout = lookup(health_check.value, "timeout", null)
95+
unhealthy_threshold = lookup(health_check.value, "unhealthy_threshold", null)
96+
}
97+
}
8098

8199
# NOTE: TF is unable to destroy a target group while a listener is attached,
82100
# therefor we have to create a new one before destroying the old. This also means
@@ -85,29 +103,34 @@ resource "aws_lb_target_group" "task" {
85103
create_before_destroy = true
86104
}
87105

88-
tags = "${merge(var.tags, map("Name", "${var.name_prefix}-target-${var.task_container_port}"))}"
106+
tags = merge(
107+
var.tags,
108+
{
109+
Name = "${var.name_prefix}-target-${var.task_container_port}"
110+
},
111+
)
89112
}
90113

91114
# ------------------------------------------------------------------------------
92115
# ECS Task/Service
93116
# ------------------------------------------------------------------------------
94117
data "null_data_source" "task_environment" {
95-
count = "${var.task_container_environment_count}"
118+
count = var.task_container_environment_count
96119

97120
inputs = {
98-
name = "${element(keys(var.task_container_environment), count.index)}"
99-
value = "${element(values(var.task_container_environment), count.index)}"
121+
name = element(keys(var.task_container_environment), count.index)
122+
value = element(values(var.task_container_environment), count.index)
100123
}
101124
}
102125

103126
resource "aws_ecs_task_definition" "task" {
104-
family = "${var.name_prefix}"
105-
execution_role_arn = "${aws_iam_role.execution.arn}"
127+
family = var.name_prefix
128+
execution_role_arn = aws_iam_role.execution.arn
106129
network_mode = "awsvpc"
107130
requires_compatibilities = ["FARGATE"]
108-
cpu = "${var.task_definition_cpu}"
109-
memory = "${var.task_definition_memory}"
110-
task_role_arn = "${aws_iam_role.task.arn}"
131+
cpu = var.task_definition_cpu
132+
memory = var.task_definition_memory
133+
task_role_arn = aws_iam_role.task.arn
111134

112135
container_definitions = <<EOF
113136
[{
@@ -134,34 +157,35 @@ resource "aws_ecs_task_definition" "task" {
134157
"environment": ${jsonencode(data.null_data_source.task_environment.*.outputs)}
135158
}]
136159
EOF
160+
137161
}
138162

139163
resource "aws_ecs_service" "service" {
140-
depends_on = ["null_resource.lb_exists"]
141-
name = "${var.name_prefix}"
142-
cluster = "${var.cluster_id}"
143-
task_definition = "${aws_ecs_task_definition.task.arn}"
144-
desired_count = "${var.desired_count}"
164+
depends_on = [null_resource.lb_exists]
165+
name = var.name_prefix
166+
cluster = var.cluster_id
167+
task_definition = aws_ecs_task_definition.task.arn
168+
desired_count = var.desired_count
145169
launch_type = "FARGATE"
146-
deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"
147-
deployment_maximum_percent = "${var.deployment_maximum_percent}"
148-
health_check_grace_period_seconds = "${var.health_check_grace_period_seconds}"
170+
deployment_minimum_healthy_percent = var.deployment_minimum_healthy_percent
171+
deployment_maximum_percent = var.deployment_maximum_percent
172+
health_check_grace_period_seconds = var.health_check_grace_period_seconds
149173

150174
network_configuration {
151-
subnets = ["${var.private_subnet_ids}"]
152-
security_groups = ["${aws_security_group.ecs_service.id}"]
153-
assign_public_ip = "${var.task_container_assign_public_ip}"
175+
subnets = var.private_subnet_ids
176+
security_groups = [aws_security_group.ecs_service.id]
177+
assign_public_ip = var.task_container_assign_public_ip
154178
}
155179

156180
load_balancer {
157-
container_name = "${var.container_name != "" ? var.container_name : var.name_prefix}"
158-
container_port = "${var.task_container_port}"
159-
target_group_arn = "${aws_lb_target_group.task.arn}"
181+
container_name = var.container_name != "" ? var.container_name : var.name_prefix
182+
container_port = var.task_container_port
183+
target_group_arn = aws_lb_target_group.task.arn
160184
}
161185

162186
deployment_controller {
163187
# The deployment controller type to use. Valid values: CODE_DEPLOY, ECS.
164-
type = "${var.deployment_controller_type}"
188+
type = var.deployment_controller_type
165189
}
166190
}
167191

@@ -170,7 +194,8 @@ resource "aws_ecs_service" "service" {
170194
# see https://github.com/hashicorp/terraform/issues/12634.
171195
# Service depends on this resources which prevents it from being created until the LB is ready
172196
resource "null_resource" "lb_exists" {
173-
triggers {
174-
alb_name = "${var.lb_arn}"
197+
triggers = {
198+
alb_name = var.lb_arn
175199
}
176200
}
201+

outputs.tf

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@
33
# ------------------------------------------------------------------------------
44
output "service_arn" {
55
description = "The Amazon Resource Name (ARN) that identifies the service."
6-
value = "${aws_ecs_service.service.id}"
6+
value = aws_ecs_service.service.id
77
}
88

99
output "target_group_arn" {
1010
description = "The ARN of the Target Group."
11-
value = "${aws_lb_target_group.task.arn}"
11+
value = aws_lb_target_group.task.arn
1212
}
1313

1414
output "target_group_name" {
1515
description = "The Name of the Target Group."
16-
value = "${aws_lb_target_group.task.name}"
16+
value = aws_lb_target_group.task.name
1717
}
1818

1919
output "task_role_arn" {
2020
description = "The Amazon Resource Name (ARN) specifying the service role."
21-
value = "${aws_iam_role.task.arn}"
21+
value = aws_iam_role.task.arn
2222
}
2323

2424
output "task_role_name" {
2525
description = "The name of the service role."
26-
value = "${aws_iam_role.task.name}"
26+
value = aws_iam_role.task.name
2727
}
2828

2929
output "service_sg_id" {
3030
description = "The Amazon Resource Name (ARN) that identifies the service security group."
31-
value = "${aws_security_group.ecs_service.id}"
31+
value = aws_security_group.ecs_service.id
3232
}
3333

3434
output "service_name" {
3535
description = "The name of the service."
36-
value = "${aws_ecs_service.service.name}"
36+
value = aws_ecs_service.service.name
3737
}
3838

3939
output "log_group_name" {
4040
description = "The name of the Cloudwatch log group for the task."
41-
value = "${aws_cloudwatch_log_group.main.name}"
41+
value = aws_cloudwatch_log_group.main.name
4242
}
43+

0 commit comments

Comments
 (0)