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.
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.
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.
Let’s break down the CloudFormation template into manageable sections.
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
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"
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
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
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"
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.