Using Amazon SNS as triggers

One of the key benefits of having a service such as SNS is that it can also be used as a trigger mechanism for a variety of use cases. Messages sent by SNS can be used to trigger simple Lambda functions that in turn perform some action over another AWS service, or simply process the message from SNS and forward its contents to another application. In this section, we will be exploring a really simple use case where an SNS topic is used as a trigger mechanism for a Lambda function to push CloudWatch alerts over to Slack! The alerts will be sent out to a custom-made Slack channel that your IT team can use to track alerts and other important notifications with regards to your AWS environment.

At a broader level, here are the list of things that we plan to do for this activity:

  • Create an SNS topic that will act as the Lambda trigger
  • Create a CloudWatch alarm for one of our EC2 machines, say, if CPU utilization goes higher than 80% then trigger the alarm
  • The CloudWatch alarm will post the notification to an SNS topic
  • The SNS topic will act as a trigger to our Lambda function
  • As soon as the Lambda function gets a trigger, it will post the notification to our Slack channel

Sounds simple? Let's get down to implementing it then:

  1. First, we will need to create a simple SNS topic which will act as a trigger for the Lambda function. Go ahead and create a simple SNS topic as we did in our earlier steps. Once completed, make a note of the SNS topic's ARN from the topics dashboard. In this case, our SNS is configured to send notifications to an email subscriber in the form of an IT admin email alias.
  2. Next up, we create our CloudWatch alarm. To do so, select the CloudWatch service from the AWS Management Console and click on Alarms in the navigation panel. Select Create alarm to get started.
  3. In this scenario, we will be monitoring the EC2 instances in our environment, so I've gone ahead and selected the EC2 Metrics option. Alternatively, you can select any other Metrics, as per your requirements. In our case, we have gone ahead and configured a simple CPUUtilization alarm, as shown in the following screenshot:
  1. Make sure that you set up a notification for the alerts and point it to the newly created SNS topic, as shown in the following screenshot:

With the SNS topic and CloudWatch alarm in place, we now need to configure a Slack channel where the alert notifications will be posted. For that, we will need an incoming webhook to be set and a hook URL that will be used to post the notifications:

  1. Go to your Slack team's settings page and select the Apps & integrations option, as shown in the following screenshot:
You can sign up for a free Slack account at https://slack.com/get-started.
  1. Once you click on Apps & integrations, it will take you to a new page which lists a variety of pre-configured apps. Search for Incoming and select the Incoming Webhooks from the options that appear.
  2. Next, click on Add Configuration. It will ask you to select the Channel to post, along with a few other necessary parameters. Make sure that you copy and save the Webhook URL before you proceed any further with the next steps.

Now that we have our Slack hook URL ready, we can finally get started with deploying our Lambda function. For this exercise, we will be using an existing AWS Lambda function blueprint designed for Slack integration, using the Node.js 4.3 version:

  1. From the AWS Management dashboard, filter the service Lambda using the Filter option, or alternatively, select https://console.aws.amazon.com/lambda/home.
  2. From the AWS Lambda landing page, select the Create a function option to get started.
  1. For working with Lambda functions, you can choose to create your own function from scratch, or alternatively, filter and use a function from a list of predefined and configured blueprints. In this case, select the Blueprints option and use the adjoining blueprints filter to search for the following function: Blueprint name: cloudwatch-alarm-to-slack (as shown in the following screenshot):
  1. Select the blueprint and fill out the necessary information for your function, such as its name, role name, and so on. Once done, from the SNS section, select the newly created SNS topic from the drop-down list.
  2. Remember to select the Enable trigger checkbox before proceeding with the next steps.
  3. Finally, in the Environment variables section, provide the appropriate values for the slackChannel and kmsEncryptedHookUrl parameters, as shown in the following screenshot. Remember, the kmsEncryptedHookUrl is nothing but the Slack hook URL that we created a while back:
  1. With the values filled in, simply select the Create function option and let the magic begin!

Based on the selected CloudWatch metric for your alarm, go ahead and create some synthetic load for your EC2 instance. Once the load crosses the set threshold in the alarm, it triggers a corresponding message to the SNS topic, which in turn triggers the Lambda function to post the alert over on the Slack channel. In this way, you can also use the same SNS topic for subscribing to various other services, such as Amazon SQS, for other processing requirements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset