Automate The Process Of Deploying Web Application On EC2 Using IAC Tool Terraform
EC2, IAM, S3, AWS CLI, Terraform
Introduction:
This blog will guide you through a comprehensive process of deploying a web application on AWS EC2 using Terraform. We will cover the necessary prerequisites, step-by-step actions, and best practices to ensure a seamless deployment experience.
Prerequisites:
To get started, you will need the following:
AWS CLI: The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services.
Terraform: An open-source infrastructure as code software tool that provides a consistent CLI workflow for managing cloud service.
A S3 bucket: Amazon S3 has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web.
IAM User with Admin Access: An IAM user with administrative access to your AWS services.
Any IDE (VS Code): An integrated development environment (IDE) of your choice. In this guide, we will be using Visual Studio Code (VS Code).
Actions:
1. Create Access Keys:
First, we need to create access keys with an IAM user who has admin access for using all AWS services. Here’s how:
2. Configure AWS CLI:
Next, we need to configure the AWS CLI. Here’s how:
Run the following command in your terminal:
aws configure
Provide the AWS Access Key and AWS Secret Access Key we created in the previous step -> choose your desired region -> press enter.
3. Create SSH Keys:
Now, we need to create SSH keys. Here’s how:
Run the following command in your terminal:
ssh-keygen
Give “newkey” as the key name then press enter until it’s done.
4. Create Provider.tf File:
Create a file named provider.tf
with the following code:
provider "aws" {
region = "ap-south-1"
}
5. Create Var.tf File:
Now, we need to create a file called var.tf
for storing variables and paste this script:
variable "AMI" {
default = {
"ap-south-1" = "ami-0e670eb768a5fc3d4"
"us-east-1" = "ami-0e670eb67534r56k4"
}
}
variable "REGION" {
default = "ap-south-1"
}
variable "TYPE" {
default = "t2.micro"
}
variable "KEY" {
default = "linuxkey"
}
6. Create Instance.tf File:
Next, we need to create a file called instance.tf
for creating an instance and a new key pair and paste the following code in it:
resource "aws_key_pair" "new-key" {
key_name = "newkey"
public_key = file("newkey.pub")
}
resource "aws_instance" "name" {
ami = var.AMI[var.REGION]
instance_type = var.TYPE
key_name = aws_key_pair.new-key.key_name
tags = {
"Name" = "demo"
}
provisioner "file" {
source = "./web.sh"
destination = "/tmp/web.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/web.sh",
"sudo /tmp/web.sh"
]
}
connection {
user = "ec2-user"
private_key = file("newkey")
host = self.public_ip
}
provisioner "local-exec" {
command = "echo aws_instance.name.public_key >> public.txt"
}
}
output "publicIP" {
value = aws_instance.name.public_ip
}
7. Create Backend.tf File:
Create a file named backend.tf
to store Terraform state in a remote location:
Create a folder in S3 bucket and give it's name as key.
terraform {
backend "s3" {
bucket = "<bucket-name>"
key = "<folder-name>"
region = "ap-south-1"
}
}
8. Create Web.sh File:
Next, we need to create a bash script to execute/run our app on the EC2 Instance. Create a file called web.sh
and paste the following content in it:
#!/bin/bash
sudo yum update -y
sudo yum install httpd wget unzip -y
sudo wget https://www.tooplate.com/zip-templates/2135_mini_finance.zip
sudo unzip 2135_mini_finance.zip
sudo cp -r 2135_mini_finance/* /var/www/html/
sudo systemctl restart httpd
9. Create Infrastructure:
Now, we are ready to create our infrastructure. Here’s how:
Run the following command to initialize the AWS provider:
terraform init
Run the following command to validate there is no syntax error:
terraform validate
Run the following command to format all Terraform files code:
terraform fmt
Run the following command to see what this command will do after run:
terraform plan
Run the following command to apply all changes/create infrastructure:
terraform apply -auto-approve
10. Access the Application:
Open port 80 in the instance`s security group.
Obtain the public IP of the EC2 instance.
Paste the public IP into a web browser to access your application.
11. Destroy Infrastructure:
- Remove the infrastructure:
terraform destroy
Conclusion:
By following these steps, you have successfully deployed a web application on AWS EC2 using Terraform. This guide provides a comprehensive overview of the process, from creating prerequisites to accessing the application. By leveraging Terraform's powerful automation capabilities, you can streamline your infrastructure management and ensure consistent and secure deployments.