We recently started working with Slack to streamline workflows and try some ChatOps. While pushing our Jenkins and Shippable updates to Slack was accomplished with a single cURL command, we also wanted our Elastic Beanstalk notifications to show up in some particular channels.
I’ve been wanting to try out AWS Lambda for some time now, and it just so happens that SNS (which Elastic Beanstalk uses for notifications) supports piping directly to a Lambda function.
Environr
The future of environment configuration is here.
Log into the AWS console, browse to the Lambda section and create a new function.
Name the function whatever you’d like — I’ve chosen “slackNotify”.
Make sure “Edit code in-line” is selected and paste the Gist below, replacing both instances of #mychannel and the Slack hook URL with your own:
Save the function, but don’t invoke it, otherwise you’ll get an error since the sample data won’t match what SNS will provide.
2. Click “Topics” and locate the topic for your Elastic Beanstalk environment. If you don’t have one, create a new topic and give a descriptive name that associates it with your environment.
3. Click the topic link and then “Create Subscription”.
The “Topic ARN” should already be filled in for you, but you’ll need to change “Protocol” to “AWS Lambda”. If you had to create a new topic, highlight the ARN and copy it to your clipboard (you’ll need it below). Now, the “Endpoint” drop-down should populate with the Lambda function we just created.
4. Choose the function and click “Create Subscription”.
If you didn’t already have an SNS topic associated with your Elastic Beanstalk environment, we’ll need to associate it with the topic you just created. The simplest way to do this is with the AWS CLI:
Armed with the ARN in your clipboard, we can associate it with our Elastic Beanstalk environment (Note: the next command will update your Elastic Beanstalk environment, potentially disrupting service. Use with caution!):
Replace ENVIRONMENT_NAME and TOPIC_ARN with the appropriate values and if that succeeds, you’ll see a JSON response letting you know your environment is updating.
If all goes well, your next successful or failed deployment should be posted to the Slack channel configured in your Lambda function.
— the behavior-driven marketing platform for developers.
3 Responses to “Post Elastic Beanstalk Notifications to a Slack Channel”
Igor
Thank you! Just set up slack notifications using your manual.
Igor
Thank you! Just set up slack notifications using your manual.
delemach
This code was great, and worked for us, but then mysteriously stopped working around Feb 5th.
I fixed it to set additional https request options, and to pass the payload according to slack documentation, and it works again. Here is the updated gist: https://gist.github.com/delemach/845a1a6dedf1c0a8cfd5
Miguel Mota
Awesome post. I used @delemach’s updated code and it worked great!