Tuesday, July 5, 2016

Posting Amazon Autoscaling Notifications to Slack

Dev-ops has become a little bit of a buzz-word, but its something I've come to embrace and enjoy; automating our infrastructure and deployments has streamlined our software development lifecycle.   One piece of our infrastructure is AWS Autoscaling.  With AWS Autoscaling, we can easily add new servers in response to shifting usage patterns.  For instance, during the night, we might drop down to 1 server as our site quiets down and as usage grows during the day, automatically scale up to as many as 6 servers.  Its been a useful cost savings measure and its also forced us to get our server provisioning correct through Ansible.

I wanted to know, via slack, when we scaled up our down.  AWS doesn't provide this functionality directly, but with a few hoops, it wasn't too hard to add.
  1. Setup Amazon Simple Notification Service (SNS) to receive messages
  2. Use Amazon Lambda to transform SNS messages to Slack webhooks
  3. Configure Autoscaling to send SNS messages
Because we need the Lambda function before we setup the SNS endpoint, we'll do that first

Setup Lambda Function

AWS Lambda is a snippet of your code, running somewhere in the cloud.  You don't manage the server and you only pay for the time you use.  Lambda currently supports Java, Python and Node.  We'll write a simple node script that sends an SNS message to a Slack incoming webhook.  
  1. Login to your AWS console and find the Lambda functions
  2. Create a new Lambda
  3. Skip the blueprint selection, start with a blank script
  4. Give your lambda function a name (autoscaling2Slack), optional description and pick the node runtime (4.3 as of this writing)
  5. Configure the script below as the lambda function with your slack webhook URL
  6. Create a new IAM role for the lambda function (basic execution role)

To test your lambda you can use this SNS message.  To view the real SNS messages, you can add some console.log statements to the lambda function and view them when autoscaling runs.

If you've set it up correctly, you should see the test message come through to slack:

Setup SNS Endpoint

With the Lambda in place, the SNS endpoint can be created.
  1. Login to your Amazon Console and find the SNS and create a new topic (slackSNS).
  2. Create a subscription to send to your Lambda function.
When you've got it setup, it should look something like this:

Send SNS messages when autoscaling occurs:

  1. Login to your EC2 console.
  2. In your autoscaling groups, add a new notification to your SNS topic.
When its setup, the ASG notifications should look something like this:

Wrapping up

I like slack and I hate email.  This lets me see autoscaling notifications from an SNS message that doesn't go to email.  Hopefully you've found this useful.  


I started with this, which sends Elastic Beanstalk notifications to slack: https://medium.com/cohealo-engineering/how-set-up-a-slack-channel-to-be-an-aws-sns-subscriber-63b4d57ad3ea.  The process for AWS autoscaling is _almost_ the same, but the SNS payload is different. 

No comments:

Post a Comment