This repository provides the code and documentation for modernizing the deployment and scaling of a web application using Terraform on Amazon Web Services (AWS) infrastructure. The primary objective is to implement a three-tier architecture using Infrastructure-as-Code (IaC) principles with Terraform.
- What is Terraform?
- What is Infrastructure as Code (IaC)?
- How Terraform Works
- Basic Terraform Commands
- Basic Terraform Terms
- Understanding the Architecture
- Environment Setup Prerequisites
- Steps to Create the Architecture
- How to Run this Code
- Troubleshooting
- Infrastructure Automation: Key Use Cases
- Scaling and Automating with Terraform
- Conclusion
Terraform is an open-source Infrastructure-as-Code (IaC) tool developed by HashiCorp. It allows you to define and manage infrastructure through code, automatically creating, updating, or deleting resources to match your desired state. This approach brings predictability, version control, and automation to infrastructure management.
IaC automates infrastructure provisioning and management with code. Instead of manual configurations, tools like Terraform enable you to define infrastructure using code that's versioned, testable, and ensures consistent and repeatable deployments.
Terraform follows a simple three-step process:
- Write: Define your infrastructure in Terraform configuration files.
- Plan: Terraform generates an execution plan that shows what will change in your infrastructure.
- Apply: Execute the plan to create, update, or delete resources accordingly.
terraform init
: Initializes your Terraform environment.terraform plan
: Generates an execution plan to show what changes will be applied.terraform apply
: Executes the plan to create or modify resources.terraform destroy
: Destroys the created infrastructure.
- Variables in Terraform: Placeholders for dynamic values in your configurations, making them flexible and reusable.
- Resources in Terraform: Infrastructure components you manage, such as EC2 instances, VPCs, and databases.
A three-tier architecture divides an application into three interconnected layers: the presentation tier, application tier, and data tier. In our case, this architecture consists of a custom Virtual Private Cloud (VPC), Internet Gateway, subnets, EC2 instances, route tables, NAT Gateway, and an RDS database.
To get started, you'll need the following:
- AWS Account Access Keys: You must have an AWS account and access credentials. Create keys in the AWS IAM section.
- AWS CLI Installation: Install the AWS Command Line Interface (CLI) to interact with AWS services.
- Terraform Installation: Install Terraform, an infrastructure-as-code tool by HashiCorp.
- AWS Profile Configuration: Use the AWS CLI to set up a profile with your access keys and preferred region for deployment.
- VS Code Installation: Install Visual Studio Code (VS Code) for code editing.
Follow these steps to create the architecture using Terraform, as outlined in the code provided:
- Define the provider and region.
- Create a custom VPC.
- Create subnets.
- Set up security groups for EC2 instances and RDS.
- Create EC2 instances.
- Set up an Internet Gateway.
- Create a route table for the public subnet.
- Define a route for the public subnet.
- Associate the custom route table with the public subnet.
- Set up a NAT Gateway.
- Create an RDS database.
- Create an Elastic IP.
- Save the provided code in a .tf file.
- Open a terminal.
- Run
terraform init
. - Run
terraform plan
. - Run
terraform apply
to create the infrastructure. - To destroy the infrastructure, run
terraform destroy
.
If you encounter issues:
- Ensure Terraform is installed and environment variables are set correctly.
- Check the security group rules and network ACLs.
- Verify route table settings for the public subnet.
- Ensure instances have public IPs or Elastic IPs.
- Double-check route table associations and instance statuses.