Skip to content

Commit 8c479f9

Browse files
author
Sumit Sarkar
committed
FEAT: Upgrade module to utilize terraform 0.12
1 parent e8dab77 commit 8c479f9

File tree

4 files changed

+131
-124
lines changed

4 files changed

+131
-124
lines changed

main.tf

Lines changed: 87 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
provider "aws" {
2-
region = "${var.region}"
2+
region = var.region
33
}
44

55
#
@@ -20,17 +20,17 @@ data "aws_iam_policy_document" "container_instance_ec2_assume_role" {
2020

2121
resource "aws_iam_role" "container_instance_ec2" {
2222
name = "${var.environment}ContainerInstanceProfile"
23-
assume_role_policy = "${data.aws_iam_policy_document.container_instance_ec2_assume_role.json}"
23+
assume_role_policy = data.aws_iam_policy_document.container_instance_ec2_assume_role.json
2424
}
2525

2626
resource "aws_iam_role_policy_attachment" "ec2_service_role" {
27-
role = "${aws_iam_role.container_instance_ec2.name}"
27+
role = aws_iam_role.container_instance_ec2.name
2828
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
2929
}
3030

3131
resource "aws_iam_instance_profile" "container_instance" {
32-
name = "${aws_iam_role.container_instance_ec2.name}"
33-
role = "${aws_iam_role.container_instance_ec2.name}"
32+
name = aws_iam_role.container_instance_ec2.name
33+
role = aws_iam_role.container_instance_ec2.name
3434
}
3535

3636
#
@@ -52,11 +52,11 @@ data "aws_iam_policy_document" "ecs_assume_role" {
5252

5353
resource "aws_iam_role" "ecs_service_role" {
5454
name = "ecs${title(var.environment)}ServiceRole"
55-
assume_role_policy = "${data.aws_iam_policy_document.ecs_assume_role.json}"
55+
assume_role_policy = data.aws_iam_policy_document.ecs_assume_role.json
5656
}
5757

5858
resource "aws_iam_role_policy_attachment" "ecs_service_role" {
59-
role = "${aws_iam_role.ecs_service_role.name}"
59+
role = aws_iam_role.ecs_service_role.name
6060
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
6161
}
6262

@@ -77,31 +77,34 @@ data "aws_iam_policy_document" "ecs_autoscale_assume_role" {
7777
# Security group resources
7878
#
7979
resource "aws_security_group" "container_instance" {
80-
vpc_id = "${var.vpc_id}"
80+
vpc_id = var.vpc_id
8181

8282
egress {
8383
from_port = 0
84-
to_port = 0
85-
protocol = "-1"
84+
to_port = 0
85+
protocol = "-1"
8686
cidr_blocks = [
87-
"0.0.0.0/0"]
87+
"0.0.0.0/0",
88+
]
8889
}
8990

90-
tags {
91+
tags = {
9192
Name = "sgContainerInstance"
92-
Project = "${var.project}"
93-
Environment = "${var.environment}"
93+
Project = var.project
94+
Environment = var.environment
9495
}
9596
}
9697

9798
#
9899
# AutoScaling resources
99100
#
100101
data "template_file" "container_instance_base_cloud_config" {
101-
template = "${file("${path.module}/cloud-config/base-container-instance.yml.tpl")}"
102+
template = file(
103+
"${path.module}/cloud-config/base-container-instance.yml.tpl",
104+
)
102105

103-
vars {
104-
ecs_cluster_name = "${aws_ecs_cluster.container_instance.name}"
106+
vars = {
107+
ecs_cluster_name = aws_ecs_cluster.container_instance.name
105108
}
106109
}
107110

@@ -111,28 +114,24 @@ data "template_cloudinit_config" "container_instance_cloud_config" {
111114

112115
part {
113116
content_type = "text/cloud-config"
114-
content = "${data.template_file.container_instance_base_cloud_config.rendered}"
117+
content = data.template_file.container_instance_base_cloud_config.rendered
115118
}
116119

117120
part {
118-
content_type = "${var.cloud_config_content_type}"
119-
content = "${var.cloud_config_content}"
121+
content_type = var.cloud_config_content_type
122+
content = var.cloud_config_content
120123
}
121124
}
122125

123126
data "aws_ami" "ecs_ami" {
124-
count = "${var.lookup_latest_ami ? 1 : 0}"
127+
count = var.lookup_latest_ami ? 1 : 0
125128
most_recent = true
126129

127130
filter {
128131
name = "name"
129132
values = ["amzn-ami-*-amazon-ecs-optimized"]
130133
}
131134

132-
filter {
133-
name = "owner-alias"
134-
values = ["${var.ami_owners}"]
135-
}
136135

137136
filter {
138137
name = "architecture"
@@ -143,59 +142,60 @@ data "aws_ami" "ecs_ami" {
143142
name = "virtualization-type"
144143
values = ["hvm"]
145144
}
145+
owners = [var.ami_owners]
146146
}
147147

148148
data "aws_ami" "user_ami" {
149-
count = "${var.lookup_latest_ami ? 0 : 1}"
150-
owners = ["${var.ami_owners}"]
149+
count = var.lookup_latest_ami ? 0 : 1
150+
owners = var.ami_owners
151151

152152
filter {
153153
name = "image-id"
154-
values = ["${var.ami_id}"]
154+
values = [var.ami_id]
155155
}
156156
}
157157

158158
resource "aws_launch_configuration" "container_instance" {
159-
count = "${length(var.instance_types)}"
159+
count = length(var.instance_types)
160160

161161
lifecycle {
162162
create_before_destroy = true
163163
}
164164

165165
root_block_device {
166-
volume_type = "${element(var.root_block_device_type, count.index)}"
167-
volume_size = "${element(var.root_block_device_size, count.index)}"
166+
volume_type = var.root_block_device_type[count.index]
167+
volume_size = var.root_block_device_size[count.index]
168168
}
169169

170170
name_prefix = "lc${title(var.environment)}ContainerInstance-"
171-
iam_instance_profile = "${aws_iam_instance_profile.container_instance.name}"
171+
iam_instance_profile = aws_iam_instance_profile.container_instance.name
172172

173173
# Using join() is a workaround for depending on conditional resources.
174174
# https://github.com/hashicorp/terraform/issues/2831#issuecomment-298751019
175-
image_id = "${var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.image_id) : join("", data.aws_ami.user_ami.*.image_id)}"
175+
image_id = var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.image_id) : join("", data.aws_ami.user_ami.*.image_id)
176176

177-
instance_type = "${element(var.instance_types, count.index)}"
178-
key_name = "${var.key_name}"
179-
security_groups = ["${aws_security_group.container_instance.id}"]
180-
user_data = "${data.template_cloudinit_config.container_instance_cloud_config.rendered}"
177+
instance_type = var.instance_types[count.index]
178+
key_name = var.key_name
179+
security_groups = [aws_security_group.container_instance.id]
180+
user_data = data.template_cloudinit_config.container_instance_cloud_config.rendered
181181
}
182182

183183
resource "aws_autoscaling_group" "container_instance" {
184-
count = "${length(var.instance_types)}"
184+
count = length(var.instance_types)
185185
lifecycle {
186186
create_before_destroy = true
187187
}
188188

189-
name = "asg${title(var.environment)}ContainerInstance${count.index}"
190-
launch_configuration = "${element(aws_launch_configuration.container_instance.*.name, count.index)}"
191-
health_check_grace_period = "${var.health_check_grace_period}"
189+
name = "asg${title(var.environment)}ContainerInstance${count.index}"
190+
launch_configuration = aws_launch_configuration.container_instance.*.name[count.index]
191+
health_check_grace_period = var.health_check_grace_period
192192
health_check_type = "EC2"
193-
desired_capacity = "${element(var.desired_capacity, count.index)}"
193+
desired_capacity = var.desired_capacity[count.index]
194194
termination_policies = ["OldestLaunchConfiguration", "Default"]
195-
min_size = "${element(var.min_size, count.index)}"
196-
max_size = "${element(var.max_size, count.index)}"
197-
enabled_metrics = ["${var.enabled_metrics}"]
198-
vpc_zone_identifier = ["${var.private_subnet_ids}"]
195+
min_size = var.min_size[count.index]
196+
max_size = var.max_size[count.index]
197+
enabled_metrics = var.enabled_metrics
198+
vpc_zone_identifier = var.private_subnet_ids
199199

200200
tag {
201201
key = "Name"
@@ -205,19 +205,19 @@ resource "aws_autoscaling_group" "container_instance" {
205205

206206
tag {
207207
key = "Project"
208-
value = "${var.project}"
208+
value = var.project
209209
propagate_at_launch = true
210210
}
211211

212212
tag {
213213
key = "Environment"
214-
value = "${var.environment}"
214+
value = var.environment
215215
propagate_at_launch = true
216216
}
217217

218218
tag {
219219
key = "spot-enabled"
220-
value = "${var.spot_enabled}"
220+
value = var.spot_enabled
221221
propagate_at_launch = true
222222
}
223223
}
@@ -233,103 +233,104 @@ resource "aws_ecs_cluster" "container_instance" {
233233
# CloudWatch resources
234234
#
235235
resource "aws_autoscaling_policy" "container_instance_scale_up" {
236-
count = "${length(var.instance_types)}"
236+
count = length(var.instance_types)
237237

238238
name = "asgScalingPolicy${title(var.environment)}ClusterScaleUp${count.index}"
239239
scaling_adjustment = 1
240240
adjustment_type = "ChangeInCapacity"
241-
cooldown = "${element(var.scale_up_cooldown_seconds, count.index)}"
242-
autoscaling_group_name = "${element(aws_autoscaling_group.container_instance.*.name, count.index)}"
241+
cooldown = var.scale_up_cooldown_seconds[count.index]
242+
autoscaling_group_name = aws_autoscaling_group.container_instance.*.name[count.index]
243243
}
244244

245245
resource "aws_autoscaling_policy" "container_instance_scale_down" {
246-
count = "${length(var.instance_types)}"
246+
count = length(var.instance_types)
247247
name = "asgScalingPolicy${title(var.environment)}ClusterScaleDown${count.index}"
248248
scaling_adjustment = -1
249249
adjustment_type = "ChangeInCapacity"
250-
cooldown = "${element(var.scale_down_cooldown_seconds, count.index)}"
251-
autoscaling_group_name = "${element(aws_autoscaling_group.container_instance.*.name, count.index)}"
250+
cooldown = var.scale_down_cooldown_seconds[count.index]
251+
autoscaling_group_name = aws_autoscaling_group.container_instance.*.name[count.index]
252252
}
253253

254254
resource "aws_cloudwatch_metric_alarm" "container_instance_high_cpu" {
255-
count = "${length(var.instance_types)}"
255+
count = length(var.instance_types)
256256

257257
alarm_name = "alarm${title(var.environment)}ClusterCPUReservationHigh${count.index}"
258258
comparison_operator = "GreaterThanOrEqualToThreshold"
259-
evaluation_periods = "${element(var.high_cpu_evaluation_periods, count.index)}"
259+
evaluation_periods = var.high_cpu_evaluation_periods[count.index]
260260
metric_name = "CPUReservation"
261261
namespace = "AWS/ECS"
262-
period = "${element(var.high_cpu_period_seconds, count.index)}"
262+
period = var.high_cpu_period_seconds[count.index]
263263
statistic = "Maximum"
264-
threshold = "${element(var.high_cpu_threshold_percent, count.index)}"
264+
threshold = var.high_cpu_threshold_percent[count.index]
265265

266-
dimensions {
267-
ClusterName = "${aws_ecs_cluster.container_instance.name}"
266+
dimensions = {
267+
ClusterName = aws_ecs_cluster.container_instance.name
268268
}
269269

270270
alarm_description = "Scale up if CPUReservation is above N% for N duration"
271-
alarm_actions = ["${element(aws_autoscaling_policy.container_instance_scale_up.*.arn, count.index)}"]
271+
alarm_actions = [aws_autoscaling_policy.container_instance_scale_up.*.arn[count.index]]
272272
}
273273

274274
resource "aws_cloudwatch_metric_alarm" "container_instance_low_cpu" {
275-
count = "${length(var.instance_types)}"
275+
count = length(var.instance_types)
276276
alarm_name = "alarm${title(var.environment)}ClusterCPUReservationLow${count.index}"
277277
comparison_operator = "LessThanOrEqualToThreshold"
278-
evaluation_periods = "${element(var.low_cpu_evaluation_periods, count.index)}"
278+
evaluation_periods = var.low_cpu_evaluation_periods[count.index]
279279
metric_name = "CPUReservation"
280280
namespace = "AWS/ECS"
281-
period = "${element(var.low_cpu_period_seconds, count.index)}"
281+
period = var.low_cpu_period_seconds[count.index]
282282
statistic = "Maximum"
283-
threshold = "${element(var.low_cpu_threshold_percent, count.index)}"
283+
threshold = var.low_cpu_threshold_percent[count.index]
284284

285-
dimensions {
286-
ClusterName = "${aws_ecs_cluster.container_instance.name}"
285+
dimensions = {
286+
ClusterName = aws_ecs_cluster.container_instance.name
287287
}
288288

289289
alarm_description = "Scale down if the CPUReservation is below N% for N duration"
290-
alarm_actions = ["${element(aws_autoscaling_policy.container_instance_scale_down.*.arn, count.index)}"]
290+
alarm_actions = [aws_autoscaling_policy.container_instance_scale_down.*.arn[count.index]]
291291

292-
depends_on = ["aws_cloudwatch_metric_alarm.container_instance_high_cpu"]
292+
depends_on = [aws_cloudwatch_metric_alarm.container_instance_high_cpu]
293293
}
294294

295295
resource "aws_cloudwatch_metric_alarm" "container_instance_high_memory" {
296-
count = "${length(var.instance_types)}"
296+
count = length(var.instance_types)
297297
alarm_name = "alarm${title(var.environment)}ClusterMemoryReservationHigh${count.index}"
298298
comparison_operator = "GreaterThanOrEqualToThreshold"
299-
evaluation_periods = "${element(var.high_memory_evaluation_periods, count.index)}"
299+
evaluation_periods = var.high_memory_evaluation_periods[count.index]
300300
metric_name = "MemoryReservation"
301301
namespace = "AWS/ECS"
302-
period = "${element(var.high_memory_period_seconds, count.index)}"
302+
period = var.high_memory_period_seconds[count.index]
303303
statistic = "Maximum"
304-
threshold = "${element(var.high_memory_threshold_percent, count.index)}"
304+
threshold = var.high_memory_threshold_percent[count.index]
305305

306-
dimensions {
307-
ClusterName = "${aws_ecs_cluster.container_instance.name}"
306+
dimensions = {
307+
ClusterName = aws_ecs_cluster.container_instance.name
308308
}
309309

310310
alarm_description = "Scale up if the MemoryReservation is above N% for N duration"
311-
alarm_actions = ["${element(aws_autoscaling_policy.container_instance_scale_up.*.arn, count.index)}"]
311+
alarm_actions = [aws_autoscaling_policy.container_instance_scale_up.*.arn[count.index]]
312312

313-
depends_on = ["aws_cloudwatch_metric_alarm.container_instance_low_cpu"]
313+
depends_on = [aws_cloudwatch_metric_alarm.container_instance_low_cpu]
314314
}
315315

316316
resource "aws_cloudwatch_metric_alarm" "container_instance_low_memory" {
317-
count = "${length(var.instance_types)}"
317+
count = length(var.instance_types)
318318
alarm_name = "alarm${title(var.environment)}ClusterMemoryReservationLow${count.index}"
319319
comparison_operator = "LessThanOrEqualToThreshold"
320-
evaluation_periods = "${element(var.low_memory_evaluation_periods, count.index)}"
320+
evaluation_periods = var.low_memory_evaluation_periods[count.index]
321321
metric_name = "MemoryReservation"
322322
namespace = "AWS/ECS"
323-
period = "${element(var.low_memory_period_seconds, count.index)}"
323+
period = var.low_memory_period_seconds[count.index]
324324
statistic = "Maximum"
325-
threshold = "${element(var.low_memory_threshold_percent, count.index)}"
325+
threshold = var.low_memory_threshold_percent[count.index]
326326

327-
dimensions {
328-
ClusterName = "${aws_ecs_cluster.container_instance.name}"
327+
dimensions = {
328+
ClusterName = aws_ecs_cluster.container_instance.name
329329
}
330330

331331
alarm_description = "Scale down if the MemoryReservation is below N% for N duration"
332-
alarm_actions = ["${element(aws_autoscaling_policy.container_instance_scale_down.*.arn, count.index)}"]
332+
alarm_actions = [aws_autoscaling_policy.container_instance_scale_down.*.arn[count.index]]
333333

334-
depends_on = ["aws_cloudwatch_metric_alarm.container_instance_high_memory"]
334+
depends_on = [aws_cloudwatch_metric_alarm.container_instance_high_memory]
335335
}
336+

0 commit comments

Comments
 (0)