Build Your Own Wordpress Website Using Terraform.

Build Your Own WordPress Website Using Terraform.

Introduction.

Building a WordPress website using Terraform involves automating the provisioning and configuration of the infrastructure needed to run WordPress on AWS (or any other cloud provider you choose). Terraform is an Infrastructure-as-Code (IaC) tool that allows you to define your infrastructure in code and manage it consistently and efficiently.

In this guide, we’ll walk you through the process of setting up a WordPress site using Terraform to automate infrastructure provisioning on AWS.

Prerequisites:

  1. Terraform: Make sure Terraform is installed on your machine. You can download it from Terraform’s official website.
  2. VS Code: Visual Studio Code should be installed, and you should have the Terraform extension installed.
  3. AWS Account: An active AWS account for deploying resources.
  4. AWS CLI: Make sure the AWS CLI is configured to use your credentials.

TASK 1: Set up Terraform Configuration

STEP 1: Create variables.tf file.

  • Enter the following command.
  • And save the file.
variable "access_key" {
    description = "Access key to AWS console"
}

variable "secret_key" {
    description = "Secret key to AWS console"
}
variable "region" {
    description = "AWS region"
}
Screenshot 2025 02 05 122012

STEP 2: Next, Create terraform.tf file.

  • Enter the following command and save the file.
region = "us-east-1"            
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"
Screenshot 2025 02 05 122142

STEP 3: Create main.tf file.

provider "aws" {
  region = "us-east-1"  # Change region as per your preference
}

resource "aws_security_group" "wordpress_sg" {
  name        = "wordpress_sg"
  description = "Security group for WordPress"

  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"]
  }

  ingress {
    from_port   = 443
    to_port     = 443
    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_instance" "wordpress" {
  ami           = "ami-0c55b159cbfafe1f0"  # This should be the latest Ubuntu AMI (update as per region)
  instance_type = "t2.micro"
  key_name      = "your-key-pair"  # Ensure you have an existing key pair

  security_groups = [aws_security_group.wordpress_sg.name]

  tags = {
    Name = "WordPress-Instance"
  }

  user_data = <<-EOF
              #!/bin/bash
              sudo apt update -y
              sudo apt install apache2 -y
              sudo apt install mysql-server -y
              sudo apt install php libapache2-mod-php php-mysql -y
              sudo systemctl start apache2
              sudo systemctl enable apache2
              sudo systemctl start mysql
              sudo systemctl enable mysql
              cd /var/www/html
              sudo wget https://wordpress.org/latest.tar.gz
              sudo tar -xvzf latest.tar.gz
              sudo chown -R www-data:www-data /var/www/html/wordpress
              EOF
}

resource "aws_eip" "wordpress_ip" {
  instance = aws_instance.wordpress.id
}

output "website_url" {
  value = "http://${aws_eip.wordpress_ip.public_ip}"
}
Screenshot 2025 02 05 152256

TASK 2: Apply terraform configurations.

STEP 1: Go to terminal enter terraform init command.

Screenshot 2025 02 05 154156

STEP 2: Next, Enter terraform plan and terraform apply command.

Screenshot 2025 02 05 154246
Screenshot 2025 02 05 154345

TASK 3: Open the WordPress Dashboard.

STEP 1: Go to aws console verify created the instance.

  • Select the public IP and paste your browser.
Screenshot 2024 11 22 174056

STEP 2: Select English and click continue.

Screenshot 2024 11 22 174129

STEP 3: Enter the Title , Username , Email.

  • Click on WordPress.
Screenshot 2024 11 22 175403

Step 4 : Click on Log In button.

Screenshot 2024 11 22 175430
Screenshot 2024 11 22 175453

STEP 5 : Now you will see wordpress dashboard.

Screenshot 2024 11 22 175515

Conclusion.

Building a WordPress website using Terraform in Visual Studio Code is an excellent way to automate the infrastructure deployment process, especially when working in a cloud environment like AWS. By leveraging Terraform, you can ensure that your WordPress setup is repeatable, scalable, and easily manageable.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *