In this blog we are going to configure high availability and secure VPC
and will launch EC2 Instances which will communicate each others.
Below is the diagram of what we are planning to do. We will create an
architecture with the following AWS resources.
- One VPC
- Two public subnets
- Two private subnets
- Two security groups
- One internet gateway
- One Elastic IP address
- One NAT gateway
- Two EC2 instances
.tf file structure shown below.
- provider.tf
- vpc.tf
- public-subnet.tf
- private-subnet.tf
- internet-gateway.tf
- nat-gateway.tf
- route-tables.tf
- route-associations.tf
- security-groups.tf
- ssh-key.tf
- instances.tf
- vars.tf
Step 1 – Create a AWS Provider – provider.tf
Step 2 – Create a simple VPC configuration – vpc.tf
Let’s quickly look at what we are trying to achieve
-
CIDR Block is provided as a variable. Refer to the whole section
of cidr blocks above.
-
instance_tenancy – Tells AWS that we want to create our EC2
instances on shared infrastructure by default.
-
enable_dns_support – This when set to true means that AWS Route 53
to resolve the hostnames to correct IP addresses.
-
enable_dns_hostnames – This means AWS will provide the EC2
instance with a public address with a public hostname based on IP
address.
-
Name – This is a special tag which gives name to the VPC.
Step 3 – Create private subnets – private-subnets.tf
For the purpose of this blog entry we will create two private
subnets
Step 4 – Create public subnets – public-subnets.tf
Step 5 – Create internet gateway – internet-gateway.tf
Step 6 – Create NAT gateway – nat-gateway.tf
Creation of NAT gateway is a slightly more involved process as
it requires a public IP address. It requires us to do the following
- Create an elastic IP address
- Create the NAT gateway
- Assign a public IP address
- Assign a public subnet
-
Associate an internet gateway to access the internet
Step 7 – Create route tables – route-tables.tf
Step 8 – Associate routes with subnets – route-associations.tf
Each subnet is now associated with a route via a route association
resource.
-
Public subnets are assigned Internet gateway. It means all the
resources in the public subnet will be accessiable from the internet for
the servers given in CIDR range for the internet gateway.
-
Privates subnets are assigned NAT Gateway if their resource need
to access internet but no servers on the internet would be able to access
the resources inside the subnet.
Step 9 – Create security groups for EC2 instances –
security-groups.tf
The security groups are configured in such a way that. The
security group have the following configuration
Only resources
inside of the public security group would be able to access the resources
inside the private security group.
Both security groups only allow SSH
connections.
Resources inside public security group can be accessed
from a certain public IP address
Step 10 – Create SSH file – ssh-key.tf
Step 11 – Create EC2 instances – instances.tf
Finally, the last step is to create EC2 instances which
will reside inside the subnets of the VPC and would be associated with
security groups.
Step 12 – Create A variable file – vars.tf
Demo :
Step 1 – Run terraform init/validate/plan/apply
Commands
Step 2 – SSH into the public instance
Step 3 –
SSH into private instance from public instance
No comments:
Post a Comment
testing