Creating VPC with public and private subnet and provisioning of NAT gateway for private subnets using Terraform
Onkar Naik
DevOps @Forescout ?? | Google Developer Expert | AWS | DevOps | 3X GCP | 1X Azure | 1X Terraform | Ansible | Kubernetes | SRE | Platform | Jenkins | Tech Blogger ??
!! Hello Connections !!
Welcome you all to my article which is based on TASK 4 of Hybrid Multi Cloud Computing .
Basically , TASK 4 is just an upgraded version of TASK 3 .As it includes the update of provisioning NAT gateway for private subnet connectivity to public world .
TASK DESCRIPTION : Perform TASK 4 with same use case of WordPress and mysql but with an additional feature to be added that is NAT Gateway to provide the internet access to instances running in the private subnet .
So, let us start our workflow journey for completion of the task .
In these task we are going to implement same use case using terraform code as we done in previous TASK 3 ,but in these task we have to provision one gateway which gives access to private subnet to connect to public internet world for any update purpose but nobody from public world cannot go inside .These gateway that we are provisioning for private subnet is called NAT gateway .
1)First we have to login through aws CLI using aws configure command .
2)We have to create one VPC for our entire networking setup of infrastructure . But before running our terraform code we have to initialize it for downloading necessary plugins using terraform init command .
#terraform init
By running terraform code using terraform apply VPC is created for our infrastructure .
#terraform apply
3)After creating VPC , we have to write terraform code for creating two types of subnets like public subnet for WordPress and private subnet for mysql database server .
4)Now for the connectivity of the subnets like to go in and go out we have to create one public facing gateway called internet gateway . By which outside clients can connect to our WordPress application .
5)For internet gateway we have to configure one routing table which provides ip range to DHCP internally and attaching these routing table to public subnet .
6)To permit outside connectivity and also to do SSH to WordPress instance we have to create one security group for WordPress instance in public subnet which allows SSH on port 22 , WordPress server on port 80 and ICMP . Also for mysql database instance we have to create one security group which allows only port 3306 of mysql database server and security group of WordPress instance for their internal connectivity.
7)Now we have to create the WordPress and mysql instances using precreated AMI that has already WordPress and mysql set up ready .
8)Finally we do all the terraform code setup and successfully implemented also .We can see whole setup of WordPress and mysql using terraform state list command .
#terraform state list
9)By using public ip of which is auto assigned to WordPress instance we can access WordPress application running under fully secured own created network setup using terraform
10)Till these step the workflow is same as like TASK 3 .Now we have to create one instance in public subnet which has main provision of going inside the mysql instance in private subnet via SSH . These instance is known as Bastion_Host . But before creating bastion_host we have to create one security group for it for SSH . Another security group we have to create for mysql instance in private subnet which allows only Bastion_Host instance to go inside .
Security groups for Bastion_Host and for mysql which allows Bastion_Host login --->
Creating Bastion_Host instance in public subnet -->
11)For creating NAT gateway we require one static ip which is Elastic IP . It is a public ip given to NAT gateway by using these IP our database instance in private subnet can go outside to public world .
12)Now , we have to create NAT gateway for mysql instance running in private subnet .
13)At last we create routing table for NAT gateway and attaching it to mysql instance .Now our mysql instance can go outside to internet .
14)By running one single terraform code entire setup of the task is created . It is nothing but we are creating Netoworking As A Service (NAAS) by using terraform .
#terraform state list
In these way finally I completed TASK 4 of Hybrid Multi Cloud Computing .
Special thanks to Mr .Vimal Daga for giving such amazing real use case based task which enhances my technical skills in the world of hybrid multi cloud computing .
To get these powerful terraform code please go through my github repo .I shared the github repo link in my post .
Thanking you all for visiting my article
Senior DevOps Engineer @Deltek
4 年Awesome ..
Associate Consultant at Capgemini
4 年Awesome?
Full Stack Web Developer | Angular | Node.js | AWS | DevOps | Immediate Joiner
4 年Good work...
SDE
4 年Nicely done