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.tf
file.Define the VPC with CIDR block
10.0.0.0/16
.Execute
terraform apply
to create the VPC.
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.
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.
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.
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.
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.
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.
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.
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.
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.