Better CFN Signal
This utility reports success or failure of a new instance deployment to CloudFormation. It is intended to be used at the tail end of userdata. The Amazon cfn-signal requires a few arguments, including CF Stack ID, Stack Resource Name, and the AWS Region. This requires effort and is not "batteries included", in the event a user just fires up a new CF stack and does not update UserData.
This utility derives this information from the instance's tags. The idea here is you give your EC2 an Instance Role capable of reading its own tags, we read them and determine the ResourceID and Cloudformation Stack, rather than having to pass this information via UserData.
Required Tags
Both of these tags are automatically applied to the EC2 instance upon creation via CloudFormation.
aws:cloudformation:logical-id
aws:cloudformation:stack-name
Required IAM Permissions
The EC2 must also be able to read its own tags, as well as use the CloudFormation SignalResource API.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudformation:SignalResource",
"ec2:DescribeTags"
],
"Resource": "*"
}
]
}
Sample Userdata
Linux
#!/bin/bash -e
echo 'Do some stuff...'
# Signal Success
better-cfn-signal
Windows
<powershell>
$ErrorActionPreference = "Stop"
Write-Host 'Do some stuff...'
# Signal Success
better-cfn-signal
</powershell>
Healthcheck Support
Optionally, Better CFN Signal can wait for a URL to return a 200 prior to sending a healthy response back to CloudFormation.
This was intended for use with the Go-Healthz healthcheck daemon. The concept is similar to Kubernetes Startup Probes.
#!/bin/bash -e
echo 'Do some stuff...'
# Signal Success after waiting up to 10 minutes for the Healthcheck URL to return 200
better-cfn-signal -healthcheck-url http://127.0.0.1:8080 -healthcheck-timeout 10m