This guide provides a step-by-step approach to launching, connecting to, and configuring a webserver on AWS using Terraform. You'll learn how to configure Terraform, manage AWS resources, and ensure a successful deployment.
Before getting started, ensure you have the following installed and configured:
- 🛠 Terraform - Installed and configured.
- ☁️ AWS CLI - Installed and configured with your access and secret keys using:
aws configure
- 🔧 Git - Installed to manage your Terraform code repository.
Begin by launching an EC2 instance via the AWS Management Console or using a predefined Terraform configuration.
Connect to your EC2 instance securely via SSH using the appropriate key pair.
If Terraform is not already installed, follow the installation steps. Also, ensure that AWS CLI is properly set up:
# Install Terraform (if not installed)
# Configure AWS CLI
aws configure
Install Git on your EC2 instance to manage your Terraform configuration files:
sudo yum install git -y
git --version
git config --global user.name "your_username"
git config --global user.email "your_email@example.com"
Clone the repository containing the Terraform configurations:
git clone https://github.com/YogitaBadhe/aws-terraform-webserver ☁️
cd aws-terraform-webserver ☁️
Alternatively, create the project files manually using PowerShell:
mkdir terraformproject
cd .\terraformproject\
New-Item main.tf
New-Item variables.tf
code .
mkdir aws
cd aws
mkdir aws_keys
cd .\aws_keys\
Create an AWS key pair named mywebserver.pem
through the AWS Management Console. Save it in the aws/aws_keys/
directory and set proper permissions:
chmod 400 aws/aws_keys/mywebserver.pem
Modify the main.tf
file to include:
- AWS region settings.
- VPC ID, Subnet ID, and AMI ID.
- Key pair name for SSH access.
Also, update variables.tf
to include the key pair name.
Run the following Terraform commands to deploy your resources:
terraform init
terraform validate
terraform fmt
terraform plan
terraform apply
Once Terraform completes the deployment, retrieve the public IP address of the instance and verify the webserver by accessing it via a web browser.
To remove all deployed resources, run:
terraform destroy
Save your changes and push them to a GitHub repository:
git add .
git commit -m "Initial commit"
git push origin main
For private repositories, use your GitHub token:
git push https://token@github.com/username/terraform-webserver.git
🎉 Congratulations! You've successfully deployed and configured a webserver on AWS using Terraform! 🚀