-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
165 lines (131 loc) · 3.88 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
resource "aws_security_group" "this" {
name_prefix = "${var.name}-${var.environment}-cluster-ecs-"
vpc_id = data.aws_vpc.this.id
tags = {
Name = "${var.name}-${var.environment}-cluster-ecs"
Network = var.vpc_name
Terraform = "terraform-aws-cluster"
}
}
resource "aws_vpc_security_group_ingress_rule" "this_this" {
security_group_id = aws_security_group.this.id
ip_protocol = "-1"
referenced_security_group_id = aws_security_group.this.id
tags = {
Name = "${var.name}-${var.environment}-cluster-ecs"
Network = var.vpc_name
Terraform = "terraform-aws-cluster"
}
}
resource "aws_vpc_security_group_ingress_rule" "this_lb" {
security_group_id = aws_security_group.this.id
ip_protocol = "-1"
referenced_security_group_id = aws_security_group.lb.id
tags = {
Name = "${var.name}-${var.environment}-cluster-ecs"
Network = var.vpc_name
Terraform = "terraform-aws-cluster"
}
}
resource "aws_vpc_security_group_egress_rule" "this_all" {
security_group_id = aws_security_group.this.id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "-1"
tags = {
Name = "${var.name}-${var.environment}-cluster-ecs"
Network = var.vpc_name
Terraform = "terraform-aws-cluster"
}
}
resource "aws_ecs_cluster" "this" {
name = "${var.name}-${var.environment}"
setting {
name = "containerInsights"
value = "disabled"
}
}
resource "aws_iam_role" "this" {
assume_role_policy = data.aws_iam_policy_document.this_assume_role.json
name = "${var.name}-${var.environment}-cluster-ecs"
}
resource "aws_iam_role_policy_attachment" "service_role" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
role = aws_iam_role.this.name
}
resource "aws_iam_instance_profile" "this" {
name = "${var.name}-${var.environment}-cluster-ecs"
role = aws_iam_role.this.name
}
resource "aws_launch_template" "this" {
name_prefix = "${var.name}-${var.environment}-cluster-ecs-"
image_id = data.aws_ami.this.id
instance_type = var.instance_type
key_name = "${var.name}-${var.environment}-cluster-ecs"
block_device_mappings {
device_name = "/dev/xvda"
ebs {
delete_on_termination = true
volume_size = 50
volume_type = "gp2"
}
}
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
instance_market_options {
market_type = var.market_type
}
network_interfaces {
associate_public_ip_address = false
security_groups = [
aws_security_group.this.id,
data.aws_security_group.this_private.id
]
}
user_data = base64encode(templatefile("${path.module}/user_data.tpl", {
cluster_name = aws_ecs_cluster.this.name
}))
}
resource "aws_autoscaling_group" "this" {
desired_capacity = 1
max_size = 1
min_size = 1
launch_template {
id = aws_launch_template.this.id
version = "$Latest"
}
instance_refresh {
strategy = "Rolling"
triggers = ["tag"]
preferences {
min_healthy_percentage = 50
}
}
vpc_zone_identifier = data.aws_subnets.this_private.ids
tag {
key = "AmazonECSManaged"
propagate_at_launch = true
value = "true"
}
}
resource "aws_ecs_capacity_provider" "this" {
name = "${var.name}-${var.environment}"
auto_scaling_group_provider {
auto_scaling_group_arn = aws_autoscaling_group.this.arn
managed_scaling {
status = "DISABLED"
}
}
depends_on = [
aws_security_group.lb,
]
}
resource "aws_ecs_cluster_capacity_providers" "this" {
capacity_providers = [aws_ecs_capacity_provider.this.name]
cluster_name = aws_ecs_cluster.this.name
default_capacity_provider_strategy {
base = 1
capacity_provider = aws_ecs_capacity_provider.this.name
weight = 100
}
}