You are on page 1of 5

Create a VPC  

 ==> 10.0.0.0/24

resource “aws_vpc” “VPC-01” {


cidr_block = “10.0.0.0/24”
}

Create a two Subnets


      SN1: 10.0.0.0/25  ==> ap-southeast-1a
 SN2: 10.0.0.128/25 ==> ap-southeast-1b

resource “aws_subnet” “SN1” {


vpc_id = aws_vpc.VPC-01.id
cidr_block = “10.0.0.0/25”
availability_zone = “ap-southeast-1a”
map_public_ip_on_launch = “true”
}

resource “aws_subnet” “SN2” {


vpc_id = aws_vpc.VPC-01.id
cidr_block = “10.0.0.128/25”
availability_zone = “ap-southeast-1b”
}

Make Sn1 as a Public

Create a internet gateway attach to VPC-01

resource “aws_internet_gateway” “IGW”{


vpc_id = aws_vpc.VPC-01.id
}

Create a Route table in VPC-01 Edit routes and attach internet gateway

resource “aws_route_table” “RT1” {


vpc_id = aws_vpc.VPC-01.id

route {
cidr_block = “0.0.0.0/0”
gateway_id = aws_internet_gateway. IGW.id
}
}

Edit subnet association and attached SN1

resource “aws_route_table_association” “RTA” {


subnet_id = aws_subnet.SN1.id
route_table_id = aws_route_table.RT1.id

Create NATGATEway and attach SN2


Create NAT gateway

subnet selection
connectivity type
elastic ip allocation ID
create routetable RT2
attach nat gateway in RT2 and associate SN2

resource "aws_eip" "EIP" {


vpc = true

resource “aws_nat_gateway” “MYNAT” {


subnet_id = aws_subnet.SN1.id
connectivity_type = “public”
allocation_id = aws_eip.EIP.id
}

resource “aws_route_table” “RT2” {


vpc_id = aws_vpc.VPC-01.id

route {
cidr_block = “0.0.0.0/0”
nat_gateway_id = aws_nat_gateway. MYNAT.id
}
}

Edit subnet association and attached SN2

resource “aws_route_table_association” “RTA2” {


subnet_id = aws_subnet.SN2.id
route_table_id = aws_route_table.RT2.id

Launch ec2 Instance in Public Subnet

resource "aws_instance" "I-01" {


ami = “ami-02ee763250491e04a”
instance_type = "t2.micro"
subnet_id = aws_subnet.SN1.id

tags = {
Name = "New instance"
}
}
Print Public IP of the Instance

output “Public_IP” {
value= aws_instance.I-01.public_ip
}
# Create a VPC    ==> 10.0.0.0/16

resource “aws_vpc” “vpc-01” {


cidr_block = “10.0.0.0/16”
tags = {
name = “vpc-01”
}
}

# Create a one public and one private Subnet in Each AZ

resource “aws_subnet” “SN1” {


vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.0.0/25”
availability_zone = “ap-southeast-1a”
map_public_ip_on_launch = “true”

tags = {
Name = “SN1”
}
}

resource “aws_subnet” “SN2” {


vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.0.128/25”
availability_zone = “ap-southeast-1a”
tags = {
Name = “SN2”
}
}

resource “aws_subnet” “SN3” {


vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.1.0/25”
availability_zone = “ap-southeast-1b”
map_public_ip_on_launch = “true”

tags = {
Name = “SN3”
}
}

resource “aws_subnet” “SN4” {


vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.1.128/25”
availability_zone = “ap-southeast-1b”
tags = {
Name = “SN4”
}
}
resource “aws_subnet” “SN5” {
vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.2.0/25”
availability_zone = “ap-southeast-1c”
map_public_ip_on_launch = “true”

tags = {
Name = “SN5”
}
}

resource “aws_subnet” “SN6” {


vpc_id = aws_vpc.vpc-01.id
cidr_block = “10.0.2.128/25”
availability_zone = “ap-southeast-1c”
tags = {
Name = “SN6”
}
}

resource “aws_internet_gateway” “IGW”{


vpc_id = aws_vpc.vpc-01.id

resource “aws_eip” “eip”{


vpc = true

resource “aws_nat_gateway” “mynat {


subnet_id = aws_subnet.SN1.id
connectivity_type = “public”
allocation_id = aws_eip.eip.id
}

resource “aws_route_table” “RT1”{


vpc_id = aws_vpc.vpc-01.id

route {
cidr_block = “10.0.0.0/0”
gateway_id = aws_internet_gateway.IGW.id
}

resource “aws_route_table” “RT2”{


vpc_id = aws_vpc.vpc-01.id

route {
cidr_block = “10.0.0.0/0”
gateway_id = aws_nat_gateway.mynat.id
}
resource “aws_route_table_association” “public-1” {
subnet_id = aws_subnet.SN1.id
route_table_id = aws_route_table.RT1.id

resource “aws_route_table_association” “public-2” {


subnet_id = aws_subnet.SN3.id
route_table_id = aws_route_table.RT1.id

resource “aws_route_table_association” “public-3” {


subnet_id = aws_subnet.SN5.id
route_table_id = aws_route_table.RT1.id

resource “aws_route_table_association” “private1” {


subnet_id = aws_subnet.SN2.id
route_table_id = aws_route_table.RT2.id

resource “aws_route_table_association” “private2” {


subnet_id = aws_subnet.SN4.id
route_table_id = aws_route_table.RT2.id

resource “aws_route_table_association” “private3” {


subnet_id = aws_subnet.SN6.id
route_table_id = aws_route_table.RT2.id

Create NATGATE way and attach to Private Subnets

You might also like