# Web Application Deployment on AWS Using Terraform, NGINX and Bash Scripting

# Web Application Deployment on AWS Using Terraform, NGINX and Bash Scripting

## 🚀 Overview:

The building of the infrastructure and deploy of a web page on AWS using Terraform aims to create a scalable and resilient infrastructure that leverages the power of Amazon Web Services (AWS) cloud platform. This project utilizes Terraform, an Infrastructure as Code (IaC) tool, to provision and manage the infrastructure components, enabling automation, repeatability, and scalability. This project demonstrates how to **provision, deploy and destroy infrastructure on AWS using Terraform and automate the deployment with bash Scripting**. A **deployment script (**[`deploy.sh`](http://deploy.sh)) and a **destroy script (**[`destroy.sh`](http://destroy.sh)) are use for the automation process. It’s a hands-on DevOps project showing Infrastructure as Code (IaC) and automation.

## 🔧 Problem Statement

Terraform is an IaC software tool that provides a consistent command line interface (CLI) workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files. In this specific case you need to create foundation Networking(VPC, Subnets, route table, IGW, NAT Gateway...), Terraform will automatically use the configuration files to provide the infrastructure resources where we can run application needed. Terraform will use his deployment to provide all AWS needed elements avoiding us to use the console and it will automate the setup, ensuring consistency and reducing human error. The bash scripting and the other hands will facilitate the deploy by simply automating the process.

## 💽 Techonology Stack

The architecture consists of the following three tiers:

* **AWS VPC**: AWS VPC
    
* **Subnets**: AWS Subnets
    
* **Route table**: AWS route table
    
* **NACL**: AWS NACL
    
* **Internet Gateway**: AWS IGW
    
* **Elastic Load Balancer**: AWS ELB
    

## 📌 Architecture Diagram

┌──────────────────────────┐

│ Developers │ │ Push Code to GitHub │

└─────────────┬────────────┘

┌──────────────────────────┐

│ Terraform Configuration files │

\- [main.tf](http://main.tf)

\- [providers.tf](http://providers.tf)

\- [variables.tf](http://variables.tf)

└─────────────┬────────────┘ ┌────────────────────────────┐

│ Bash Scripting │

\- [Deploy.sh](http://IDeploy.sh)

\- [Destroy.sh](http://Destroy.sh)

└────────────┬────────────────┘ ┌────────────────────────────┐

│ NGINX │

\- Install and configuration

\- Copy index.html

└─────────────┬──────────────┘ ┌────────────────────────────┐

│ Elastic Load Balancer│

\- Distributes traffic

\- Performs health checks

└─────────────┬──────────────┘ ┌────────────────────────────┐

│ End Users │ │ Access via ELB DNS NAME │

└────────────────────────────┘

## 🌟 Project Requirements

Before you get started, make sure you have the following prerequisites in place:

* [Terraform](https://www.terraform.io/) installed on your local machine.
    
* AWS IAM credentials configured in your text editor. In this case we will use VSCODE.
    
* Git installed on your local machine and Github account set up [Github](https://www.github.com/)
    
* Git for cloning the repository.
    

You must also know Terraform workflow

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1715055468795/5726d060-0df2-404e-a0b1-3ce1889018ac.jpeg align="center")

## 📋 Table of Contents

I - **Terraform Configuration files**

[Step 1: Provider Configuration](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Provider-configuration)

[Step 2: Variables Configuration](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-variables-configuration)

[Step 3: Main Configuration](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-VPC-configuration)

[Step 4: Output Configuration](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Output-configuration)

[Step 5: User-data](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Output-configuration)

II - **Scripting files**

[Step 6: Deploy file](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Provider-configuration)

[Step 7: Destroy file](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-variables-configuration)

III - **Instructions of Deployment**

[Step 8: Clone Repository](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Clone-Repository)

[Step 9: Run Deployment](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files)

[Step 10: Destroy Infrastructure](http://127.0.0.1:8000/vpc-foundation-terraform-on-aws/#-Format-Files)

## ✨Terraform Configuration files

You need to write different files generating resources

##### Step 1: ***Provider Configuration***

Here we declare our cloud provider and we specify the region where we will be launching resources

* [provider Configuration](https://github.com/Joebaho/Flipkart-Deploy/blob/main/providers.tf)
    

##### Step 2: ***Variables Configuration***

This is where we declare all variables and their value. It includes

* **Variables**: List of element that can vary or change. They can be reuse values throughout our code without repeating ourselves and help make the code dynamic
    
* **values**: values attributed to each variables.
    

We have

* [variables Configuration](https://github.com/Joebaho/Flipkart-Deploy/blob/main/variables.tf)
    
* [value Configuration](https://github.com/Joebaho/Flipkart-Deploy/blob/main/terraform.tfvars)
    

##### Step 3: ***Main Configuration***

This is where you create the basement, foundation and networking where all the resources will be launch. It includes VPC, Subnets, IGW, Route tables, Security groups, EC2 instances, Targets group and Elastic load balancer

* [Main Configuration](https://github.com/Joebaho/Flipkart-Deploy/blob/main/main.tf)
    

##### Step 4: ***Output Configuration***

Know as Output Value : it is a convenient way to get useful information about your infrastructure printed on the CLI. It is showing the DNS name of the load balancer and public IP of the instances.

* [Output Configuration](https://github.com/Joebaho/Flipkart-Deploy/blob/main/outputs.tf)
    

##### Step 5: ***User-data***

user data is **<mark>a script or custom data that you provide to an EC2 instance to be executed automatically on its first launch</mark>**. It's used to automate the initial setup of an instance, such as installing software, configuring settings, or running commands needed to make the instance ready for use. In our case the user data will update the server, install Nginx and copy the index.html file to the correspondant folder.

* [User-data](https://github.com/Joebaho/Flipkart-Deploy/blob/main/user-data.sh)
    

## ✨Bash Scripting files

You need to write different bash script files generating and destroying resources. The file will contains all step by step meaning all terraform command for the execution of the process.

##### Step 6: ***Deploy file***

Here we declare the initialization of the folder, the validation of the configuration, the plan of resources to be create and apply of the execution where we will be launching resources.

* [Deploy file](https://github.com/Joebaho/Flipkart-Deploy/blob/main/deploy.sh)
    

##### Step 7: ***Destroy file***

This is where we declare the terraform command to destroy all the resources created.

* [Destroy file](https://github.com/Joebaho/Flipkart-Deploy/blob/main/destroy.sh)
    

## 💼 Instructions of Deployment

Follow these steps to deploy the architecture:

##### Step 8: ***Clone Repository:***

Clone the repository in your local machine using the command "git clone" then enter the folder

> git clone [https://github.com/Joebaho/Flipkart-Deploy.git](https://github.com/Joebaho/Flipkart-Deploy.git)
> 
> cd Flipkart-Deploy

##### Step 9: ***Run Deployment***

Initialize the folder containing configuration files that were clone to Terraform and apply the configuration by typing the following command

> chmod +x deploy.sh
> 
> ./deploy.sh

The process will execute all terraform command and display the following steps. You must see this image

Terraform init: Initialize the folder

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212187396/05b6ef33-73f7-4bd0-8fb1-3b70153d8e4e.png align="center")

Terraform fmt & validate: Check for any syntax error and valid the status of the configuration files

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212232658/ff0bd9af-c0d8-4d66-9141-746ecd17a100.png align="center")

Terraform plan: confirm the numbers of resources that will be create

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212266934/75887368-fcfe-4913-a606-1f67c5d343c8.png align="center")

Terraform apply: confirm all resources created

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212291088/342c26f7-6c10-40be-b1d8-2cba159a51e7.png align="center")

Terraform output: show all the output information to be use by user.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212338167/cbf75280-a22b-49d7-b632-034c3b2dcc5f.png align="center")

After copy the ELB dns name in the output section you can go paste that in a new window on the browser and the web page will display.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212445028/0e37e738-648b-4d11-99b9-ffc1c27d7359.png align="center")

##### Step 10: ***Destroy infrastructure***

After you are done with the infrastructure and you are ready to destroy you can run the command. First you make the file executable then you apply execution.

> chmod +x destroy.sh
> 
> ./destroy.sh

After typing or pasting the command you will get images

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212705679/f6070d34-e952-4080-8774-3e7cdcd3f73b.png align="center")

You will need to confirm the destruction of the deployment. Then all the 16 resources created before will be destroyed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1764212781108/6ada8696-a786-496d-ae16-3fa6a6a223f2.png align="center")

## **📌 Learning Outcomes**

* Understand **Terraform basics** (providers, resources, state management)
    
* Automate deployments with **Shell scripting**
    
* Hands-on AWS infrastructure provisioning
    

---

## **🔗 Resources**

* [Terraform AWS Provider D](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)[ocs](https://developer.hashicorp.com/terraform/cli)
    
* [Terraform CLI Doc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)[s](https://developer.hashicorp.com/terraform/cli)
    

## [🤝 Contributi](https://developer.hashicorp.com/terraform/cli)ng

Your perspective is valuable! Wh[ether you see pote](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)[ntial for improvem](https://developer.hashicorp.com/terraform/cli)ent or ap[preciate what's al](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)[ready here, your c](https://developer.hashicorp.com/terraform/cli)ontributi[ons are welcomed and appreciated.](https://registry.terraform.io/providers/hashicorp/aws/latest/docs) [Thank you for](https://developer.hashicorp.com/terraform/cli) [considering joini](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)[ng us in making th](https://developer.hashicorp.com/terraform/cli)is project even better. Feel free to follow me for updates on this project and others, and to explore opportunities for collaboration. Together, we can create something amazing!

## 📄 License

This project is licensed under the JoebahoCloud License
