forked from GonzaloROD/DevopsPinFinal
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsetup.tf
124 lines (105 loc) · 2.73 KB
/
setup.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
provider "aws" {
region = "us-east-1"
}
#Get Linux AMI ID using SSM Parameter endpoint in us-east-1
#data "aws_ssm_parameter" "webserver-ami" {
# name = "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
#}
#Create VPC in us-east-1
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "terraform-vpc"
}
}
#Get all available AZ's in VPC for master region
data "aws_availability_zones" "azs" {
state = "available"
}
#Create subnet # 1 in us-east-1
resource "aws_subnet" "subnet" {
availability_zone = element(data.aws_availability_zones.azs.names, 0)
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
}
#Create IGW in us-east-1
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
}
#Create route table in us-east-1
resource "aws_route_table" "my_route_table" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "My_Route_Table"
}
}
# Agregar una ruta predeterminada (0.0.0.0/0) a la tabla de rutas
resource "aws_route" "default_route" {
route_table_id = aws_route_table.my_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
# Asociar la tabla de rutas a la Subnet
resource "aws_route_table_association" "subnet_association" {
subnet_id = aws_subnet.subnet.id
route_table_id = aws_route_table.my_route_table.id
}
#Create SG for allowing TCP/80 & TCP/22
resource "aws_security_group" "sg" {
name = "sg"
description = "Allow TCP/80 & TCP/22"
vpc_id = aws_vpc.vpc.id
ingress {
description = "Allow SSH traffic"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow NGINX Exporter traffic"
from_port = 9113
to_port = 9113
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow cadvisor traffic"
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow prometheus traffic"
from_port = 9090
to_port = 9090
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow Grafana traffic"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "allow traffic NGINX from TCP/80"
from_port = 80
to_port = 80
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"]
}
}
output "Webserver-Public-IP" {
value = aws_instance.webserver.public_ip
}