This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.tf
156 lines (122 loc) · 3.4 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
locals {
cluster_name = "${var.name}-${var.environment}"
}
#
# ECS
#
resource "aws_ecs_cluster" "main" {
name = local.cluster_name
lifecycle {
create_before_destroy = true
}
}
#
# IAM
#
# Docs
# https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html
data "aws_iam_policy_document" "ecs_instance_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ecs_instance_role" {
name = "ecs-instance-role-${local.cluster_name}"
assume_role_policy = data.aws_iam_policy_document.ecs_instance_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "ecs_instance_role_policy" {
count = var.use_AmazonEC2ContainerServiceforEC2Role_policy ? 1 : 0
role = aws_iam_role.ecs_instance_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_instance_profile" "ecs_instance_profile" {
name = "ecsInstanceRole-${local.cluster_name}"
path = "/"
role = aws_iam_role.ecs_instance_role.name
}
#
# Security Group
#
resource "aws_security_group" "main" {
name = "asg-${local.cluster_name}"
description = "${local.cluster_name} ASG security group"
vpc_id = var.vpc_id
tags = {
Environment = var.environment
Automation = "Terraform"
}
}
resource "aws_security_group_rule" "main" {
description = "All outbound"
security_group_id = aws_security_group.main.id
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
#
# EC2
#
resource "aws_launch_configuration" "main" {
name_prefix = format("ecs-%s-", local.cluster_name)
iam_instance_profile = aws_iam_instance_profile.ecs_instance_profile.name
instance_type = var.instance_type
image_id = var.image_id
associate_public_ip_address = false
security_groups = concat(var.security_group_ids, [aws_security_group.main.id])
root_block_device {
volume_type = "standard"
}
ebs_block_device {
device_name = "/dev/xvdcz"
volume_type = "standard"
encrypted = true
}
user_data = <<EOF
#!/bin/bash
# The cluster this agent should check into.
echo 'ECS_CLUSTER=${aws_ecs_cluster.main.name}' >> /etc/ecs/ecs.config
# Disable privileged containers.
echo 'ECS_DISABLE_PRIVILEGED=true' >> /etc/ecs/ecs.config
EOF
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "main" {
name = "ecs-${local.cluster_name}"
launch_configuration = aws_launch_configuration.main.id
termination_policies = ["OldestLaunchConfiguration", "Default"]
vpc_zone_identifier = var.subnet_ids
desired_capacity = var.desired_capacity
max_size = var.max_size
min_size = var.min_size
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "ecs-${local.cluster_name}"
propagate_at_launch = true
}
tag {
key = "Cluster"
value = local.cluster_name
propagate_at_launch = true
}
tag {
key = "Environment"
value = var.environment
propagate_at_launch = true
}
tag {
key = "Automation"
value = "Terraform"
propagate_at_launch = true
}
}