-
Notifications
You must be signed in to change notification settings - Fork 30
/
ecs.tf
194 lines (158 loc) · 4.62 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
locals {
repository_url = "ghcr.io/jimmysawczuk/sun-api"
}
# We need a cluster in which to put our service.
resource "aws_ecs_cluster" "app" {
name = "app"
}
# An ECR repository is a private alternative to Docker Hub.
resource "aws_ecr_repository" "sun_api" {
name = "sun-api"
}
# Log groups hold logs from our app.
resource "aws_cloudwatch_log_group" "sun_api" {
name = "/ecs/sun-api"
}
# The main service.
resource "aws_ecs_service" "sun_api" {
name = "sun-api"
task_definition = aws_ecs_task_definition.sun_api.arn
cluster = aws_ecs_cluster.app.id
launch_type = "FARGATE"
desired_count = 1
load_balancer {
target_group_arn = aws_lb_target_group.sun_api.arn
container_name = "sun-api"
container_port = "3000"
}
network_configuration {
assign_public_ip = false
security_groups = [
aws_security_group.egress_all.id,
aws_security_group.ingress_api.id,
]
subnets = [
aws_subnet.private_d.id,
aws_subnet.private_e.id,
]
}
}
# The task definition for our app.
resource "aws_ecs_task_definition" "sun_api" {
family = "sun-api"
container_definitions = <<EOF
[
{
"name": "sun-api",
"image": "${local.repository_url == "" ? aws_ecr_repository.sun_api.repository_url : local.repository_url}:latest",
"portMappings": [
{
"containerPort": 3000
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-region": "us-east-1",
"awslogs-group": "/ecs/sun-api",
"awslogs-stream-prefix": "ecs"
}
}
}
]
EOF
execution_role_arn = aws_iam_role.sun_api_task_execution_role.arn
# These are the minimum values for Fargate containers.
cpu = 256
memory = 512
requires_compatibilities = ["FARGATE"]
# This is required for Fargate containers (more on this later).
network_mode = "awsvpc"
}
# This is the role under which ECS will execute our task. This role becomes more important
# as we add integrations with other AWS services later on.
# The assume_role_policy field works with the following aws_iam_policy_document to allow
# ECS tasks to assume this role we're creating.
resource "aws_iam_role" "sun_api_task_execution_role" {
name = "sun-api-task-execution-role"
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume_role.json
}
data "aws_iam_policy_document" "ecs_task_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
# Normally we'd prefer not to hardcode an ARN in our Terraform, but since this is an AWS-managed
# policy, it's okay.
data "aws_iam_policy" "ecs_task_execution_role" {
arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# Attach the above policy to the execution role.
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role" {
role = aws_iam_role.sun_api_task_execution_role.name
policy_arn = data.aws_iam_policy.ecs_task_execution_role.arn
}
resource "aws_lb_target_group" "sun_api" {
name = "sun-api"
port = 3000
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.app_vpc.id
health_check {
enabled = true
path = "/health"
}
depends_on = [aws_alb.sun_api]
}
resource "aws_alb" "sun_api" {
name = "sun-api-lb"
internal = false
load_balancer_type = "application"
subnets = [
aws_subnet.public_d.id,
aws_subnet.public_e.id,
]
security_groups = [
aws_security_group.http.id,
aws_security_group.https.id,
aws_security_group.egress_all.id,
]
depends_on = [aws_internet_gateway.igw]
}
resource "aws_alb_listener" "sun_api_http" {
load_balancer_arn = aws_alb.sun_api.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_alb_listener" "sun_api_https" {
load_balancer_arn = aws_alb.sun_api.arn
port = "443"
protocol = "HTTPS"
certificate_arn = aws_acm_certificate.sun_api.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.sun_api.arn
}
}
output "alb_url" {
value = "http://${aws_alb.sun_api.dns_name}"
}
resource "aws_acm_certificate" "sun_api" {
domain_name = "sun-api.jimmysawczuk.net"
validation_method = "DNS"
}
output "domain_validations" {
value = aws_acm_certificate.sun_api.domain_validation_options
}