There are a couple of issues with our Wordpress deployment.
The EC2 instances running our wordpress site have databases running within them. Each of the databases is keeping state of the app for that instance. If the instance dies, and a new one is started by our ASG, our state will be lost.
To improve this, we can separate our database out from the instance running our app. This means that if our instance dies, our state is safe.
- A RDS instance to run our MySQL database.
- An S3 bucket to store a tarball of our customised Wordpress app (with the correct database access file).
- To modify our original launch config, to download our app from the s3 bucket instead of wordpress.org.
- Set a security group policy for our S3 bucket so that our EC2 instance can read from it.
- Create a new RDS instance in the AWS console (choose MySQL).
- Spin up a new EC2 instance in the ASG, and go through the prompts to create the database (giving it the username, password etc.). This will create a file with the database configuration. Give it the endpoint and port of the RDS isntance.
- In your RDS instance, add all of the login details to the database as per the Wordpress configuration.
- Zip all of the contents of the
/var/www/html
directory in the EC2 instance in to a tarball, and usescp
to download it to your local machine. - Create an S3 bucket and upload the tarball file.
- Create a new IAM role for the EC2 instance to read from the S3 bucket (there's a policy for AmazonS3ReadOnlyAccess). This is fine account wide for training purposes, but usually would be tighter in a real environment. This will create a ARN.
- Copy your old launch config, and modify with the new IAM role.
- New 'user data' file to download from the S3 bucket, instead of Wordpress.org (see below for script).
- Save, and add this new launch config to your ASG. Detatch the old instance, and this should spin up a new one.
For debugging the new instance, cd /var/log
, and tail cloud-init-output.log -f
to see output.
To zip your app, use tar cvzf [name-of-target-archive-filename.tgz] [file/path/of/source?]
#!/bin/bash
aws s3 cp s3://christie-wordpress-artifact/christie-wordpress.tgz .
yum install php php-mysql mysql-devel mysql-libs httpd -y
tar xvfz christie-wordpress.tgz -C /var/www/html/
service httpd start
chown -R apache /var/www/html
Note: don't know if we still need php-mysql mysql-libs on our instance.