Skip to content

Terraform project to provision a scalable, secure two-tier web architecture on AWS using EC2, ALB, CloudFront, Route 53, and ACM with custom domain support.

License

Notifications You must be signed in to change notification settings

panwar100/terraform-aws-two-tier-architecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌐Terraform AWS Two-Tier Architecture

✨This repository is created to learn and deploy a 2-tier application on aws cloud through Terraform.

πŸ“Œ Overview

This Terraform project sets up a complete two-tier architecture on AWS using:

  • VPC
  • Public & Private Subnets
  • EC2 instances
  • Application Load Balancer (ALB)
  • CloudFront Distribution
  • ACM SSL Certificate
  • Route 53 DNS
  • Custom Domain Integration (yourcreativecorner.xyz)

🌍 Prerequisites

  • AWS Account with access to Route 53, EC2, ACM, CloudFront

  • Registered domain (yourcreativecorner.xyz)

  • Terraform CLI installed (v1.4+ recommended)

  • Public hosted zone created in Route 53

🧭 Architecture Diagram

image

βš™οΈ Setup & Usage

1. Clone the Repository

git clone https://github.com/panwar100/terraform-aws-two-tier-architecture
cd terraform-aws-two-tier-architecture-master

2. πŸ–₯️ Installation of Terraform

Note: Follow the blog for the step-by-step instructions to build this project. Terraform

3. Create S3 Backend Bucket

Create an S3 bucket to store the .tfstate file in the remote backend

Screenshot 2025-05-31 163327

Warning! It is highly recommended that you enable Bucket Versioning on the S3 bucket to allow for state recovery in the case of accidental deletions and human error.

Note: We will need this bucket name in the later step

4. Create a Dynamo DB table for state file locking

Screenshot 2025-05-31 163250

  • Give the table a name
  • Make sure to add a Partition key with the name LockID and type as String

5. Generate a public-private key pair for our instances

We need a public key and a private key for our server so please follow the procedure I've included below.

cd modules/key/
ssh-keygen

The above command asks for the key name and then gives client_key it will create a pair of keys one public and one private. you can give any name you want but then you need to edit the Terraform file

Screenshot 2025-06-01 002626

Edit the below file according to your configuration

vim root/backend.tf

Add the below code in root/backend.tf

terraform {
  backend "s3" {
    bucket = "BUCKET_NAME"
    key    = "backend/FILE_NAME_TO_STORE_STATE.tfstate"
    region = "us-east-1"
    use_lockfile  = true
  }
}

6. πŸ‘¨β€πŸ’» Route 53 Hosted Zone

Go to AWS Console --> Route53 --> Hosted Zones and ensure you have a public hosted zone available, if not create one.

Screenshot 2025-05-31 182155

Add your nameservers in your domain like this

Screenshot 2025-05-31 232516

7. πŸ” ACM certificate

Go to AWS console --> AWS Certificate Manager (ACM) and make sure you have a valid certificate in Issued status, if not, feel free to create one and use the domain name on which you are planning to host your application.

Screenshot 2025-05-31 182018

click on create records in Route53

Screenshot 2025-05-31 182055

8. 🏠 Let's set up the variable for our Infrastructure

Create one file with the name terraform.tfvars

vim root/terraform.tfvars

Add the below content into the root/terraform.tfvars file and add the values of each variable.

region = ""
project_name = ""
vpc_cidr                = ""
pub_sub_1a_cidr        = ""
pub_sub_2b_cidr        = ""
pri_sub_3a_cidr        = ""
pri_sub_4b_cidr        = ""
pri_sub_5a_cidr        = ""
pri_sub_6b_cidr        = ""
db_username = ""
db_password = ""
certificate_domain_name = ""
additional_domain_name = ""

Screenshot 2025-06-01 010526

9. ✈️ Now we are ready to deploy our application on the cloud β›…

get into the project directory

cd root

πŸ‘‰ let deploy the application

# Initialize
terraform init
# Preview plan
terraform plan
# Apply infrastructure
terraform apply

Type yes, and it will prompt you for approval..

Screenshot 2025-06-01 010628

πŸ§ͺ Verification

After ~10 minutes post-deployment:

βœ… https://yourcreativecorner.xyz

Screenshot 2025-05-31 175834

βœ… https://www.yourcreativecorner.xyz

Screenshot 2025-05-31 190300

If it doesn't load immediately, wait for CloudFront & DNS propagation.

πŸ–₯️ Outputs

let's see what Terraform created on our AWS console.

Cloudfront Records in Route53

Screenshot 2025-05-31 194314

VPC

Screenshot 2025-05-31 194613

Subnet

Screenshot 2025-05-31 202542

Route Tables

Screenshot 2025-05-31 202620

Internet Getways

Screenshot 2025-05-31 202632

NAT gateways

Screenshot 2025-05-31 202653

Security Groups

Screenshot 2025-05-31 202739

Ec2 instance

Screenshot 2025-05-31 194707

Load Balancer

Screenshot 2025-05-31 194809

Auto Scaling Group

Screenshot 2025-05-31 194825

🧾 Notes

  • Make sure your domain is hosted in Route 53 and ACM certificate is issued and validated in the us-east-1 region for CloudFront.
  • Bad Gateway or SSL errors may occur temporarily during propagation.
  • You can update the terraform.tfvars to match your domain and ALB values.

🧼 Cleanup

terraform destroy

Type yes, and it will prompt you for approval..

πŸ™ Credits

This project is based on 10WeeksOfCloudOps Task 3 by Piyush Sachdeva

About

Terraform project to provision a scalable, secure two-tier web architecture on AWS using EC2, ALB, CloudFront, Route 53, and ACM with custom domain support.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published