Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC) Techniques(Interview Questions)
Welcome back to Terraform journey.
In the previous tasks, we have learned about the basics of Terraform, its configuration file, and creating an EC2 instance using Terraform. Today, we will explore more about Terraform and create multiple resources.
PermalinkTask:
Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16
Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.
Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.
Create an Internet Gateway (IGW) and attach it to the VPC.
Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.
Launch an EC2 instance in the public subnet with the following details:
AMI: ami-0557a15b87f6559cf
Instance type: t2.micro
Security group: Allow SSH access from anywhere
User data: Use a shell script to install Apache and host a simple website
Create an Elastic IP and associate it with the EC2 instance.
Open the website URL in a browser to verify that the website is hosted successfully.
PermalinkSolution
Will write our main.tf file with below code and add resource to same file onward as per task requirements.
Step 1: Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16
VPC got created.
Step 2: Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.
Step 3: Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.
In next code we are adding Public and Private subnet.
We could see new subnet in AWS console.
Step 4: Create an Internet Gateway (IGW) and attach it to the VPC.
Step 5: Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.
The aws_route_table
block initiates the creation of a route table in the specified VPC using the vpc_id
attribute. It outlines a route that directs all traffic with a destination CIDR of "0.0.0.0/0" to the internet gateway identified by the gateway_id
attribute. The tags
attribute assigns a user-friendly name ("route-table") for easy identification.
Following this, the aws_route_table_association
block links the newly created route table with a public subnet, identified by the subnet_id
attribute. The route_table_id
attribute references the ID of the route table created in the preceding block.
Step 6: Launch an EC2 instance in the public subnet with the following details:
AMI: ami-0557a15b87f6559cf
Instance type: t2.micro
Security group: Allow SSH access from anywhere
User data: Use a shell script to install Apache and host a simple website
We could see that instance got created as per our script.
Step 8: Create an Elastic IP and associate it with the EC2 instance.
Now if we see our final script will looks like below.
PermalinkThis Terraform hands-on task is designed to test our proficiency in using Terraform for Infrastructure as Code (IaC) on AWS. You will be tasked with creating a VPC, subnets, an internet gateway, and launching an EC2 instance with a web server running on it. This task will showcase your skills in automating infrastructure deployment using Terraform. It's a popular interview question for companies looking for candidates with hands-on experience in Terraform. That's it for today.
And most importantly, Don't forget to destroy...!
Thanks for reading.
Thanks,
Kishor Chavan