Day 66: Mastering Infrastructure Deployment on AWS with Terraform

Day 66: Mastering Infrastructure Deployment on AWS with Terraform

Introduction: In this comprehensive guide, we'll explore how to leverage Terraform to automate the deployment of AWS infrastructure. By following a step-by-step approach, you'll learn how to create a Virtual Private Cloud (VPC), subnets, internet gateway, launch an EC2 instance with Apache web server, and associate an Elastic IP—all through Infrastructure as Code (IaC) techniques using Terraform.

Table of Contents:

  1. Introduction to Terraform and AWS Infrastructure as Code

  2. Setting Up Terraform Environment and Providers

  3. Task 1: Creating a Virtual Private Cloud (VPC)

  4. Task 2: Establishing Private and Public Subnets

  5. Task 3: Configuring Internet Connectivity with an Internet Gateway

  6. Task 4: Routing Traffic with Route Tables

  7. Task 5: Managing Network Security with Security Groups

  8. Task 6: Allocating Elastic IPs for EC2 Instances

  9. Task 7: Deploying EC2 Instances with User Data Scripts

  10. Task 8: Hosting Websites on EC2 Instances

  11. Conclusion and Further Learning

Task:

  1. Create a VPC (Virtual Private Cloud):

    • Create a vpc.tf file.

    • Define the VPC with CIDR block 10.0.0.0/16.

    • Execute terraform apply to create the VPC.

  2. Create a Public Subnet:

    • Update the subnet.tf file.

    • Define the public subnet with CIDR block 10.0.1.0/24.

    • Execute terraform apply to create the public subnet.

  3. Create a Private Subnet:

    • Update the subnet.tf file.

    • Define the private subnet with CIDR block 10.0.2.0/24.

    • Execute terraform apply to create the private subnet.

  4. Create an Internet Gateway (IGW):

    • Create an internet_gateway.tf file.

    • Define the IGW and attach it to the VPC.

    • Execute terraform apply to create the IGW.

  5. Create a Route Table:

    • Create a route_table.tf file.

    • Define the route table for the public subnet and associate it.

    • Execute terraform apply to create the route table.

  6. Create a Security Group:

    • Create a security_group.tf file.

    • Define the security group to allow SSH and HTTP access from anywhere.

    • Execute terraform apply to create the security group.

  7. Create an Elastic IP (EIP):

    • Create an elastic_ip.tf file.

    • Define the EIP and associate it with the EC2 instance.

    • Execute terraform apply to create the EIP.

  8. Create User Data for Apache Installation:

    • Update the userdata.sh script.

    • Use a shell script to install Apache and host a simple website.

    • Save the script.

  9. Launch an EC2 Instance:

    • Update the ec2_instance.tf file.

    • Define the EC2 instance with the specified details (AMI, instance type, security group, user data).

    • Execute terraform apply to launch the EC2 instance.

  10. Access the Website:

    • Navigate to the EC2 instance's public IP address in a web browser.

    • Verify that the website hosted on the EC2 instance is accessible.

Conclusion: By following these steps, you'll successfully deploy an AWS infrastructure using Terraform, showcasing your proficiency in Infrastructure as Code (IaC) techniques. This hands-on project demonstrates your ability to automate infrastructure deployment and manage cloud resources efficiently with Terraform on AWS.