AWS CloudFormation Template for a WordPress eCommerce Site

Introduction

Building a robust eCommerce site requires more than just a standard WordPress setup. With a reliable cloud infrastructure solution like Amazon Web Services (AWS), developers can leverage tools such as AWS CloudFormation, Amazon RDS (Relational Database Service), and Amazon S3 (Simple Storage Service) to create a scalable and efficient platform. In this blog, we will explore how to create a CloudFormation template optimized for a WordPress eCommerce application powered by WooCommerce.

Understanding AWS CloudFormation

AWS CloudFormation allows users to define and provision AWS infrastructure with code. It uses JSON or YAML formatted templates to create a collection of related AWS resources, provisioning them in an orderly and predictable way. This approach not only simplifies infrastructure management but also allows for version control and easy replication across different environments.

Key Components of Our CloudFormation Template

1. Amazon RDS: This service provides scalable and cost-efficient relational databases. It enables automatic backups, replication, and read replicas for improved performance.

2. Amazon S3: This storage service is used for storing and retrieving any amount of data at any time. For our WordPress site, S3 will handle media uploads (images, videos, etc.), freeing up server space and potentially reducing costs.

3. WooCommerce: This powerful eCommerce plugin transforms a basic WordPress site into a fully functional online store, providing features such as payment gateway integration, product catalogues, and inventory management.

Step-by-Step Guide to Creating the CloudFormation Template

Let’s break down the CloudFormation template into manageable sections.

1. Define the Parameters

Defining parameters allows you to customize the deployment. Here’s a sample snippet for defining key parameters:

Parameters:
  DBInstanceType:
    Description: "Database instance type"
    Type: String
    Default: "db.t3.micro"
    AllowedValues:
      - "db.t3.micro"
      - "db.t3.small"
      - "db.t3.medium"

  WordPressAdminUser:
    Description: "WordPress admin username"
    Type: String
    Default: "admin"

  WordPressAdminPassword:
    Description: "WordPress admin password"
    Type: String
    NoEcho: true

2. Create the Amazon RDS Instance

The following section creates an Amazon RDS instance:

Resources:
  WordPressDB:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceClass: !Ref DBInstanceType
      AllocatedStorage: "20"
      Engine: "mysql"
      EngineVersion: "5.7"
      MasterUsername: !Ref WordPressAdminUser
      MasterUserPassword: !Ref WordPressAdminPassword
      DBName: "wordpress"
      BackupRetentionPeriod: "7"
      VPCSecurityGroups:
        - !GetAtt WordPressDBSecurityGroup.GroupId
  WordPressDBSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "Allow MySQL access"
      SecurityGroupIngress:
        - IpProtocol: "tcp"
          FromPort: "3306"
          ToPort: "3306"
          CidrIp: "0.0.0.0/0"

3. Configure Amazon S3 for Media Storage

Next, we add S3 for storing media files:

  MediaStorageBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Sub "wordpress-media-${AWS::Region}"
      AccessControl: Private
      LifecycleConfiguration:
        Rules:
          - Id: "MoveToGlacier"
            Status: "Enabled"
            ExpirationInDays: 365

4. Install and Configure WordPress with WooCommerce

Lastly, we need to ensure WordPress with WooCommerce is installed on our EC2 instance:

  WordPressInstance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: "t2.micro"
      KeyName: !Ref KeyName
      ImageId: !Ref LatestAmiId
      UserData:
        Fn::Base64:
          !Sub |
            #!/bin/bash
            yum update -y
            yum install -y httpd mariadb-server
            systemctl start httpd
            systemctl enable httpd
            
            # Install PHP and extensions
            amazon-linux-extras install php8.0 -y
            yum install -y php-mysqlnd
            
            # Install WordPress
            wget https://wordpress.org/latest.tar.gz
            tar -zxvf latest.tar.gz
            cp -R wordpress/* /var/www/html/
            systemctl restart httpd
            
            # Install WooCommerce
            wp plugin install woocommerce --activate

5. Outputs

We can also define outputs that can be useful for later reference, such as the website’s URL and the database endpoint.

Outputs:
  WebsiteURL:
    Description: "URL for newly created WordPress site"
    Value: !Sub "http://${WordPressInstance.PublicIp}"

  DBEndpoint:
    Description: "RDS Database Endpoint"
    Value: !GetAtt WordPressDB.Endpoint"

Conclusion

Creating a WordPress eCommerce site using AWS CloudFormation offers an efficient, automated way to deploy your infrastructure. The template outlined in this blog addresses all necessary components, from setting up an Amazon RDS instance for database management to configuring S3 for media storage and installing WooCommerce. With this approach, you’ll be able to deploy scalable, reliable, and easily manageable web applications.