-
Notifications
You must be signed in to change notification settings - Fork 73
/
setup.tf
93 lines (81 loc) · 1.98 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
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"
}
}
#Create IGW in us-east-1
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
}
#Get main route table to modify
data "aws_route_table" "main_route_table" {
filter {
name = "association.main"
values = ["true"]
}
filter {
name = "vpc-id"
values = [aws_vpc.vpc.id]
}
}
#Create route table in us-east-1
resource "aws_default_route_table" "internet_route" {
default_route_table_id = data.aws_route_table.main_route_table.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "Terraform-RouteTable"
}
}
#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 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 traffic 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
}