Multi-tier web application on AWS using Terraform

Multi-tier web application on AWS using Terraform

Β·

3 min read

In this tutorial, we'll cover the steps to create a VPC, subnets, security groups, an EC2 instance for the web server, and an RDS instance for the database. By the end, you'll have a fully functional multi-tier web application running on AWS

Before we dive in, make sure you have the following installed and configured: Terraform, AWS CLI, and your AWS credentials set up.

Let's start by setting up our project. Open your terminal and create a new directory for your Terraform project:

mkdir multi-tier-app

cd multi-tier-app

Next, initialize your Terraform project

terraform init

Now, let's define our infrastructure. We'll start with creating a VPC and subnets. Open your code editor and create a file named main.tf

"In main.tf, add the following code to define a VPC and a subnet."

provider "aws" { region = "us-west-2" }

resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" tags = { Name = "main-vpc" } }

resource "aws_subnet" "subnet1" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" availability_zone = "us-west-2a" tags = { Name = "subnet1" } }

Next, we'll create security groups for our web server and database. Security groups act as virtual firewalls to control inbound and outbound traffic.

resource "aws_security_group" "web_sg" { vpc_id = aws_vpc.main.id

ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }

egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] }

tags = { Name = "web-sg" } }

resource "aws_security_group" "db_sg" { vpc_id = aws_vpc.main.id

ingress { from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["10.0.0.0/16"] }

egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] }

tags = { Name = "db-sg" } }

"Now, let's add an EC2 instance for our web server. We'll use the Amazon Linux 2 AMI and place it in our previously created subnet."

resource "aws_instance" "web" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" subnet_id = aws_subnet.subnet1.id security_groups = [aws_security_group.web_sg.name]

tags = { Name = "web-server" } }

Next, we'll add an RDS instance for our MySQL database. This instance will be placed in the same VPC and will use the security group we created earlier

resource "aws_db_instance" "db" { engine = "mysql" instance_class = "db.t2.micro" allocated_storage = 20 name = "mydatabase" username = "admin" password = "password" vpc_security_group_ids = [aws_security_group.db_sg.id] db_subnet_group_name = aws_db_subnet_group.main.name

tags = { Name = "mydatabase" } }

resource "aws_db_subnet_group" "main" { name = "main-subnet-group" subnet_ids = [aws_subnet.subnet1.id]

tags = { Name = "main-subnet-group" } }

With our infrastructure defined, it's time to apply our Terraform configuration and provision the resources on AWS. Run the following command in your terminal

terraform apply

Terraform will prompt you to confirm the changes. Type 'yes' and press Enter."

Once Terraform has finished applying the changes, head over to the AWS Management Console to verify that all resources have been created.

You should see your VPC, subnets, security groups, EC2 instance, and RDS instance listed in the respective sections

Congratulations! You've successfully deployed a multi-tier web application on AWS using Terraform. This setup is highly scalable and can be extended with additional resources and configurations as needed

Here is a video description of the same: YouTube Video Tutorial

Β