0% found this document useful (0 votes)
25 views3 pages

Terraform Project - WordPress Site On AWS

Uploaded by

Sushiel P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Terraform Project - WordPress Site On AWS

Uploaded by

Sushiel P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Deploying a Highly Available (HA) WordPress site on AWS using RDS for the database and

Auto Scaling for the web servers. This project is more advanced and demonstrates how to
use Terraform to manage complex, production-grade infrastructure.
Step 1: Install Terraform and Set Up AWS Credentials
1. Follow Step 1 and Step 2 from the previous guide to install Terraform and configure
AWS credentials.
Step 2: Create the Terraform Project
1. Create a Project Directory:

mkdir terraform-wordpress
cd terraform-wordpress

2. Create Terraform Configuration Files:


• Create the following files: main.tf, variables.tf, outputs.tf.
Step 3: Define the Infrastructure
1. main.tf:
provider "aws" {
region = var.region
}

# Create a VPC
resource "aws_vpc" "wp_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "wp_vpc"
}
}

# Create Public Subnets


resource "aws_subnet" "wp_public_subnet" {
count = 2
vpc_id = aws_vpc.wp_vpc.id
cidr_block = cidrsubnet(aws_vpc.wp_vpc.cidr_block, 8, count.index)
availability_zone = element(data.aws_availability_zones.available.names,
count.index)
tags = {
Name = "wp_public_subnet_${count.index}"
}
}

# Create Internet Gateway


resource "aws_internet_gateway" "wp_igw" {
vpc_id = aws_vpc.wp_vpc.id
tags = {
Name = "wp_igw"
}
}

# Create Route Table for Public Subnets


resource "aws_route_table" "wp_public_rt" {
vpc_id = aws_vpc.wp_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.wp_igw.id
}
tags = {
Name = "wp_public_rt"
}
}

# Associate Public Subnets with Route Table


resource "aws_route_table_association" "wp_public_rta" {
count = 2
subnet_id = element(aws_subnet.wp_public_subnet.*.id, count.index)
route_table_id = aws_route_table.wp_public_rt.id
}

# Create RDS MySQL Database


resource "aws_db_instance" "wp_db" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "wordpressdb"
username = var.db_username
password = var.db_password
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
vpc_security_group_ids = [aws_security_group.wp_db_sg.id]
db_subnet_group_name = aws_db_subnet_group.wp_db_subnet_group.name
}

resource "aws_db_subnet_group" "wp_db_subnet_group" {


name = "wp_db_subnet_group"
subnet_ids = aws_subnet.wp_public_subnet.*.id
}

# Create Launch Configuration for Auto Scaling


resource "aws_launch_configuration" "wp_lc" {
image_id = var.ami_id
instance_type = "t2.micro"
security_groups = [aws_security_group.wp_web_sg.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd php php-mysql
systemctl start httpd
systemctl enable httpd
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz -C /var/www/html/
cp /var/www/html/wordpress/wp-config-sample.php
/var/www/html/wordpress/wp-config.php
sed -i "s/database_name_here/wordpressdb/g"
/var/www/html/wordpress/wp-config.php
sed -i "s/username_here/${var.db_username}/g"
/var/www/html/wordpress/wp-config.php
sed -i "s/password_here/${var.db_password}/g"
/var/www/html/wordpress/wp-config.php
sed -i "s/localhost/${aws_db_instance.wp_db.endpoint}/g"
/var/www/html/wordpress/wp-config.php
EOF
}

# Create Auto Scaling Group


resource "aws_autoscaling_group" "wp_asg" {
launch_configuration = aws_launch_configuration.wp_lc.name
min_size = 2
max_size = 4
desired_capacity = 2
vpc_zone_identifier = aws_subnet.wp_public_subnet.*.id
target_group_arns = [aws_lb_target_group.wp_tg.arn]
}

# Create Application Load Balancer


resource "aws_lb" "wp_alb" {
name = "wp-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.wp_alb_sg.id]
subnets = aws_subnet.wp_public_subnet.*.id
}

resource "aws_lb_target_group" "wp_tg" {


name = "wp-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.wp_vpc.id
}

resource "aws_lb_listener" "wp_listener" {


load_balancer_arn = aws_lb.wp_alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.wp_tg.arn
}
}

# Security Groups
resource "aws_security_group" "wp_web_sg" {
vpc_id = aws_vpc.wp_vpc.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"]
}
}

resource "aws_security_group" "wp_db_sg" {


vpc_id = aws_vpc.wp_vpc.id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
security_groups = [aws_security_group.wp_web_sg.id]
}
}

resource "aws_security_group" "wp_alb_sg" {


vpc_id = aws_vpc.wp_vpc.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"]
}
}
2. variables.tf:

variable "region" {
default = "us-east-1"
}

variable "ami_id" {
default = "ami-0c02fb55956c7d316" # Amazon Linux 2 AMI
}

variable "db_username" {
default = "admin"
}

variable "db_password" {
default = "password123"
}

3. outputs.tf:

output "alb_dns_name" {
value = aws_lb.wp_alb.dns_name
}

Step 4: Initialize and Apply the Configuration


1. Initialize Terraform:

terraform init

2. Plan and Apply:

terraform plan
terraform apply

Step 5: Access the WordPress Site


1. After the deployment, Terraform will output the ALB DNS name.
2. Open a browser and navigate to http://<alb_dns_name>/wordpress to complete the
WordPress setup.
Step 6: Clean Up
1. Destroy the infrastructure to avoid unnecessary charges:

terraform destroy

You might also like