First things first
Setting up a WordPress website can be a rewarding yet challenging endeavor, especially when aiming to automate the entire process using Terraform and AWS. In this post, I’ll walk you through my journey of deploying a fully functional WordPress site on AWS, highlighting the architecture I created, the tools I used, and the lessons I learned along the way.
The Architecture
The architecture I built, as illustrated in the diagram, encompasses various AWS services to ensure high availability, scalability, and automation:
RDS MySQL: Manages the relational database for WordPress.
Application Load Balancer: Distributes incoming traffic across multiple EC2 instances.
EC2 Auto Scaling: Automatically adjusts the number of instances based on the load.
SNS: Sends notifications about various events.
S3 Bucket: Stores static assets and backups.
CloudWatch: Monitors and logs AWS resources and applications.
Tools and Technologies
Throughout this project, I leveraged several tools and technologies:
- Terraform: For infrastructure as code (IaC) to manage AWS resources.
- AWS CLI: For scripting and automation.
- Bash: For writing the user data script to set up the EC2 instance.
- WordPress CLI (WP-CLI): For automating WordPress installation and configuration.
Setting Up with Terraform
Using Terraform, I defined the AWS resources and their configurations. Below is a snippet of my Terraform code for creating an EC2 instance and attaching it to an auto-scaling group:
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t2.micro"
key_name = var.key_name
user_data = file("user_data.sh")
tags = {
Name = "WordPressWebServer"
}
}
resource "aws_autoscaling_group" "web_asg" {
launch_configuration = aws_launch_configuration.web_lc.id
min_size = 1
max_size = 3
desired_capacity = 1
tag {
key = "Name"
value = "WebServer"
propagate_at_launch = true
}
}
The User Data Script
One of the most critical components of this setup was the user data script. This script automates the configuration of the EC2 instance, installs necessary software, and sets up WordPress. Here’s an excerpt of the script:
#! /bin/bash
# Install updates
sudo yum update -y
# Install Curl
sudo yum install curl -y
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
# Configure AWS CLI with IAM role credentials
aws configure set default.region us-west-2
# Install stress testing (optional)
sudo yum install -y stress-ng
# Install httpd
sudo yum install -y httpd
# Install PHP
sudo amazon-linux-extras install -y php8.0
# Retrieve RDS variables from Terraform output
DBName=${db_name}
DBUser=${db_username}
DBRootPassword='rootpassword'
DBPassword=${db_password}
db_endpoint=${db_endpoint}
# Start Apache server and enable it on system startup
sudo systemctl start httpd
sudo systemctl enable httpd
# Download and install WordPress
sudo wget http://wordpress.org/latest.tar.gz -P /var/www/html
cd /var/www/html
sudo tar -zxvf latest.tar.gz
sudo cp -rvf wordpress/* .
sudo rm -R wordpress
sudo rm latest.tar.gz
# Install WordPress Core
wp core install --path=/var/www/html --url="http://${db_endpoint}" --title="My WordPress Site" --admin_user=admin --admin_password=admin_password --admin_email=admin@example.com
This script is a snippet of what I used to configure the EC2 instance with, so it is ready to serve the WordPress site immediately after launch.
Challenges and Solutions
Creating the User Data Script: Writing the user data script was tedious but crucial. I used WP-CLI to streamline the WordPress setup, which saved a lot of manual configuration time.
Auto Scaling Group Dependency: The auto-scaling group was set up to create an AMI image of the EC2 instance only after the instance setup was fully completed. This dependency was managed by checking the status of the setup process before creating the image.
Conclusion
Deploying a WordPress website on AWS using Terraform and automating the setup with a user data script was a challenging but rewarding experience. This project not only enhanced my technical skills but also provided a deep understanding of cloud architecture and automation.
If you’re planning a similar project, I hope this guide provides you with valuable insights and a solid starting point. Happy coding!