Opsworks provisioned Auto Scaling Groups
TL;DR
Provision your automatically scaled EC2 instances using Opsworks
Summary
I’ve been using Cloudformation/Opsworks for some time to (almost) completely automate the launch of new infrastructure. Chef’s Supermarket provides us with a lot of off-the-shelf recipes that are easy to use.
However Opsworks misses a crucial (imo) feature which is application-based auto healing, the one that it possesses only performs health checks on the instance itself and not on an application being run. This is kinda weird since it knows the ELB status and could perform the same auto-healing that is applying when the Opsworks agent fails to ping home. As the AWS documentation tells us:
Every instance has an AWS OpsWorks agent that communicates
regularly with the service. AWS OpsWorks uses that
communication to monitor instance health. If an agent does
not communicate with the service for more than approximately
five minutes, AWS OpsWorks considers the instance to have failed.
This is where Auto Scaling Groups comes in, it does exactly that, you can configure a ELB health check timeout, if that expires on an unhealth instance it’s taken down and a new one is launched to replace it. ASG are linked to Auto Scaling Configurations which basically are shell scripts that are run when the instance is launched, so why not marry the two and have the best of both worlds?
Just configure a normal AS configuration plus a AS group, you will use however a special script that registers the new instance with Opsworks and request for provisioning. There are a few gotchas though, the first being that since the auto-healing functionality will be implemented by ASG you must disable Opsworks auto-healing.
If you’re on a VPC you’ll run into an Ohai issue where it fails to detect that it’s running on an EC2 instance, the symptoms involve your recipes failing due to not being able to extract ec2 data from the node object. We fix this by creating an empty file that helps Ohai determining that is indeed on an EC2 instance.
We’ll need jq so install it right away
There are two phases for Opsworks provisioning, first we must register the instance and then assign it to a layer.
I’m using a form of convention over configuration so there are only four inputs to the script: Product, Environment, Platform and Major version, all names are derived
from these values.
The Opsworks stack name is an example of this: v$MAJOR $ENVIRONMENT $PLATFORM
.
Autoscaling group instances are launched with no name, so we ask Opsworks for one and register the instance.
Now a bit of busy waiting until the registering process is complete.
And finally assign the instance to the desired layer, this is the bit where the Chef recipes will be run.
And that’s it, if for some reason your application crashes ASG will take over and launch a new instance that will be provisioned by Opsworks.
tags: