Terraform Interview Questions Answers

Vanshika SharmaVanshika Sharma
6 min read

Table of contents

Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16:

To create a Virtual Private Cloud (VPC) with a CIDR block of 10.0.0.0/16 Using AWS CLI, follow these steps:

Step 1: Create a VPC

Run the following command in the AWS CLI to create a VPC with the specified CIDR block:

aws ec2 create-vpc --cidr-block 10.0.0.0/16

This will return a JSON output containing the VPC ID required for further configurations.

Step 2: Tag the VPC (Optional)

To assign a name to the VPC, use the following command:

aws ec2 create-tags --resources <VPC-ID> --tags Key=Name,Value=MyVPC

Replace <VPC-ID> with the actual VPC ID from the previous step.

Step 3: Enable DNS Hostnames (Optional)

If you need public DNS hostnames for instances in the VPC, enable it using:

aws ec2 modify-vpc-attribute --vpc-id <VPC-ID> --enable-dns-hostnames "{\"Value\":true}"

Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC:

To create a public subnet with a CIDR block 10.0.1.0/24 In the VPC you just created, follow these steps:

Step 1: Get the VPC ID

If you don't have the VPC ID, you can retrieve it with:

aws ec2 describe-vpcs --filters "Name=cidr-block,Values=10.0.0.0/16" --query "Vpcs[0].VpcId" --output text

Save the output as <VPC-ID> for the next steps.

Step 2: Create the Subnet

Run the following command, replacing <VPC-ID> with your actual VPC ID:

aws ec2 create-subnet --vpc-id <VPC-ID> --cidr-block 10.0.1.0/24 --availability-zone us-east-1a
  • Replace us-east-1a with your preferred Availability Zone.

Step 3: Tag the Subnet (Optional)

To assign a name to the subnet:

aws ec2 create-tags --resources <SUBNET-ID> --tags Key=Name,Value=PublicSubnet

Replace <SUBNET-ID> with the actual Subnet ID from the previous step.

Step 4: Modify Subnet to Make It Public

By default, subnets are private. To make this subnet public, enable auto-assign public IP:

aws ec2 modify-subnet-attribute --subnet-id <SUBNET-ID> --map-public-ip-on-launch

Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC:

To create a private subnet with CIDR block 10.0.2.0/24 In the same VPC, follow these steps:

Step 1: Get the VPC ID

If you don’t have the VPC ID, retrieve it using:

aws ec2 describe-vpcs --filters "Name=cidr-block,Values=10.0.0.0/16" --query "Vpcs[0].VpcId" --output text

Save the output as <VPC-ID>.

Step 2: Create the Private Subnet

Run the following command, replacing <VPC-ID> with your actual VPC ID:

aws ec2 create-subnet --vpc-id <VPC-ID> --cidr-block 10.0.2.0/24 --availability-zone us-east-1b
  • Replace us-east-1b with your preferred Availability Zone.

Step 3: Tag the Subnet (Optional)

To assign a name to the private subnet:

aws ec2 create-tags --resources <SUBNET-ID> --tags Key=Name,Value=PrivateSubnet

Replace <SUBNET-ID> with the actual Subnet ID from the previous step.

Step 4: Verify Auto-Assign Public IP is Disabled

Since this is a private subnet, make sure instances do not get public IPs:

aws ec2 modify-subnet-attribute --subnet-id <SUBNET-ID> --no-map-public-ip-on-launch

Create an Internet Gateway (IGW) and attach it to the VPC:

To create an Internet Gateway (IGW) and attach it to your VPC, follow these steps:


Step 1: Create an Internet Gateway

Run the following command to create an Internet Gateway:

aws ec2 create-internet-gateway

This will return a JSON output containing the Internet Gateway ID (igw-xxxxxxxxxxxxx). Save this IGW ID for the next step.


Step 2: Attach the IGW to the VPC

Attach the Internet Gateway to your VPC by replacing <IGW-ID> and <VPC-ID> with the actual IDs:

