Skip to content

Instantly share code, notes, and snippets.

@christiearcus
Last active January 18, 2017 05:35
Show Gist options
  • Save christiearcus/119ac56fe0173457bd7d7eb214c19b86 to your computer and use it in GitHub Desktop.
Save christiearcus/119ac56fe0173457bd7d7eb214c19b86 to your computer and use it in GitHub Desktop.
Ops weekly hangs 02

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.

We will need:

  • 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.

Steps:

  1. Create a new RDS instance in the AWS console (choose MySQL).
  2. 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.
  3. In your RDS instance, add all of the login details to the database as per the Wordpress configuration.
  4. Zip all of the contents of the /var/www/html directory in the EC2 instance in to a tarball, and use scp to download it to your local machine.
  5. Create an S3 bucket and upload the tarball file.
  6. 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.
  7. Copy your old launch config, and modify with the new IAM role.
  8. New 'user data' file to download from the S3 bucket, instead of Wordpress.org (see below for script).
  9. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment