Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

AWS Logo
Menu
Deploying a Single WordPress Instance on AWS with Terraform via Amazon Q

Deploying a Single WordPress Instance on AWS with Terraform via Amazon Q

In today's cloud-driven world, automating infrastructure and managing resources efficiently is key to saving time and minimizing errors. Amazon Q is a powerful tool that can help you streamline your deployment processes with ease. In this blog, we'll walk you through deploying a WordPress website on AWS using Amazon Q, showcasing how simple it is to set up and manage resources with the right prompts and guidance.

Published Jan 15, 2025
  1. To get started with amazon Q in vs code go to Extension → Search for Amazon Q → Then click on install button and restart vs code
    Image not found
  2. Now you can see that the Amazon Q extension has been successfully installed in VS Code. To start using the service, click on “Use for Free”. Currently, this AWS service is free, so you can select this option. If you have a Pro plan, you can log in with that as well. For now, I will proceed with the first option and click on the “Continue” button.
    Image not found
  3. After clicking Continue, a pop-up will appear prompting you to open the AWS website to authenticate. Click on Open to proceed.
    Image not found
  4. Now you will see AWS requesting access to your VS Code for proper integration. Click on "Allow Access" to proceed.
    Image not found
  5. I clicked on "Allow Access", and my request was approved. You can now close this tab and reopen your VS Code.
    Image not found
  6. Now, I can see that Amazon Q is accessible inside my VS Code, and I can start building anything with Amazon Q.
    Image not found
  7. Now, we can deploy an EC2 WordPress server using Amazon Q. Before starting, ensure that your AWS CLI is installed and credentials are properly configured.
    Image not found
  8. Now, I will provide instructions to Amazon Q about what I want to achieve, and it will assist me in accomplishing this scenario.
    Image not found
  9. Here, I have created the folder structure as suggested by Amazon Q. Now, I will paste the code into the respective folders.
    Image not found
  10. Now that all my files are ready with the code, Amazon Q has also provided the code. Just copy and run it to proceed.
A. main.tf
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
provider "aws" {
region = var.region
}

resource "aws_instance" "wordpress" {
ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
vpc_security_group_ids = [aws_security_group.wp_sg.id]

user_data = templatefile("userdata.sh", {
db_name = var.db_name
db_user = var.db_user
db_password = var.db_password
})

tags = {
Name = "WordPress-Server"
}
}

resource "aws_security_group" "wp_sg" {
name = "wordpress-sg"
description = "Security group for WordPress"

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

ingress {
from_port = 22
to_port = 22
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"]
}
}
  • AWS Provider: Configures the AWS provider with the region specified in the var.region variable.
  • EC2 Instance: Creates a WordPress EC2 instance using the specified AMI, instance type, key pair, and security group, with user data for initializing WordPress.
  • Security Group: Defines a security group allowing HTTP (port 80) and SSH (port 22) traffic from anywhere, with unrestricted outbound traffic.
B. variables.tf
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
variable "region" {
default = "us-east-1"
}

variable "ami_id" {
default = "ami-0c7217cdde317cfec" # Ubuntu 22.04 LTS in us-east-1
}

variable "instance_type" {
default = "t2.micro"
}

variable "key_name" {
description = "Name of the SSH key pair"
}

variable "db_name" {
default = "wordpress"
}

variable "db_user" {
default = "admin"
}

variable "db_password" {
default = "test@123" #replace your password here
}
  • Variables for AWS Config: Sets default values for region, ami_id (Ubuntu 22.04 LTS), and instance_type (t2.micro) for the EC2 instance.
  • SSH Key Pair Variable: Defines key_name as a variable for specifying the SSH key pair name (no default).
  • Database Configuration: Provides default values for database name (wordpress), user (admin), and password (test@123).
C. outputs.tf
1
2
3
output "public_ip" {
value = aws_instance.wordpress.public_ip
}
  • Output Block: Displays the public IP address of the WordPress EC2 instance after the Terraform deployment
D. userdata.sh
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
#!/bin/bash
sudo apt update
sudo apt install apache2 mysql-server php php-mysql php-curl php-gd php-mbstring php-xml php-xmlrpc php-soap php-intl php-zip -y

# Start and enable services
sudo systemctl start apache2
sudo systemctl start mysql
sudo systemctl enable apache2
sudo systemctl enable mysql

# Configure MySQL
sudo mysql -e "CREATE DATABASE ${db_name};"
sudo mysql -e "CREATE USER '${db_user}'@'localhost' IDENTIFIED BY '${db_password}';"
sudo mysql -e "GRANT ALL PRIVILEGES ON ${db_name}.* TO '${db_user}'@'localhost';"
sudo mysql -e "FLUSH PRIVILEGES;"

# Remove default Apache page
sudo rm -rf /var/www/html/*

# Download and configure WordPress
cd /tmp
wget https://wordpress.org/latest.tar.gz
tar -xvf latest.tar.gz
sudo cp -R wordpress/* /var/www/html/
sudo rm -rf wordpress latest.tar.gz

# Configure wp-config.php
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo sed -i "s/database_name_here/${db_name}/" /var/www/html/wp-config.php
sudo sed -i "s/username_here/${db_user}/" /var/www/html/wp-config.php
sudo sed -i "s/password_here/${db_password}/" /var/www/html/wp-config.php

# Set proper permissions
sudo chown -R www-data:www-data /var/www/html/
sudo find /var/www/html/ -type d -exec chmod 755 {} \;
sudo find /var/www/html/ -type f -exec chmod 644 {} \;

# Restart Apache
sudo systemctl restart apache2
  • Install Dependencies: Updates the system and installs Apache, MySQL, PHP, and required extensions for WordPress.
  • Set Up MySQL: Creates the WordPress database and user with the provided credentials and assigns privileges.
  • Deploy WordPress: Downloads, configures, and sets permissions for WordPress, updating wp-config.php with database details.
  • Start Services: Ensures Apache and MySQL are started and enabled for system startup.
E. terraform.tfvars
1
key_name = "AWS-New" # Replace with your AWS key pair name
  • Key Pair Comment: Specifies "AWS-New" as the name of the AWS key pair to use for SSH access to the EC2 instance. Replace it with your actual key pair name.
    Image not found
    Image not found

    11. Let's now try to deploy and see if Amazon Q is able to assist us as expected.
    Image not found
    Image not found

    12. Here, you can see that we have successfully deployed the WordPress website on AWS with no errors. Now, we need to check the final thing—whether we are able to access WordPress or not.
    Image not found
    Image not found
    13. And here it is! We are able to access the WordPress site. This is Amazon Q—using the right prompts, you can build anything with Q. Happy Quing! 🎉
In conclusion, Amazon Q provides a seamless and efficient way to deploy and manage applications on AWS, as demonstrated by our successful WordPress deployment. With the help of Amazon Q, users can easily integrate AWS services and automate deployments, making cloud management more accessible and efficient. So, go ahead, explore Amazon Q, and let it simplify your cloud infrastructure needs! Happy Quing!
 

Comments