Cloud / IaC · Freelance

Auto-Scaling WordPress on AWS with CloudFormation, EFS, ALB and Aurora

Repeatable CloudFormation templates for a stateless WordPress web tier in an Auto Scaling group, with shared wp-content on EFS, an ALB and a managed database.

  • CloudFormation
  • EC2 Auto Scaling
  • Application Load Balancer
  • EFS
  • RDS Aurora MySQL
  • ACM
  • Route 53
  • Bash

// Why this matters

Auto-scaling WordPress on AWS is one of the most requested infrastructure jobs because WordPress is everywhere and almost nobody runs it in a way that survives a traffic spike or an instance failure. A single EC2 box with a local database works until a campaign lands or the disk fills, and then recovery means someone SSHing in at night.

The goal here was a template, not a one-off server. Once it is expressed as CloudFormation a second environment, a staging copy or a different client is a parameter file. That repeatability was the real deliverable.

// The problem

WordPress is not stateless out of the box. Uploads are written to the local filesystem, plugins and themes are installed by clicking in the admin, and the salts and database settings live in wp-config.php. Put two instances behind a load balancer and a user uploads an image that exists on only one of them, so the other returns a 404 half the time.

The second problem is bootstrapping. Auto Scaling launches instances you never touch, so every manual step has to become code. If the bootstrap script has a typo the group will happily launch instance after instance that never serves traffic, and you find out when the load balancer shows no healthy targets.

Finally there are the database choices. Some clients wanted Aurora MySQL, others had a mandate for SQL Server. The web tier must not care which is behind it, or you maintain two templates.

// The solution

We make the web tier disposable. Shared wp-content is an EFS file system mounted on every instance, so uploads, plugins and themes appear everywhere immediately. The database is a managed Aurora cluster in private subnets with credentials in Secrets Manager. Instances hold nothing worth keeping.

The template is split into nested stacks: a network stack, a data stack (EFS, database, security groups) and a web stack (launch template, ASG, ALB, scaling policy). Outputs from the first two feed the third. This keeps each template small enough to review and lets you replace the database stack independently of the web tier.

Bootstrap runs from launch template user data. It installs the web server and PHP, mounts EFS with the NFS options AWS recommends, pulls the database credentials from Secrets Manager and renders wp-config. It then calls cfn-signal. The ASG has a CreationPolicy requiring a minimum number of success signals, so a broken bootstrap fails the stack instead of silently producing zombie instances.

Scaling is target tracking on average CPU, which is crude but robust for PHP. I considered request count per target, which is more accurate for web traffic, but CPU needs no extra tuning for a first version. A scheduled action raises the floor before known peaks. The database engine is a parameter that selects connection settings; the MSSQL variant needs a WordPress database abstraction plugin, which is why it stayed a variant rather than the default.

How it flows flow1. Network stack → 2. Data stack → 3. Launch template → 4. ASG → 5. Bootstrap → 6. cfn-signal gate → 7. ALB → 8. Scaling policy01Network stack02Data stack03Launch template04ASG05Bootstrap06 · GATEcfn-signal gate07ALB08Scaling policy
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. Network stackVPC, public and private subnets, NAT
  2. Data stackEFS with mount targets, Aurora cluster, secrets
  3. Launch templateAMI, instance profile, user data bootstrap
  4. ASGmin, desired and max across two zones
  5. Bootstrapmount EFS, write wp-config, start PHP and web server
  6. cfn-signal gate gatestack waits until instances report healthy
  7. ALBHTTPS listener, health check on /wp-login.php
  8. Scaling policytarget tracking on CPU, scale-in protection off
EdgeRoute 53ALB HTTPS 443ACM certificateWeb tierASG across 2 zonesLaunch templateApache and PHPShared stateEFS wp-contentMount targets per zoneDataAurora MySQL clusterSecrets ManagerNetworkPublic subnetsPrivate subnetsNAT gateway
Three-tier layout. Only the web tier is replaced during scaling events.
ASGInstanceEFSSecrets ManagerCloudFormationlaunch from templatemount wp-contentread database credentialswp-config rendered, services startedcfn-signal successregister with ALB target group
Instance bootstrap. The signal is what makes the stack trustworthy.

// Before you start

  • An AWS account and a region with at least two availability zones
  • AWS CLI v2 and permission to create IAM roles, VPC, EFS, RDS, ELB and Auto Scaling resources
  • An ACM certificate in the same region as the ALB for the site domain
  • A hosted zone, or a plan to create a CNAME to the ALB at your DNS provider
  • Comfort reading bash and CloudFormation YAML

