AWS CloudFormation Template for a Basic WordPress Blog

Introduction

Creating a WordPress blog on AWS can seem daunting, but if you use AWS CloudFormation, the process can be streamlined significantly. This blog post will guide you through creating a CloudFormation template that sets up an Amazon EC2 instance with a LAMP stack (Linux, Apache, MySQL, PHP), installs WordPress, and configures routing through an Elastic Load Balancer (ELB). This setup ensures better scalability and reliability for your blog.

What is AWS CloudFormation?

AWS CloudFormation is a service that helps you model and set up your Amazon Web Services resources so that you can spend less time managing those resources and more time focusing on your applications. It does this by allowing you to use a declarative template in JSON or YAML format to define the desired state of your infrastructure.

Understanding the Components of the CloudFormation Template

Our template will contain the following key components:

  • Parameters: User-defined variables that allow customization when launching the stack.
  • Resources: The AWS services that will be created, including EC2 instances and ELB.
  • Outputs: Information about the resources created, like the public URL of your WordPress installation.

Basic CloudFormation Template Structure

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS CloudFormation template for a basic WordPress blog",
  "Parameters": {
    "InstanceType": {
      "Type": "String",
      "Default": "t2.micro",
      "Description": "EC2 instance type"
    },
    "KeyName": {
      "Type": "AWS::EC2::KeyPair::KeyName",
      "Description": "SSH key pair name"
    }
  },
  "Resources": {
    "MyLoadBalancer": {
      "Type": "AWS::ElasticLoadBalancing::LoadBalancer",
      "Properties": {
        "Listeners": [
          {
            "LoadBalancerPort": "80",
            "InstancePort": "80",
            "Protocol": "HTTP"
          }
        ],
        "HealthCheck": {
          "Target": "HTTP:80/",
          "Interval": "30",
          "Timeout": "5",
          "HealthyThreshold": "2",
          "UnhealthyThreshold": "5"
        }
      }
    },
    "MyInstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": {"Ref": "InstanceType"},
        "ImageId": "ami-0abcdef1234567890",  // Use a valid AMI ID
        "KeyName": {"Ref": "KeyName"},
        "SecurityGroups": [{ "Ref": "InstanceSecurityGroup" }],
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["\n", [
              "#!/bin/bash",
              "yum update -y",
              "yum install -y httpd mysql php php-mysqlnd",
              "wget https://wordpress.org/latest.tar.gz",
              "tar -xzf latest.tar.gz",
              "cp -r wordpress/* /var/www/html/",
              "chown -R apache:apache /var/www/html/",
              "systemctl start httpd",
              "systemctl enable httpd"
            ]]
          }
        }
      }
    }
  },
  "Outputs": {
    "WebsiteURL": {
      "Description": "WordPress website URL",
      "Value": { "Fn::GetAtt": ["MyLoadBalancer", "DNSName"] }
    }
  }
} 

Breakdown of the Template

Let’s analyze the components:

Component Description
Parameters Allows customization of instance type and SSH key pair name at stack launch.
Load Balancer Distributes incoming traffic across multiple targets, ensuring high availability and reliability.
EC2 Instance Launches the actual server that will host your WordPress site.
UserData Shell commands executed upon instance launch, installing the LAMP stack and WordPress.
Outputs Provides the URL of the deployed WordPress site for easy access.

Deploying the CloudFormation Stack

To deploy your CloudFormation template, follow these steps:

  1. Login to your AWS Management Console.
  2. Go to the CloudFormation service.
  3. Click on “Create Stack” and select “With new resources (standard).”
  4. In the “Specify template” section, upload your template file or paste the raw content.
  5. Specify parameters like the instance type and key pair.
  6. Review the configurations and click “Create stack.”

Accessing Your WordPress Site

Once your stack is created successfully, CloudFormation will set up all resources required for your WordPress blog. You can find the URL of your website in the outputs section of your CloudFormation stack. It will look something like this:

http://your-load-balancer-dns-name

Open this URL in your browser, and you should see the WordPress installation page. Follow the on-screen instructions to set up your website.

Conclusion

Using AWS CloudFormation to deploy a basic WordPress blog saves you time and effort by automating the setup process for your EC2 instance and LAMP stack. This approach not only allows for scalability using an Elastic Load Balancer but also fosters best practices in deployment and infrastructure management. With just a few steps, you can have your WordPress blog up and running on AWS.

Additional Resources