-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
257 lines (223 loc) · 6.55 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
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
locals {
app_name_and_env = "${var.app_name}-${local.app_env}"
app_env = var.app_env
db_host = module.rds.address
db_password = random_password.db_root.result
account = data.aws_caller_identity.this.account_id
region = data.aws_region.current.name
}
/*
* Create user for CI/CD to perform ECS actions
*/
resource "aws_iam_user" "cd" {
count = var.create_cd_user ? 1 : 0
name = "cd-${local.app_name_and_env}"
}
resource "aws_iam_access_key" "cd" {
count = var.create_cd_user ? 1 : 0
user = aws_iam_user.cd[0].name
}
resource "aws_iam_user_policy" "cd" {
count = var.create_cd_user ? 1 : 0
name = "ecs_deployment"
user = aws_iam_user.cd[0].name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"ecr:GetAuthorizationToken",
"ecs:DeregisterTaskDefinition",
"ecs:DescribeTaskDefinition",
"ecs:ListTaskDefinitions",
"ecs:RegisterTaskDefinition",
],
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ecs:DescribeServices",
"ecs:UpdateService",
]
Resource = "arn:aws:ecs:*:${local.account}:service/${module.ecsasg.ecs_cluster_name}/${module.ecs.service_name}"
},
{
Effect = "Allow"
Action = [
"ecs:DescribeTasks",
"ecs:StopTask",
]
Resource = "arn:aws:ecs:*:${local.account}:task/${module.ecsasg.ecs_cluster_name}/*"
},
{
Effect = "Allow"
Action = [
"ecs:ListTasks",
]
"Effect" : "Allow",
"Condition" : {
"ArnEquals" : {
"ecs:cluster" : "arn:aws:ecs:*:${local.account}:cluster/${module.ecsasg.ecs_cluster_name}"
}
}
Resource = "*"
},
{
Effect = "Allow"
Action = [
"ecs:StartTask",
]
Resource = "arn:aws:ecs:*:${local.account}:task-definition/${module.ecs.task_def_family}:*"
},
{
Effect = "Allow"
Action = [
"iam:PassRole",
]
Resource = module.ecsasg.ecsServiceRole_arn
},
]
})
}
/*
* Create Cloudwatch log group
*/
resource "aws_cloudwatch_log_group" "logs" {
name = local.app_name_and_env
retention_in_days = var.log_retention_in_days
}
/*
* Create target group for ALB
*/
resource "aws_alb_target_group" "tg" {
name = substr("tg-${local.app_name_and_env}", 0, 32)
port = "80"
protocol = "HTTP"
vpc_id = module.vpc.id
deregistration_delay = "30"
stickiness {
type = "lb_cookie"
}
dynamic "health_check" {
for_each = [var.health_check]
content {
enabled = try(health_check.value.enabled, null)
healthy_threshold = try(health_check.value.healthy_threshold, null)
interval = try(health_check.value.interval, null)
matcher = try(health_check.value.matcher, null)
path = try(health_check.value.path, null)
port = try(health_check.value.port, null)
protocol = try(health_check.value.protocol, null)
timeout = try(health_check.value.timeout, null)
unhealthy_threshold = try(health_check.value.unhealthy_threshold, null)
}
}
}
/*
* Create listener rule for hostname routing to new target group
*/
resource "aws_alb_listener_rule" "tg" {
listener_arn = module.alb.https_listener_arn
priority = "218"
action {
type = "forward"
target_group_arn = aws_alb_target_group.tg.arn
}
condition {
host_header {
values = ["${var.subdomain}.${var.domain_name}"]
}
}
}
/*
* Create cloudwatch dashboard for service
*/
module "ecs-service-cloudwatch-dashboard" {
count = var.create_dashboard ? 1 : 0
source = "silinternational/ecs-service-cloudwatch-dashboard/aws"
version = "~> 3.1"
cluster_name = module.ecsasg.ecs_cluster_name
dashboard_name = "${local.app_name_and_env}-${local.region}"
service_names = [var.app_name]
}
/*
* Create RDS root password
*/
resource "random_password" "db_root" {
length = 16
# this list is the same as the default list with only '@' removed
override_special = "!#$%&*()-_=+[]{}<>:?"
}
/*
* Create an RDS database
*/
module "rds" {
source = "github.com/silinternational/terraform-modules//aws/rds/mariadb?ref=8.13.3"
app_name = var.app_name
app_env = local.app_env
ca_cert_identifier = var.rds_ca_cert_identifier
db_name = var.database_name
db_root_user = var.database_user
db_root_pass = local.db_password
subnet_group_name = module.vpc.db_subnet_group_name
security_groups = [module.vpc.vpc_default_sg_id]
allocated_storage = 20 // 20 gibibyte
instance_class = "db.t3.micro"
multi_az = true
}
/*
* Optional Adminer database manager
*/
module "adminer" {
count = var.create_adminer ? 1 : 0
source = "silinternational/adminer/aws"
version = "~> 1.1"
adminer_default_server = module.rds.address
app_name = var.app_name
app_env = var.app_env
vpc_id = module.vpc.id
alb_https_listener_arn = module.alb.https_listener_arn
subdomain = "adminer-${local.app_name_and_env}"
cloudflare_domain = var.domain_name
ecs_cluster_id = module.ecsasg.ecs_cluster_id
ecsServiceRole_arn = module.ecsasg.ecsServiceRole_arn
alb_dns_name = module.alb.dns_name
enable = var.enable_adminer
}
/*
* Create new ecs service
*/
module "ecs" {
source = "github.com/silinternational/terraform-modules//aws/ecs/service-only?ref=8.13.3"
cluster_id = module.ecsasg.ecs_cluster_id
service_name = var.app_name
service_env = local.app_env
container_def_json = var.container_def_json
desired_count = var.desired_count
tg_arn = aws_alb_target_group.tg.arn
lb_container_name = "hub"
lb_container_port = "80"
ecsServiceRole_arn = module.ecsasg.ecsServiceRole_arn
}
/*
* Create Cloudflare DNS record
*/
resource "cloudflare_record" "dns" {
count = var.create_dns_record ? 1 : 0
zone_id = data.cloudflare_zone.this.id
name = var.subdomain
value = module.alb.dns_name
type = "CNAME"
proxied = true
allow_overwrite = var.dns_allow_overwrite
}
data "cloudflare_zone" "this" {
name = var.domain_name
}
/*
* AWS data
*/
data "aws_caller_identity" "this" {}
data "aws_region" "current" {}