DEPLOYING WORDPRESS AND MYSQL ON CUSTOM VPC WITH NAT USING TERRAFORM



If you check my previous story, you can see that it had a similar to this one. The only difference is that the instances in the private subnet(MySQL) do not have any internet connectivity. This story will show you how to setup the whole thing with internet connectivity on both private and public subnets.


Internet connectivity on private subnets can be achieved using NAT gateways.


NAT Gateway

A NAT gateway gives cloud resources without public IP addresses access to the internet without exposing those resources to incoming internet connections.

Pre-requisites:-

  1. AWS account

  2. AWS CLI installed and configured

  3. Terraform installed and added to path

  4. VSCode with HashiCorp Terraform and Terraform Autocomplete plugins installed

Process:-

1. First we need to create the provider. Our provider is AWS therefore, we select AWS with other relevant information like the credentials.

provider "aws" 
{
region                  = "ap-south-1"
shared_credentials_file = "C:/Users/KIIT/.aws/credentials"
profile                 = "chirag"
}

2. Next we shall create the key pairs which shall be used to remotely login to the instances.

resource "tls_private_key" "keypair" {
algorithm   = "RSA"
}
resource "local_file" "privatekey" {
content     = tls_private_key.keypair.private_key_pemfilename = "key1.pem"
}
resource "aws_key_pair" "deployer" {
key_name   = "key1.pem"
public_key = tls_private_key.keypair.public_key_openssh
}

3. We shall now start with the VPC creation. The VPC will have many parts namely subnets, ip-routing tables for each subnet, internet gateway for the public subnet, nat gateway for the private subnet, etc.

resource "aws_vpc" "main" {
cidr_block       = "192.168.0.0/16"
instance_tenancy = "default"tags = {
Name = "myvpc"
}
}