Skip to content

Commit 1d5cbcf

Browse files
author
diego
committed
refactor(Terraform 0.12.x): Update dynamic blocks
1 parent 0dfbf44 commit 1d5cbcf

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

main.tf

+37-10
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,48 @@ resource "aws_ecs_task_definition" "this" {
1515
count = "${var.network_mode != "awsvpc" ? 1 : 0}"
1616
family = "${local.id}"
1717
container_definitions = "${data.template_file.this.rendered}"
18-
volume = "${var.volumes}"
18+
dynamic "volume" {
19+
for_each = var.volumes.name == "" ? [] : list(var.volumes)
20+
content {
21+
name = volume.name
22+
host_path = volume.host_path
23+
}
24+
}
1925
task_role_arn = "${aws_iam_role.this.arn}"
2026
execution_role_arn = "${var.enable_ssm ? aws_iam_role.this.arn : ""}"
21-
requires_compatibilities = ["${var.compatibilities}"]
22-
placement_constraints = "${var.placement_constraints}"
27+
requires_compatibilities = "${var.compatibilities}"
28+
dynamic "placement_constraints" {
29+
for_each = var.placement_constraints.type == "" ? [] : list(var.placement_constraints)
30+
content {
31+
type = placement_constraints.type
32+
expression = placement_constraints.expression
33+
}
34+
}
35+
#placement_constraints {
36+
# type = "${var.placement_constraints.type}"
37+
# expression = "${var.placement_constraints.expression}"
38+
#}
2339
}
2440

2541
resource "aws_ecs_task_definition" "private" {
2642
count = "${var.network_mode == "awsvpc" ? 1 : 0}"
2743
family = "${local.id}"
2844
container_definitions = "${data.template_file.this.rendered}"
29-
volume = "${var.volumes}"
45+
dynamic "volume" {
46+
for_each = var.volumes.name == "" ? [] : list(var.volumes)
47+
48+
content {
49+
name = volume.name
50+
host_path = volume.host_path
51+
}
52+
}
53+
#volume {
54+
# name = "${var.volumes.name}"
55+
# host_path = "${var.volumes.host_path}"
56+
#}
3057
task_role_arn = "${aws_iam_role.this.arn}"
3158
execution_role_arn = "${aws_iam_role.this.arn}"
32-
requires_compatibilities = ["${var.compatibilities}"]
59+
requires_compatibilities = "${var.compatibilities}"
3360
network_mode = "${var.network_mode}"
3461
}
3562

@@ -93,7 +120,7 @@ resource "aws_ecs_service" "this" {
93120
count = "${var.balancer["vpc_id"] != "" && var.cluster != "" && var.network_mode != "awsvpc" ? 1 : 0}"
94121
name = "${local.id}"
95122
cluster = "${var.cluster}"
96-
task_definition = "${aws_ecs_task_definition.this.arn}"
123+
task_definition = "${aws_ecs_task_definition.this.0.arn}"
97124
desired_count = "${var.desired}"
98125
iam_role = "arn:aws:iam::${var.aws_account}:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS"
99126
health_check_grace_period_seconds = 0
@@ -114,7 +141,7 @@ resource "aws_ecs_service" "private" {
114141
count = "${var.balancer["vpc_id"] != "" && var.cluster != "" && var.network_mode == "awsvpc" ? 1 : 0}"
115142
name = "${local.id}"
116143
cluster = "${var.cluster}"
117-
task_definition = "${aws_ecs_task_definition.private.arn}"
144+
task_definition = "${aws_ecs_task_definition.private.0.arn}"
118145
desired_count = "${var.desired}"
119146
#iam_role = "aws-service-role"
120147
health_check_grace_period_seconds = 0
@@ -131,7 +158,7 @@ resource "aws_ecs_service" "private" {
131158
}
132159

133160
network_configuration {
134-
subnets = ["${var.subnets}"]
161+
subnets = "${var.subnets}"
135162
security_groups = ["${var.security_group}"]
136163
}
137164

@@ -145,7 +172,7 @@ resource "aws_ecs_service" "no_balancer" {
145172
count = "${var.balancer["vpc_id"] != "" || var.cluster == "" ? 0 : 1}"
146173
name = "${local.id}"
147174
cluster = "${var.cluster}"
148-
task_definition = "${aws_ecs_task_definition.this.arn}"
175+
task_definition = "${aws_ecs_task_definition.this.0.arn}"
149176
desired_count = "${var.desired}"
150177
health_check_grace_period_seconds = 0
151178

@@ -176,7 +203,7 @@ EOF
176203

177204
module "logs" {
178205
source = "Aplyca/cloudwatchlogs/aws"
179-
version = "0.1.0"
206+
version = "0.2.0"
180207

181208
name = "${local.id}"
182209
role = "${aws_iam_role.this.name}"

variables.tf

+9-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ variable "health_check" {
5252

5353
variable "volumes" {
5454
description = "Volumes"
55-
default = []
55+
default = {
56+
name = ""
57+
host_path = ""
58+
}
5659
}
5760

5861
variable "desired" {
@@ -113,6 +116,9 @@ variable "target_group" {
113116

114117
variable "placement_constraints" {
115118
description = "(Optional) A set of rules that are taken during task placement"
116-
type = "list"
117-
default = []
119+
//type = "list"
120+
default = {
121+
type = ""
122+
expression = ""
123+
}
118124
}

0 commit comments

Comments
 (0)