How to Add Slack Alerts for
AWS Elastic Beanstalk Deploys
Published on
-/- lines long
You might want to know immediately via Slack when your AWS Elastic Benastalk deploy has been built, so you can test it out. Or, if you're blocking someone else on your team, this can be even more important. Let's see how it can be done…
By default, Elastic Beanstalk sends email updates whenever the state of your environment changes and technically, you could send emails to Slack(opens in new tab). But those updates don't really tell you when a build is ready; only when the environment health changes, which is a different thing. Furthermore, those emails are long, full of clutter, and can get spammy:
You could instead use Elastic Beanstalk platform hooks(opens in new tab) whenever a build starts and finishes to send a customized message to a channel using a Slack automation(opens in new tab).
Setting up Slack
-
Go to the Automations tab
-
Create a new Workflow
-
Set the workflow trigger to be a webhook
-
Set up variables that we'll use in our message:
-
env
for the EB environment name -
status
for whether the build is starting or has finished -
instance
for the instance ID, in case we need to troubleshoot
-
-
Click Continue, so the webhook URL gets generated
-
Add a Step to the workflow, e.g. Send a message to a channel
-
Write your message and use the previously created variables, for example:
[
{env}
] Deploy status:{status}
{instance}
-
Click Finish Up
-
Add a name and description to your workflow
-
Hit Publish
You should end up with something like this:
Now just copy the webhook trigger URL somewhere for later use in the EB platform hooks. It should look like this:
https://hooks.slack.com/triggers/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
Setting up Elastic Beanstalk
Create .platform/hooks/postdeploy/slack.sh
in your project:
#!/bin/bash
# Make sure that script exits with status code `0`, so in the event that there's
# an error with the script, Elastic Beanstalk doesn't stop.
trap "exit 0" EXIT ERR
# https://serverfault.com/a/955117/530311
env=$(/opt/elasticbeanstalk/bin/get-config container -k environment_name)
# https://stackoverflow.com/a/16527884/3130281
instance_string=$(ec2-metadata -i)
instance="${instance_string//instance-id: /}"
curl -X POST \
-H "Content-Type: application/json" \
-d "{\"env\":\"$env\",\"instance\":\"$instance\",\"status\":\"READY\"}" \
"https://hooks.slack.com/triggers/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"
The script should be self-explanatory, thanks to the comments inside of it, but essentially:
-
We
trap
, so that the script always exits with code0
and doesn't potentially interrupt the build -
We obtain the environment name and instance ID for passing later to the webhook
-
We fire the webhook with the corresponding variables
If you deploy this, you'd get messages whenever an Elastic Beanstalk finishes building a new deploy of your app.
You could also add a .platform/hooks/prebuild/slack.sh
hook in order to send messages when a build starts, as opposed to when it ends. This could be useful in situations where the build fails and you never end up getting a message.
The prebuild
hook should contain the exact same code as the postdeploy
one, except the hardcoded value for the status
variable is "BUILD"
, instead of "READY"
. It can, of course, be whatever you want. It's just text that appears later in Slack:
- -d "{\"env\":\"$env\",\"instance\":\"$instance\",\"status\":\"READY\"}" \
+ -d "{\"env\":\"$env\",\"instance\":\"$instance\",\"status\":\"BUILD\"}" \
Testing It Out
If you deploy your app with the additions in the .platform
folder, you'd get messages like these in Slack:
A nice little side-effect is that you know exactly which instances have successfully built the app. You'll also get messages when auto-scaling kicks in and new instances are added to your environment.
Conclusion
Using Slack automations and a simple bash script, you can implement notifications for your Elastic Beanstalk environment in minutes, without having to touch anything in your AWS account. This is useful if you want a quick and easy way to add transparency and improve collaboration within your team.