-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathecs.tf
282 lines (246 loc) · 7.09 KB
/
ecs.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -------------------------------------------------------------------
# ECS Cluster
# -------------------------------------------------------------------
resource "aws_ecs_cluster" "uber" {
name = var.clustername
setting {
name = "containerInsights"
value = "disabled"
}
}
resource "aws_iam_policy" "task_role_logs" {
name = "UbersystemECSLogs"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"logs:CreateLogGroup"
]
Resource = "*"
}
]
})
}
resource "aws_iam_policy" "task_role_ssm" {
name = "UbersystemECSSSM"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
})
}
resource "aws_iam_policy" "task_role_secrets" {
name = "UbersystemECSSecrets"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": "*"
}
]
})
}
resource "aws_iam_role" "task_role" {
name = "UbersystemECSRole"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "task_role_secrets_attach" {
role = aws_iam_role.task_role.name
policy_arn = aws_iam_policy.task_role_secrets.arn
}
resource "aws_iam_role_policy_attachment" "task_role_logs_attach" {
role = aws_iam_role.task_role.name
policy_arn = aws_iam_policy.task_role_logs.arn
}
resource "aws_iam_role_policy_attachment" "task_role_ssm_attach" {
role = aws_iam_role.task_role.name
policy_arn = aws_iam_policy.task_role_ssm.arn
}
resource "aws_iam_role_policy_attachment" "task_role_default_attach" {
role = aws_iam_role.task_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# -------------------------------------------------------------------
# ECS EC2 Instances
# -------------------------------------------------------------------
data "aws_iam_policy_document" "ecs_agent" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "ec2_role_secrets" {
name = "UbersystemECSEC2Secrets"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue"
],
"Resource": "*"
}
]
})
}
resource "aws_iam_role" "ecs_agent" {
name = "ecs-agent"
assume_role_policy = data.aws_iam_policy_document.ecs_agent.json
}
resource "aws_iam_role_policy_attachment" "ecs_agent" {
role = aws_iam_role.ecs_agent.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_role_policy_attachment" "ec2_secrets" {
role = aws_iam_role.ecs_agent.name
policy_arn = aws_iam_policy.ec2_role_secrets.arn
}
resource "aws_iam_role_policy_attachment" "AmazonSSMManagedInstanceCore" {
role = aws_iam_role.ecs_agent.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_instance_profile" "ecs_agent" {
name = "ecs-agent"
role = aws_iam_role.ecs_agent.name
}
resource "aws_key_pair" "ecs" {
key_name = "uber_ec2_admin"
public_key = var.ssh_key
}
resource "aws_launch_configuration" "ecs_config_launch_config" {
name_prefix = "${var.clustername}_ecs_cluster"
image_id = data.aws_ami.aws_optimized_ecs.id
instance_type = var.instance_type
associate_public_ip_address = true
lifecycle {
create_before_destroy = true
}
user_data = <<EOF
#!/bin/bash
echo ECS_CLUSTER=${var.clustername} >> /etc/ecs/ecs.config
echo ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE=true >> /etc/ecs/ecs.config
IFS=","
USERS="${var.ssh_users}"
for USER in $USERS; do
echo "Adding $USER..."
adduser -d "/home/$USER" -s /bin/bash -m -G wheel,docker "$USER"
passwd -u "$USER"
mkdir -p "/home/$USER/.ssh"
curl -so "/home/$USER/.ssh/authorized_keys" "https://github.com/$USER.keys"
chown -R "$USER:$USER" "/home/$USER/.ssh"
chmod 755 "/home/$USER/.ssh"
chmod 644 "/home/$USER/.ssh/authorized_keys"
done
echo "%wheel ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/wheel
yum install -y unzip
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
EOF
security_groups = [
aws_security_group.uber_efs_ec2.id
]
key_name = aws_key_pair.ecs.key_name
iam_instance_profile = aws_iam_instance_profile.ecs_agent.arn
}
data "aws_ami" "aws_optimized_ecs" {
most_recent = true
filter {
name = "name"
values = ["amzn-ami*amazon-ecs-optimized"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["591542846629"] # AWS
}
resource "aws_ecs_account_setting_default" "account_settings" {
name = "awsvpcTrunking"
value = "enabled"
}
resource "aws_autoscaling_group" "ecs_cluster" {
name_prefix = "${var.clustername}_asg_"
termination_policies = [
"OldestInstance"
]
default_cooldown = 30
health_check_grace_period = 30
max_size = var.max_instances
min_size = var.min_instances
desired_capacity = var.desired_capacity
launch_configuration = aws_launch_configuration.ecs_config_launch_config.name
protect_from_scale_in = false
lifecycle {
create_before_destroy = true
}
vpc_zone_identifier = [
aws_subnet.primary.id,
aws_subnet.secondary.id
]
tag {
key = "AmazonECSManaged"
value = true
propagate_at_launch = true
}
}
resource "aws_ecs_capacity_provider" "ec2_cluster" {
name = "EC2"
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.ecs_cluster.arn
managed_termination_protection = "DISABLED"
managed_draining = "ENABLED"
managed_scaling {
status = "DISABLED"
}
}
}
resource "aws_ecs_cluster_capacity_providers" "uber_providers" {
cluster_name = aws_ecs_cluster.uber.name
capacity_providers = [
"FARGATE",
aws_ecs_capacity_provider.ec2_cluster.name
]
default_capacity_provider_strategy {
base = 1
weight = 100
capacity_provider = aws_ecs_capacity_provider.ec2_cluster.name
}
}