aws ec2 attach-internet-gateway --internet-gateway-id <IGW-ID> --vpc-id <VPC-ID>

Step 3: Tag the Internet Gateway (Optional)

To assign a name to the IGW:

aws ec2 create-tags --resources <IGW-ID> --tags Key=Name,Value=MyInternetGateway

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:

To create a Route Table for the public subnet and associate it with the Internet Gateway (IGW), follow these steps:


Step 1: Create a Route Table

Run the following command, replacing <VPC-ID> with your actual VPC ID:

aws ec2 create-route-table --vpc-id <VPC-ID>

This will return a JSON output containing the Route Table ID (rtb-xxxxxxxxxxxxx). Save this Route Table ID.


Step 2: Add a Route to the Internet Gateway

Add a route in the Route Table to allow public internet access. Replace <RTB-ID> with the actual Route Table ID and <IGW-ID> with the Internet Gateway ID:

aws ec2 create-route --route-table-id <RTB-ID> --destination-cidr-block 0.0.0.0/0 --gateway-id <IGW-ID>

This directs all outbound traffic (0.0.0.0/0) to the Internet Gateway.


Step 3: Associate the Route Table with the Public Subnet

To associate the Route Table with your Public Subnet, replace <SUBNET-ID> with the actual Subnet ID:

aws ec2 associate-route-table --route-table-id <RTB-ID> --subnet-id <SUBNET-ID>

Step 4: Tag the Route Table (Optional)

To name the Route Table, use:

aws ec2 create-tags --resources <RTB-ID> --tags Key=Name,Value=PublicRouteTable

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.

    To complete this setup, follow these steps:


    Step 1: Create a User Data Script

    This script will install Apache and host a simple webpage on the EC2 instance.

      #!/bin/bash
      sudo yum update -y
      sudo yum install -y httpd
      sudo systemctl start httpd
      sudo systemctl enable httpd
      echo "<h1>Welcome to My Website - Hosted on AWS EC2</h1>" | sudo tee /var/www/html/index.html
    

    Step 2: Launch the EC2 Instance with User Data

    Run the following AWS CLI command, replacing <SUBNET-ID> and <SG-ID>:

      aws ec2 run-instances --image-id ami-0557a15b87f6559cf \
          --count 1 \
          --instance-type t2.micro \
          --key-name MyKeyPair \
          --subnet-id <SUBNET-ID> \
          --associate-public-ip-address \
          --security-group-ids <SG-ID> \
          --user-data file://user-data.sh
    

Step 3: Allocate an Elastic IP

To create an Elastic IP:

    aws ec2 allocate-address

This will return an Allocation ID (eipalloc-xxxxxxxxxxxxxx). Save this ID.


Step 4: Associate the Elastic IP with the EC2 Instance

Retrieve the Instance ID:

    aws ec2 describe-instances --filters "Name=image-id,Values=ami-0557a15b87f6559cf" --query "Reservations[0].Instances[0].InstanceId" --output text

Now, associate the Elastic IP with the instance:

    aws ec2 associate-address --instance-id <INSTANCE-ID> --allocation-id <EIP-ALLOC-ID>

Replace <INSTANCE-ID> and <EIP-ALLOC-ID> with actual values.


Step 5: Open the Website in a Browser

Retrieve the Elastic IP:

    aws ec2 describe-addresses --query "Addresses[0].PublicIp" --output text

Copy the Elastic IP and open it in a browser:

    http://<Elastic-IP>

If everything is set up correctly, you should see:

    Welcome to My Website - Hosted on AWS EC2
2
Subscribe to my newsletter

Read articles from Vanshika Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vanshika Sharma
Vanshika Sharma

I am currently a B.Tech student pursuing Computer Science with a specialization in Data Science at I.T.S Engineering College. I am always excited to learn and explore new things to increase my knowledge. I have good knowledge of programming languages such as C, Python, Java, and web development.