-
Notifications
You must be signed in to change notification settings - Fork 70
/
main.tf
232 lines (187 loc) · 5.59 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#
# Container Instance IAM resources
#
data "aws_iam_policy_document" "container_instance_ec2_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "container_instance_ec2" {
name = coalesce(
var.ecs_for_ec2_service_role_name,
local.ecs_for_ec2_service_role_name,
)
assume_role_policy = data.aws_iam_policy_document.container_instance_ec2_assume_role.json
}
resource "aws_iam_role_policy_attachment" "ec2_service_role" {
role = aws_iam_role.container_instance_ec2.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_instance_profile" "container_instance" {
name = aws_iam_role.container_instance_ec2.name
role = aws_iam_role.container_instance_ec2.name
}
#
# ECS Service IAM permissions
#
data "aws_iam_policy_document" "ecs_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["ecs.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "ecs_service_role" {
name = coalesce(var.ecs_service_role_name, local.ecs_service_role_name)
assume_role_policy = data.aws_iam_policy_document.ecs_assume_role.json
}
resource "aws_iam_role_policy_attachment" "ecs_service_role" {
role = aws_iam_role.ecs_service_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole"
}
data "aws_iam_policy_document" "ecs_autoscale_assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["application-autoscaling.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
#
# Security group resources
#
resource "aws_security_group" "container_instance" {
vpc_id = var.vpc_id
tags = {
Name = coalesce(var.security_group_name, local.security_group_name)
Project = var.project
Environment = var.environment
}
}
#
# AutoScaling resources
#
data "template_file" "container_instance_base_cloud_config" {
template = file(
"${path.module}/cloud-config/base-container-instance.yml.tpl",
)
vars = {
ecs_cluster_name = aws_ecs_cluster.container_instance.name
}
}
data "template_cloudinit_config" "container_instance_cloud_config" {
gzip = false
base64_encode = false
part {
content_type = "text/cloud-config"
content = data.template_file.container_instance_base_cloud_config.rendered
}
part {
content_type = var.cloud_config_content_type
content = var.cloud_config_content
}
}
data "aws_ami" "ecs_ami" {
count = var.lookup_latest_ami ? 1 : 0
most_recent = true
owners = var.ami_owners
filter {
name = "name"
values = ["amzn-ami-*-amazon-ecs-optimized"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
data "aws_ami" "user_ami" {
count = var.lookup_latest_ami ? 0 : 1
owners = var.ami_owners
filter {
name = "image-id"
values = [var.ami_id]
}
}
resource "aws_launch_template" "container_instance" {
block_device_mappings {
device_name = var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.root_device_name) : join("", data.aws_ami.user_ami.*.root_device_name)
ebs {
volume_type = var.root_block_device_type
volume_size = var.root_block_device_size
}
}
credit_specification {
cpu_credits = var.cpu_credit_specification
}
disable_api_termination = false
name_prefix = "lt${title(var.environment)}ContainerInstance-"
iam_instance_profile {
name = aws_iam_instance_profile.container_instance.name
}
# Using join() is a workaround for depending on conditional resources.
# https://github.com/hashicorp/terraform/issues/2831#issuecomment-298751019
image_id = var.lookup_latest_ami ? join("", data.aws_ami.ecs_ami.*.image_id) : join("", data.aws_ami.user_ami.*.image_id)
instance_initiated_shutdown_behavior = "terminate"
instance_type = var.instance_type
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.container_instance.id]
user_data = base64encode(
data.template_cloudinit_config.container_instance_cloud_config.rendered,
)
monitoring {
enabled = var.detailed_monitoring
}
}
resource "aws_autoscaling_group" "container_instance" {
lifecycle {
create_before_destroy = true
}
name = coalesce(var.autoscaling_group_name, local.autoscaling_group_name)
launch_template {
id = aws_launch_template.container_instance.id
version = "$Latest"
}
health_check_grace_period = var.health_check_grace_period
health_check_type = "EC2"
desired_capacity = var.desired_capacity
termination_policies = ["OldestLaunchConfiguration", "Default"]
min_size = var.min_size
max_size = var.max_size
enabled_metrics = var.enabled_metrics
vpc_zone_identifier = var.subnet_ids
tag {
key = "Name"
value = "ContainerInstance"
propagate_at_launch = true
}
tag {
key = "Project"
value = var.project
propagate_at_launch = true
}
tag {
key = "Environment"
value = var.environment
propagate_at_launch = true
}
}
#
# ECS resources
#
resource "aws_ecs_cluster" "container_instance" {
name = coalesce(var.cluster_name, local.cluster_name)
}