This README provides an overview of a Terraform script that helps you set up a basic AWS infrastructure, including a Virtual Private Cloud (VPC), subnets, security groups, and auto-scaling configurations to host an Nginx web server.
Before using this Terraform script, ensure that you have the following prerequisites:
Terraform installed on your local machine. AWS credentials configured with the necessary permissions. An existing AWS account.
This Terraform script automates the creation of the following AWS resources:
VPC and Subnets: It defines a VPC with two subnets – one public and one private – in different availability zones within the specified region.
Internet Gateway: An internet gateway is attached to the VPC to allow public network access.
Route Table: A route table is created to route traffic through the internet gateway, ensuring internet connectivity for the public subnet.
NAT Gateway: A Network Address Translation (NAT) gateway is set up in the public subnet to allow instances in the private subnet to initiate outbound connections to the internet.
Security Group: A security group named "nginx-sg" is created to control inbound traffic to the EC2 instances. It allows incoming traffic on port 80 (HTTP) from anywhere (0.0.0.0/0).
Auto Scaling Configuration: This script defines an Auto Scaling Launch Configuration for EC2 instances. It installs Nginx, configures a simple web page, and sets up a health-check endpoint.
Auto Scaling Group: An Auto Scaling Group is created to manage the number of instances based on desired capacity, minimum, and maximum capacity settings.
Auto Scaling Policy: A scaling policy is defined to trigger scaling based on CPU utilization.
CloudWatch Alarm: An alarm is set up to monitor CPU utilization, and it triggers the scaling policy when utilization exceeds a specified threshold.
Load Balancer (ALB): An Application Load Balancer (ALB) is created, which balances incoming traffic between the instances in the private subnet.
Target Group: A target group is set up to define health checks and routing for the ALB.
SSH Key Pair: An SSH key pair is generated for EC2 instance access.
Clone this repository to your local machine:
git clone https://github.com/your-repo/terraform-aws-nginx
cd terraform-aws-nginx
Create a terraform.tfvars file in the same directory to provide values for variables defined in the script:
region = "us-west-2" # Replace with your desired AWS region
ami = "ami-0123456789abcdef0" # Replace with your desired Amazon Machine Image (AMI)
instance_type = "t2.micro" # Replace with your desired EC2 instance type
initial_capacity = 2 # Set the initial number of instances
min_capacity = 2 # Minimum instances
max_capacity = 4 # Maximum instances
cpu_threshold = 80 # Set the CPU utilization threshold for scaling
Initialize Terraform and apply the configuration:
terraform init
terraform apply
Review the execution plan and confirm with 'yes' to apply the changes.
To destroy the resources created by this Terraform script, run the following command:
terraform destroy
Confirm the action with 'yes' when prompted.
This Terraform script simplifies the process of setting up a basic AWS infrastructure for hosting an Nginx web server with auto-scaling capabilities. Customize the script according to your requirements and use it as a foundation for your AWS IaC projects.
Additional Resources:
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Please replace https://github.com/devopshubproject/tf-aws-ha
with the URL of your Git repository if you have one. This README provides a comprehensive guide to your Terraform project and can be extended or modified as needed.