-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
147 lines (131 loc) · 3.38 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
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = {
name = "main"
}
}
resource "aws_subnet" "subnet" {
vpc_id = aws_vpc.main.id
count = length(var.availability_zones)
cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 8,count.index ) ## takes 10.0.0.0/16 --> 10.0.1.0/24
map_public_ip_on_launch = true
availability_zone = element(var.availability_zones, count.index)
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name=var.name
}
}
resource "aws_route_table" "rt" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
}
resource "aws_route_table_association" "subnet_route" {
count = length(var.availability_zones)
subnet_id = aws_subnet.subnet[count.index].id
route_table_id = aws_route_table.rt.id
}
resource "aws_security_group" "sg" {
name = var.sg_name
vpc_id = aws_vpc.main.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
self = "false"
cidr_blocks = ["0.0.0.0/0"]
description = "http"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Create Application Load Balancer
resource "aws_lb" "alb" {
name = var.alb_name
internal = false
load_balancer_type = "application"
subnets = aws_subnet.subnet.*.id
security_groups = [aws_security_group.sg.id]
}
resource "aws_lb_target_group" "tg" {
name = var.tg_name
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
target_type = "ip"
depends_on = [aws_lb.alb]
}
resource "aws_lb_listener" "listener" {
load_balancer_arn = aws_lb.alb.arn
port = "80"
protocol = "HTTP"
default_action {
target_group_arn = aws_lb_target_group.tg.arn
type = "forward"
}
}
############CREATING A ECS CLUSTER#############
resource "aws_ecs_cluster" "cluster" {
name = var.cluste_name
setting {
name = "containerInsights"
value = "enabled"
}
tags = {
Environment = var.Environment
}
}
resource "aws_ecs_task_definition" "task" {
family = var.family
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE", "EC2"]
cpu = 512
memory = 2048
container_definitions = <<DEFINITION
[
{
"name" : "nginx",
"image" : "nginx:1.23.1",
"cpu" : 512,
"memory" : 2048,
"essential" : true,
"portMappings" : [
{
"containerPort" : 80,
"hostPort" : 80
}
]
}
]
DEFINITION
}
resource "aws_ecs_service" "service" {
name = var.service_name
count = length(var.availability_zones)
cluster = aws_ecs_cluster.cluster.id
task_definition = aws_ecs_task_definition.task.id
desired_count = 1
launch_type = "FARGATE"
platform_version = "LATEST"
load_balancer {
target_group_arn = aws_lb_target_group.tg.arn
container_name = "nginx"
container_port = 80
}
network_configuration {
assign_public_ip = true
security_groups = [aws_security_group.sg.id]
subnets = [aws_subnet.subnet[count.index].id]
}
lifecycle {
ignore_changes = [task_definition]
}
}