Day 66: Mastering Infrastructure Deployment on AWS with Terraform

"DevOps enthusiast and aspiring engineer. Currently honing skills in streamlining development workflows and automating infrastructure. Learning AWS, Docker, Kubernetes, Python, and Ansible. Eager to contribute and grow within the DevOps community."
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:
Introduction to Terraform and AWS Infrastructure as Code
Setting Up Terraform Environment and Providers
Task 1: Creating a Virtual Private Cloud (VPC)
Task 2: Establishing Private and Public Subnets
Task 3: Configuring Internet Connectivity with an Internet Gateway
Task 4: Routing Traffic with Route Tables
Task 5: Managing Network Security with Security Groups
Task 6: Allocating Elastic IPs for EC2 Instances
Task 7: Deploying EC2 Instances with User Data Scripts
Task 8: Hosting Websites on EC2 Instances
Conclusion and Further Learning
Task:
Create a VPC (Virtual Private Cloud):
Create a
vpc.tffile.Define the VPC with CIDR block
10.0.0.0/16.
Execute
terraform applyto create the VPC.
Create a Public Subnet:
Update the
subnet.tffile.Define the public subnet with CIDR block
10.0.1.0/24.
Execute
terraform applyto create the public subnet.
Create a Private Subnet:
Update the
subnet.tffile.Define the private subnet with CIDR block
10.0.2.0/24.
Execute
terraform applyto create the private subnet.
Create an Internet Gateway (IGW):
Create an
internet_gateway.tffile.Define the IGW and attach it to the VPC.

Execute
terraform applyto create the IGW.
Create a Route Table:
Create a
route_table.tffile.
Define the route table for the public subnet and associate it.
Execute
terraform applyto create the route table.
Create a Security Group:
Create a
security_group.tffile.Define the security group to allow SSH and HTTP access from anywhere.

Execute
terraform applyto create the security group.
Create an Elastic IP (EIP):
Create an
elastic_ip.tffile.Define the EIP and associate it with the EC2 instance.

Execute
terraform applyto create the EIP.
Create User Data for Apache Installation:
Update the
userdata.shscript.Use a shell script to install Apache and host a simple website.
Save the script.

Launch an EC2 Instance:
Update the
ec2_instance.tffile.Define the EC2 instance with the specified details (AMI, instance type, security group, user data).

Execute
terraform applyto launch the EC2 instance.
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.




