Kevin's Worklog

Documenting the Daily Stream

A Quick and Dirty eXist-db AWS Startup Script

In lieu of using one of the more formal methods from the eXist-db Advanced Installation page, I just wanted a quick and dirty way to automatically spin up an eXist-db database, with code checked out from the eXist-db GitHub repository, when an EC2 instance was started.

My requirements? I wanted to run the Web application at port 80 and I wanted to do this on an EC2 instance that doesn’t have an Elastic IP. What follows is the quick and dirty solution I came up with. I’m not suggesting it’s better than the more formal methods, but it works and does what I wanted.

First, I started with installing some of the necessary software:

sudo aptitude install git screenie openjdk-8-jdk ant

Next, I checked out the eXist-db code and built it:

cd /home/ubuntu
git checkout https://github.com/eXist-db/exist.git
ant

Then I created a script that would configure a CNAME on my Route 53 DNS configuration, port map port 8080 to port 80, and startup the eXist database. I also created a configuration file for the AWS Route 53 connection. I put the configuration file at /etc/route53/config. It contains:

aws_access_key_id = [SOMETHING_SECRET_GOES_HERE]
aws_secret_access_key = [SOMETHING_SECRET_GOES_HERE]

The contents of the script are:

#! /bin/bash

# Make sure only root can run this script
if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root" 1>&2
  exit 1
fi

# Load AWS configuration
. /etc/route53/config

# Export access key ID and secret for cli53
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY

# Use command line scripts to get the public hostname
PUBLIC_HOSTNAME=$(ec2metadata | grep 'public-hostname:' | cut -d ' ' -f 2)

# Create a new CNAME record on Route 53, replacing the old entry if necessary
/usr/local/bin/cli53 rrcreate lisforge.net c4lse CNAME "$PUBLIC_HOSTNAME" --replace --ttl "60"

# It's okay with me if the first fails because it's never been configured before
iptables -D PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

# At startup, this script is run as root, but we don't want to run eXist-db as root
cd /home/ubuntu/exist
sudo -u ubuntu bin/startup.sh

I put the script at /usr/local/sbin/exist-startup and then put the following the /etc/rc.local file:

screenie -j exist /usr/local/sbin/exist-startup

That’s it. Now each time I start the EC2 instance with eXist-db installed, I can go to c4lse.lisforge.net and find the application running there.

If I’m on the EC2 instance and want to see the console output, I can use screenie to reattach to the session. Quick and dirty.