-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
165 lines (165 loc) · 4.32 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
provider "aws" {
region = "us-east-2"
}
resource "aws_ecr_repository" "image_repo" {
name = "website"
}
resource "aws_ecs_cluster" "cluster" {
name = "website"
}
resource "aws_ecs_task_definition" "task" {
family = "website-task"
container_definitions = <<DEFINITION
[
{
"name": "website-task",
"image": "${aws_ecr_repository.image_repo.repository_url}",
"essential": true,
"portMappings": [
{
"containerPort": 3000,
"hostPort": 3000
}
],
"memory": 512,
"cpu": 256
}
]
DEFINITION
requires_compatibilities = ["FARGATE"]
network_mode = "awsvpc"
memory = 512
cpu = 256
execution_role_arn = aws_iam_role.iamWebsite.arn
}
resource "aws_default_vpc" "default_vpc" {}
resource "aws_default_subnet" "default_subnet_a" {
availability_zone = "us-east-2a"
}
resource "aws_default_subnet" "default_subnet_b" {
availability_zone = "us-east-2b"
}
resource "aws_ecs_service" "website-service" {
depends_on = [
aws_lb_listener.listener,
aws_lb_listener.listener_https
]
name = "website-service"
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.task.arn
launch_type = "FARGATE"
desired_count = 1
force_new_deployment = true
load_balancer {
target_group_arn = aws_lb_target_group.tg.arn
container_name = aws_ecs_task_definition.task.family
container_port = 3000
}
network_configuration {
subnets = ["${aws_default_subnet.default_subnet_a.id}", "${aws_default_subnet.default_subnet_b.id}"]
assign_public_ip = true
security_groups = ["${aws_security_group.svc_sg.id}"]
}
}
resource "aws_iam_role" "iamWebsite" {
name = "iamWebsite"
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
}
resource "aws_alb" "load_balancer" {
name = "website-lb"
load_balancer_type = "application"
subnets = ["${aws_default_subnet.default_subnet_a.id}", "${aws_default_subnet.default_subnet_b.id}"]
security_groups = ["${aws_security_group.lb_sg.id}"]
}
resource "aws_security_group" "svc_sg" {
ingress {
from_port = 0
to_port = 0
protocol = "-1"
security_groups = ["${aws_security_group.lb_sg.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "lb_sg" {
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_lb_target_group" "tg" {
name = "website-tg"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_default_vpc.default_vpc.id
health_check {
matcher = "200,301,302"
path = "/"
}
}
resource "aws_acm_certificate" "cert" {
domain_name = "nickburnett.dev"
validation_method = "DNS"
}
resource "aws_lb_listener" "listener" {
depends_on = [
aws_lb_target_group.tg
]
load_balancer_arn = aws_alb.load_balancer.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = 443
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "listener_https" {
depends_on = [
aws_lb_target_group.tg
]
load_balancer_arn = aws_alb.load_balancer.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_acm_certificate.cert.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.tg.arn
}
}
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ecs-tasks.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "iamWebsite_policy" {
role = aws_iam_role.iamWebsite.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}