// Step-by-step implementation

  1. 01 Parent template wiring nested stacks

    The parent stack passes parameters down and stitches outputs between children. Nested stacks need their templates in S3, which the aws cloudformation package command handles by uploading local files and rewriting the TemplateURL values.

  2. 02 Shared storage and the managed database

    EFS needs one mount target per private subnet and a security group that allows NFS from the web security group only. Aurora gets credentials generated by Secrets Manager, so no password ever appears in a parameter or a template output.

    Use bursting or elastic throughput on EFS depending on traffic; elastic avoids the burst credit cliff that makes a quiet site suddenly slow.

  3. 03 Launch template with a deterministic bootstrap

    The user data does five things in order: install packages, mount EFS, fetch credentials, render wp-config, signal. Using set -euo pipefail means any failure aborts before the signal, which is exactly what we want the CreationPolicy to see.

    WordPress core is downloaded into the instance, while only wp-content is shared. That keeps core versions per-instance and avoids two nodes fighting over core files on NFS.

  4. 04 Auto Scaling group with a CreationPolicy

    The group spans both private subnets and registers with the ALB target group. ELB health checks with a grace period let a slow bootstrap finish before the instance is judged. The CreationPolicy and UpdatePolicy give safe stack creation and rolling replacement when you change the launch template.

  5. 05 Application Load Balancer with HTTPS

    The ALB terminates TLS with the ACM certificate and forwards to port 80 on the instances. Redirect plain HTTP to HTTPS at the listener. The health check hits /wp-login.php, which exercises PHP and the database connection, rather than a static file that would pass even when WordPress is broken.

    WordPress needs to know it is behind TLS or it generates mixed-content links; set HTTP_X_FORWARDED_PROTO handling in wp-config.

  6. 06 Scheduled capacity and the Makefile around it

    Target tracking reacts after load arrives. For predictable peaks add scheduled actions that raise the minimum before traffic. A small Makefile gives the team one way to package, deploy and tear down.

    Keep one parameter file per environment next to the Makefile so promoting a change is the same command with a different file.

// How to verify it works

  • Run aws cloudformation describe-stacks and confirm CREATE_COMPLETE only after all signals arrived.
  • Open the site over HTTPS, upload an image in the admin and reload several times; confirm it never 404s as requests spread across nodes.
  • Terminate one instance with aws autoscaling terminate-instance-in-auto-scaling-group and confirm a replacement joins and the site stays up.
  • Generate load with a tool such as hey and watch the desired capacity rise in aws autoscaling describe-auto-scaling-groups.
  • Check target health with aws elbv2 describe-target-health and confirm every target is healthy.

// Design decisions and trade-offs

  • Split the stack into nested templates: network, data, web tier
  • Make web instances disposable; everything stateful lives in EFS and the database
  • Bootstrap through launch template user data and cfn-signal so failed instances never join the load balancer
  • Scale on average CPU with a scheduled floor for known traffic peaks
  • Keep the database engine a parameter so the same web tier supports MySQL and SQL Server variants

// Pitfalls and lessons learned

  • Storing core files on EFS makes updates risky and every request slower; share only wp-content.
  • Without matching salts on every node, users get logged out whenever the balancer sends them elsewhere.
  • EFS bursting credits drain on busy sites; elastic throughput avoids a mysterious slowdown after weeks.
  • A health check on a static file keeps broken instances in rotation; test PHP and the database.
  • Forgetting forwarded-protocol handling yields redirect loops or mixed content behind the ALB.
  • WordPress plugins that write to the web root outside wp-content silently diverge between instances.
  • Rolling updates without WaitOnResourceSignals replace healthy nodes before the new ones are ready.

// Frequently asked questions

How do I set up auto scaling WordPress on AWS?

Put WordPress on EC2 in an Auto Scaling group behind an ALB, share wp-content through EFS, use a managed database such as Aurora, and bootstrap instances with user data that signals CloudFormation when ready.

Why EFS instead of syncing uploads to S3?

EFS needs no plugin and works for themes and plugins too. S3 offload plugins are cheaper at scale but only cover media; many teams start with EFS and move media later.

Can the same template use SQL Server?

The web tier is engine-agnostic; swap the data stack and the connection settings. WordPress itself needs a compatibility layer for SQL Server, so treat it as a variant and test plugins carefully.

How many instances should the minimum be?

Two, one per zone, so a zone failure does not take the site down. Raise the floor for known peaks with scheduled actions.

Is there a managed alternative?

Yes, containers on Fargate with the same EFS and database split, as in ECS Blue/Green Deployments with CodeDeploy and GitHub Actions. The instance approach is simpler to debug for teams used to servers.

// The outcome

WordPress runs on identical throwaway instances that scale out under load and are replaced automatically when unhealthy. Teams that needed the stack more than once simply deployed it again with another parameter file.

// Where to take it next

// Keep reading