Voiced by Amazon Polly |
Overview
In the digital age, having a robust and scalable infrastructure is essential for any online presence. With the power of cloud computing, platforms like Amazon Web Services (AWS) provide the tools and services necessary to create scalable and reliable environments. In this blog post, we will learn how to set up a WordPress website on AWS using Terraform, a popular infrastructure-as-code tool.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
Setting up a WordPress website on AWS using Terraform offers numerous benefits, including automation, repeatability, and scalability. Terraforms declarative syntax allows developers to define infrastructure configurations as code, enabling easy replication and modification across environments. By leveraging AWS services such as Amazon EC2, Amazon RDS, Amazon S3, and Load Balancer, developers can ensure high availability, security, and performance for their WordPress sites.
Prerequisites
Before diving into the implementation, ensure you have the following prerequisites:
- An AWS account with appropriate permissions to create resources.
- Terraform is installed on your local machine.
Steps to Setup the Infrastructure
We will use Terraform to define our infrastructure as code, making it easy to manage and reproduce. Below are the steps to be followed.
Step 1: Set up Terraform Configuration: Create a directory for your project and initialize a new Terraform configuration file (e.g., main.tf). Inside main.tf, define your provider and required resources.
1 2 3 4 5 6 7 8 9 |
# Define AWS provider provider "aws" { region = "ap-south-1" } # Define VPC, RDS, EC2, ALB, and target group resources |
Step 2:
Define the Amazon VPC: To isolate our resources, define Amazon VPC with appropriate subnets, route tables, and security groups.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# Define VPC resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" } # Define public and private subnets resource "aws_subnet" "public_subnet" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" availability_zone = "ap-south-1a" map_public_ip_on_launch = true } resource "aws_subnet" "private_subnet" { vpc_id = aws_vpc.main.id cidr_block = "10.0.2.0/24" availability_zone = "ap-south-1b" } # Define internet gateway resource "aws_internet_gateway" "gw" { vpc_id = aws_vpc.main.id } # Attach internet gateway to VPC resource "aws_vpc_attachment" "gw_attachment" { vpc_id = aws_vpc.main.id internet_gateway_id = aws_internet_gateway.gw.id } # Define route table for public subnet resource "aws_route_table" "public_route_table" { vpc_id = aws_vpc.main.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.gw.id } tags = { Name = "Public" } } # Associate public subnet with public route table resource "aws_route_table_association" "public_route_assoc" { subnet_id = aws_subnet.public_subnet.id route_table_id = aws_route_table.public_route_table.id } |
Step 3:
Set up Amazon RDS: Configure an Amazon RDS instance to host the WordPress database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Define RDS instance resource "aws_db_instance" "wordpress_db" { identifier = "wordpress-db" allocated_storage = 20 storage_type = "gp2" engine = "mysql" engine_version = "8.0.21" instance_class = "db.t2.micro" username = "admin" password = "password" parameter_group_name = "default.mysql8.0" publicly_accessible = false tags = { "Name" = "wordpress_db" } } |
Step 4:
Configure Amazon EC2 Instance: Set up an Amazon EC2 instance running Ubuntu to host WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# Define security group for EC2 instance resource "aws_security_group" "wordpress_sg" { vpc_id = aws_vpc.main.id ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } 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"] } } # Define EC2 instance resource "aws_instance" "wordpress_instance" { ami = "ami-0c55b159cbfafe1f0" # Ubuntu 20.04 LTS instance_type = "t2.micro" subnet_id = aws_subnet.public_subnet.id vpc_security_group_ids = [aws_security_group.wordpress_sg.id] connection { type = "ssh" user = "ubuntu" private_key = file("~/.ssh/id_rsa") # Update with your private key file path host = self.public_ip } provisioner "remote-exec" { inline = [ "sudo apt update", "sudo apt install -y apache2 mysql-client php libapache2-mod-php php-mysql", "sudo systemctl enable apache2", "sudo systemctl start apache2", "wget https://wordpress.org/latest.tar.gz", "tar -zxvf latest.tar.gz", "sudo mv wordpress /var/www/html/wordpress", "sudo chown -R www-data:www-data /var/www/html/wordpress", "sudo chmod -R 755 /var/www/html/wordpress", "sudo systemctl restart apache2" ] } } |
Step 5:
Create Application Load Balancer: Set up an ALB to distribute traffic to our Amazon EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Define ALB resource "aws_lb" "wordpress_lb" { name = "wordpress-lb" internal = false load_balancer_type = "application" security_groups = [aws_security_group.wordpress_sg.id] subnets = [aws_subnet.public_subnet.id] } |
Step 6:
Define Target Group and Listener: Define a target group and configure a listener to forward traffic to our Amazon EC2 instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# Define target group resource "aws_lb_target_group" "wordpress_target_group" { name = "wordpress-target-group" port = 80 protocol = "HTTP" vpc_id = aws_vpc.main.id health_check { path = "/" interval = 30 timeout = 5 healthy_threshold = 2 unhealthy_threshold = 2 matcher = "200" } } # Define listener for ALB resource "aws_lb_listener" "wordpress_listener" { load_balancer_arn = aws_lb.wordpress_lb.arn port = 80 |
Conclusion
In this blog post, we’ve seen how to set up a scalable WordPress infrastructure on AWS using Terraform.
Using AWS services such as Amazon VPC, Amazon RDS, Amazon EC2, ALB, and target groups, we’ve created a resilient environment capable of handling WordPress workloads effectively. This approach enables efficient resource management, cost optimization, and streamlined deployment processes, which are essential for modern web applications.
Drop a query if you have any questions regarding WordPress and we will get back to you quickly.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 650k+ professionals in 500+ cloud certifications and completed 300+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, and many more.
To get started, go through our Consultancy page and Managed Services Package, CloudThat’s offerings.
FAQs
1. Why use Terraform for infrastructure provisioning?
ANS: – Terraform enables infrastructure as code, allowing users to define, provision, and manage cloud resources declaratively. It offers version control, automation, and reproducibility benefits, making it easier to manage complex infrastructure setups.
2. What are the advantages of hosting WordPress on AWS?
ANS: – Hosting WordPress on AWS offers scalability, reliability, and performance benefits. AWS provides a wide range of services, such as Amazon RDS for managed databases, Amazon EC2 for scalable compute resources, and ALB for load balancing, ensuring a robust infrastructure for WordPress websites.
3. How does the Application Load Balancer (ALB) improve WordPress performance?
ANS: – ALB distributes incoming traffic across multiple Amazon EC2 instances, enhancing the availability and scalability of WordPress websites. It can automatically scale in response to traffic fluctuations, ensuring optimal performance and minimal downtime.
WRITTEN BY Shaikh Mohammed Fariyaj Najam
Mohammed Fariyaj Shaikh works as a Research Associate at CloudThat. He has strong analytical thinking and problem-solving skills, knowledge of AWS Cloud Services, migration, infrastructure setup, and security, as well as the ability to adopt new technology and learn quickly.
Click to Comment