Instance Launch in Public & Private Subnet Using NAT Gateways (AWS, Terraform)
This article is similar to as my previous article , The only difference is that in private subnet it used NAT Gateways which doesn’t allow public world to access the web but Private subnet instance can access the web.
If you haven’t checked out my article , I recommend you to first go through it : Launch Instances in Public & Private Subnets Using Terraform
What is NAT Gateway?
NAT Gateway is a highly available AWS service which connect to the Internet from instances within a private subnet in a VPC.
Problem statement :
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.
Initialize Provider (AWS) and Create VPC
- First you have to mention provider which could be AWS, Azure , gcp etc and then you have to initialize using terraform init as mention below.
provider "aws" {
region = "ap-south-1"
profile = "samkit"
}resource "aws_vpc" "main" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = "true"
tags = {
Name = "myvpc1"
}
}
Subnet Creation
Create public and private subnet
resource "aws_subnet" "mysubnetpublic" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "192.168.1.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = "true"
tags = {
Name = "mysubnet-1a"}}resource "aws_subnet" "mysubnetprivate" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "192.168.0.0/24"
availability_zone = "ap-south-1b"
tags = {
Name = "mysubnet-1b"
}
}
Create Elastic IP for AWS:
With this EIP you can create a static IP for your instance which would remain same throught.
resource "aws_eip" "lb" {
instance = "${aws_instance.os2.id}"
vpc = true
}
Create NAT Gateway:
Here we are mentioning private subnet for which we want NAT Gateway accessibility.
resource "aws_nat_gateway" "gw" {
allocation_id = "${aws_eip.lb.id}"
subnet_id = "${aws_subnet.mysubnetprivate.id}"
tags = {
Name = "gw NAT"
}
}
Internet Gateway (IGW) , Route Table & Launch Instance
4. Create IGW and attached it to VPC. Set a rule in the route table for subnets and launch instances.
resource "aws_internet_gateway" "gw" {vpc_id = "${aws_vpc.main.id}"tags = {Name = "myvpcgw"}}resource "aws_route_table" "r" {vpc_id = "${aws_vpc.main.id}"route {cidr_block = "0.0.0.0/0"gateway_id = "${aws_internet_gateway.gw.id}"}tags = {Name = "MyGatewayRoute"}}resource "aws_route_table_association" "a" {subnet_id = aws_subnet.mysubnetpublic.idroute_table_id = aws_route_table.r.id}resource "aws_route_table_association" "b" {subnet_id = aws_subnet.mysubnetprivate.idroute_table_id = aws_route_table.r.id}resource "aws_security_group" "sg" {name = "myterravpcfirewall"description = "Allow inbound traffic"vpc_id = "${aws_vpc.main.id}"ingress {description = "HTTP"from_port = 80to_port = 80protocol = "tcp"cidr_blocks = ["0.0.0.0/0"]}ingress {description = "SSH"from_port = 22to_port = 22protocol = "tcp"cidr_blocks = ["0.0.0.0/0"]}ingress {description = "TCP"from_port = 3306to_port = 3306protocol = "tcp"cidr_blocks = ["0.0.0.0/0"]}egress {from_port = 0to_port = 0protocol = "-1"cidr_blocks = ["0.0.0.0/0"]}}resource "aws_instance" "os1" {ami = "ami-052c08d70def0ac62"instance_type = "t2.micro"key_name = "key1"vpc_security_group_ids = [aws_security_group.sg.id]subnet_id = "subnet-021abe62bd46ff22f"tags = {Name = "WordPressOS"}}resource "aws_instance" "os2" {ami = "ami-08706cb5f68222d09"instance_type = "t2.micro"key_name = "key1"vpc_security_group_ids = [aws_security_group.sg.id]subnet_id = "subnet-09b05e9356f9656bf"tags = {Name = "MySqlOS"}}
After writing all the terraform code you just have to write :
terraform apply -auto-approve
Finally our instance will be launched
Thanks for reading !