forked from terraform-community-modules/tf_aws_ecs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
191 lines (163 loc) · 6.16 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
data "aws_ami" "ecs_ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn-ami-${var.ami_version}-amazon-ecs-optimized"]
}
}
data "template_file" "user_data" {
template = file("${path.module}/templates/user_data.tpl")
vars = {
additional_user_data_script = var.additional_user_data_script
cluster_name = aws_ecs_cluster.cluster.name
docker_storage_size = var.docker_storage_size
dockerhub_token = var.dockerhub_token
dockerhub_email = var.dockerhub_email
}
}
data "aws_vpc" "vpc" {
id = var.vpc_id
}
resource "aws_launch_configuration" "ecs" {
name_prefix = coalesce(var.name_prefix, "ecs-${var.name}-")
image_id = var.ami == "" ? format("%s", data.aws_ami.ecs_ami.id) : var.ami # Workaround until 0.9.6
instance_type = var.instance_type
key_name = var.key_name
iam_instance_profile = aws_iam_instance_profile.ecs_profile.name
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibility in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
security_groups = concat([aws_security_group.ecs.id], var.security_group_ids)
associate_public_ip_address = var.associate_public_ip_address
spot_price = var.spot_bid_price
ebs_block_device {
device_name = var.ebs_block_device
volume_size = var.docker_storage_size
volume_type = "gp2"
delete_on_termination = true
}
user_data = coalesce(var.user_data, data.template_file.user_data.rendered)
lifecycle {
create_before_destroy = true
}
root_block_device {
volume_size = var.aws_ecs_root_block_size
}
}
# Optional Second Launch Config for the optional Second ASG
resource "aws_launch_configuration" "ecs_second" {
count = var.second_asg_servers > 0 ? 1 : 0
name_prefix = coalesce(var.name_prefix, "ecs-second-${var.name}-")
image_id = var.second_asg_ami == "" ? format("%s", data.aws_ami.ecs_ami.id) : var.second_asg_ami # Workaround until 0.9.6
instance_type = var.instance_type
key_name = var.key_name
iam_instance_profile = aws_iam_instance_profile.ecs_profile.name
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibility in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
security_groups = concat([aws_security_group.ecs.id], var.security_group_ids)
associate_public_ip_address = var.associate_public_ip_address
spot_price = var.spot_bid_price
ebs_block_device {
device_name = var.ebs_block_device
volume_size = var.docker_storage_size
volume_type = "gp2"
delete_on_termination = true
}
user_data = coalesce(var.user_data, data.template_file.user_data.rendered)
lifecycle {
create_before_destroy = true
}
root_block_device {
volume_size = var.aws_ecs_root_block_size
}
}
resource "aws_autoscaling_group" "ecs" {
name_prefix = "asg-${aws_launch_configuration.ecs.name}-"
vpc_zone_identifier = var.subnet_id
launch_configuration = aws_launch_configuration.ecs.name
min_size = var.min_servers
max_size = var.max_servers
desired_capacity = var.servers
termination_policies = ["OldestLaunchConfiguration", "ClosestToNextInstanceHour", "Default"]
load_balancers = var.load_balancers
enabled_metrics = var.enabled_metrics
tags = concat(
[map("key", "Name","value","${var.name} ${var.tagName}","propagate_at_launch", "true")],
var.extra_tags,
)
lifecycle {
create_before_destroy = true
}
timeouts {
delete = "${var.heartbeat_timeout + var.asg_delete_extra_timeout}s"
}
}
# Optional Second ASG
resource "aws_autoscaling_group" "ecs_second" {
count = var.second_asg_servers > 0 ? 1 : 0
name_prefix = "asg-second-${aws_launch_configuration.ecs_second[0].name}-"
vpc_zone_identifier = var.subnet_id
launch_configuration = aws_launch_configuration.ecs_second[0].name
min_size = var.second_asg_min_servers
max_size = var.second_asg_max_servers
desired_capacity = var.second_asg_servers
termination_policies = ["OldestLaunchConfiguration", "ClosestToNextInstanceHour", "Default"]
load_balancers = var.load_balancers
enabled_metrics = var.enabled_metrics
tags = concat(
[{
"key" = "Name"
"value" = "${var.name} ${var.tagName} Second"
"propagate_at_launch" = true
}],
var.extra_tags,
)
lifecycle {
create_before_destroy = true
}
timeouts {
delete = "${var.heartbeat_timeout + var.asg_delete_extra_timeout}s"
}
}
resource "aws_security_group" "ecs" {
name = "ecs-sg-${var.name}"
description = "Container Instance Allowed Ports"
vpc_id = data.aws_vpc.vpc.id
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
}
ingress {
from_port = 0
to_port = 65535
protocol = "udp"
cidr_blocks = var.allowed_cidr_blocks
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "ecs-sg-${var.name}"
}
}
# Make this a var that an get passed in?
resource "aws_ecs_cluster" "cluster" {
name = var.name
}