Task — 4 | NAT Gateway to provide the internet access to instances running in the private subnet.

ADITYA RAJ
5 min readSep 16, 2020

--

NAT Gateway to provide the internet access to instances running in the private subnet.

  • Task- 4
  • Performing the following steps:

1. Write an Infrastructure as code using terraform, which automatically create a VPC.

2. In that VPC we have to create 2 subnets:

1. public subnet [ Accessible for Public World! ]

2. private subnet [ Restricted for Public World! ]

3. Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.

4. Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.

5. Create a NAT gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC in the public network

6. Update the routing table of the private subnet, so that to access the internet it uses the nat gateway created in the public subnet

7. Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 sothat our client can connect to our wordpress site. Also attach the key to instance for further login into it.

8. Launch an ec2 instance which has MYSQL setup already with security group allowing port 3306 in private subnet so that our wordpress vm can connect with the same. Also attach the key with the same.

************************************************************************

* Cli Command .

1 ) Firstly Initialize Terraform by,

terraform init

2) Now, run terraform Code by,

terraform apply

Now , here Wordpress instance creating

Now, here Routing Table is created

Now, here Security Group is created

here Subnet is created,

here ,vpc Created,

here successfully Created ,

  • Lets See in GUI.

Successfully Created

************************************************************************

#Source_Code

Step 1 : Create code using terraform for Infrastructure.

Step 2: Create profile .

provider "aws" {
region = "ap-south-1"
profile = "task"
}

Step 2: Create Instances with Wordpress and Provide Key.

#WORDPRESS_IMAGE
resource "aws_instance" "wordpress" {
ami = "ami-000cbce3e1b899ebd"
instance_type = "t2.micro"
associate_public_ip_address = true
subnet_id = aws_subnet.public.id
key_name = "rajaditya"
vpc_security_group_ids = [aws_security_group.webserver.id]
tags = {
Name = "wordpress-aditya"
}
}

Step 3: Create Second Instance with MySql And Provide Key.

#MYSQL_IMAGE
resource "aws_instance" "mysql" {
ami = "ami-0019ac6129392a0f2"
instance_type = "t2.micro"
subnet_id = aws_subnet.private.id
vpc_security_group_ids = [aws_security_group.database.id]
key_name = "rajaditya"

tags = {
Name = "mysql-aditya"
}
}

Step 4: Create Security Groups for Wordpress With Port 80.

#WEBSERVER_SECURITYGROUP
resource "aws_security_group" "webserver" {
name = "for_wordpress"
description = "Allow hhtp"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
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"]
}
tags = {
Name = "securitygroup"
}
}

Step 5: Create Security Groups For Database .

#securitygroup_FOR_DATABASE
resource "aws_security_group" "database" {
name = "for_MYSQL"
description = "Allow MYSQL"
vpc_id = "${aws_vpc.myvpc.id}"
ingress {
description = "MYSQL"
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [aws_security_group.webserver.id]

}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "securitygourp"
}
}

Step 6: Create VPC.

#Creating_VPC
resource "aws_vpc" "myvpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true
tags = {
Name = "aditya-vpc"

}
}

Step 7: Create Routing Table and NAT gateway.

#ROUTETABLE
resource "aws_route_table" "internetgateway" {
vpc_id = "${aws_vpc.myvpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}

tags = {
Name = "route-table"
}
}
resource "aws_route_table_association" "asstopublic" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.internetgateway.id
}
resource "aws_eip" "nat" {
vpc=true

}
resource "aws_nat_gateway" "nat-gw" {
allocation_id = "${aws_eip.nat.id}"
subnet_id = "${aws_subnet.public.id}"
depends_on = [aws_internet_gateway.gw]
tags = {
Name = "net-gateway"
}
}
resource "aws_route_table" "private" {
vpc_id = "${aws_vpc.myvpc.id}"
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.nat-gw.id}"
}

tags = {
Name = "database"
}
}
resource "aws_route_table_association" "nat"
{
subnet_id = aws_subnet.private.id
route_table_id = aws_route_table.private.id
}

Step 8 : Create Subnet Public.

#SUBNET_PUBLIC
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "192.168.0.0/24"
availability_zone = "ap-south-1a"
tags = {
Name = "public-subnet"
}
}

Step 9: Create Subnet Private.

#SUBNET_PRIVATE
resource "aws_subnet" "private" {
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "192.168.1.0/24"
availability_zone = "ap-south-1b"
tags = {
Name = "private-subnet"
}
}

Step 10: Create INTERNET GATEWAY.

#INTERNETGATEWAY
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.myvpc.id}"
tags = {
Name = "internet-gateway"
}
}

Here your Code is created of Terraform .

********* Thank you Vimal Daga Sir *********

**************************************************************************

Full Source Code Is here ======>>>>

Github link : — https://github.com/rajadityaranjan/hybrid-multi-cloud-computing-task---4.git

************************************************************

--

--