AWS SNS Integration/cloudwatch alerts with Google Chat

AWS SNS is very straightforward and as it does not have complex configuration while setting up.But still with plain configuration and direct setup, it doesn’t support things like webhook URLs. Communication platform notification like Slack/Google Chat is one of the things that it does not support directly. So, to solve this problem we need something which integrates with AWS SNS to solve these kinds of problem.

How to solve this :-

If we talk about integration, AWS SNS supports multiple integrations and one of the main integrations that we are looking for is- AWS Lambda & AWS Chatbot.

Let’s talk about AWS Lambda.

An AWS resource which can solve communication platform issue for both slack & google chat is AWS Lambda. In simple words, AWS Lambda is an event-driven, serverless computing platform which means that in AWS Lambda, the code is executed based on the response of events triggered from AWS resources like API Gateway, S3, Kinesis, and many more.

Requirements

  1. AWS SNS topic
  2. AWS SNS subscription
  3. AWS Lambda
  4. Google chat webhook URLs

Steps

  • Google Chat Webhook creation

First, we will create a webhook URL for Google chat. For that, we will create a room or if you have already a room created, skip the first and second steps. If the user already has a webhook URL present, skip the Google chat webhook creation part.

Step 1: To create a google chat workspace, click on the “+” sign under “workspace”.

Step 2: Provide Google chat workspace name and let other options be set as default.

Step 3: Once workspace created, click on drag down menu of Google chat workspace and click on “Manage Webhooks”.

Step 4: There will a pop-up of “Incoming webhooks”, it will ask for the webhook name & avatar/image URL.

Currently, we generated a webhook URL which will be later used in the Lambda function.

  • AWS Lambda

at the moment, we are setting up plain Lambda function which will later modify with custom code.

Step 1: Under the AWS Lambda dashboard, click “Create function” to start the process of creating Lambda function.

While setting-up Lambda function, the first option it will request for base template of Lambda function like “Author from scratch”, “Use a blueprint” etc. Choose “Author from scratch” for now which will later replace by custom code.

Give Lambda function name and select language for function as AWS Lambda support multiple programming languages. It all depends on the user’s expertise and understanding of a specific programming language. Leave the other options default. For now, we are using python 3.8

Once you click “create function”, AWS will create a Lambda function with basic layout with default configuration.

  • Lambda function code

we will create a zip bundle which will contain custom function and custom code dependencies ,which will replace base function template of Lambda function that we created.

Step 1: Create a python “cloudwatch.py” and add below code under that python file.

The custom code contains Lambda function base and under that Lambda function base, there is hardcoded webhook URL of Google chat workspace which is triggered by Lambda handler.

Step 2: The only thing user will have to provide is the webhook URL in the code which is hardcoded in the function.

from httplib2 import Http
from json import dumps

def lambda_handler(event, context):
url = "<WEBOOK-URL>"
bot_message = {'text' : event['Records'][0]['Sns']['Message']}
message_headers = {'Content-Type': 'application/json; charset=UTF-8'}
http_obj = Http()
response = http_obj.request(
uri=url,
method='POST',
headers=message_headers,
body=dumps(bot_message),
)
return response

url = "<WEBHOOK-URL>"  This needs to be changed

For more details you can refer Below Link

https://developers.google.com/hangouts/chat/how-tos/webhookshttps://docs.aws.amazon.com/lambda/latest/dg/with-sns-create-package.htmlhttps://aws.amazon.com/premiumsupport/knowledge-center/sns-lambda-webhooks-chime-slack-teams/

Once you create python file “<NAME>.py” and added custom code & substitute “<WEBHOOK-URL>” with Google chat webhook URL, We need to install all dependencies because AWS Lambda supports only a few libraries that don’t need extra steps to configure code dependencies. So, we need to install dependencies like httplib2 and Http that AWS Lambda doesn’t support in a specific folder and create a zip bundle of python code along with dependencies.

Step 3: Gather “httplib2” & “requests” which is Dependent libraries of custom code using pip command with “-t” tag.

$ sudo pip3 install httplib2 -t .
$ sudo pip3 install requests -t .

Using “-t” tag with pip command, pip command installed all dependent packages under the current directory that we mentioned, Check for all dependent libraries under the code directory.

Step 4: Once you get python code with dependent packages under specific directory. We will bundle custom python code with dependent packages together and create zip file which will later replace default template of Lambda function.

$ zip -r integration_code.zip .

Note down the python code file name & handler name that we specified. we have to change it according to our code file name it will be same as code file name ,In my case, “lambda_function” is filename & “lambda_handler” is handler/function name. Both are default as per AWS Lambda.

  • Uploading python zip bundle

We created Lambda function & zip bundle of custom code with dependent packages. Now, we will upload generated zip bundle to a Lambda function that we created.

Step 1: On the right side of Lambda function under “function code”, click on “Actions” and select “upload a .zip file” and upload the python zip bundle that we created.

Step 3: Under “Runtime settings” section of Lambda function, change the runtime setting according to filename & handler name. This setting needs to be accurate & correct. Otherwise, your function will not call and generate error rather than functioning properly.

For the time being, we are not changing anything. we used the default file path name & function name.

We are done with Google chat integration with AWS Lambda by uploading the python code bundle under the specified AWS Lambda function.

  • Manual event Trigger for testing purpose :-
  • At the moment, we create a “testing event” which will be used to check whether our code is working fine or not by triggering manual event using Lambda dashboard. AWS Lambda already provides multiple templates which basically contains JSON format output event response to test specific integration between Lambda and AWS resource [like SNS]. We will create a manual event and use default SNS JSON event response which will use in manual trigger.

Step 1: Under Function code of AWS Lambda, click on “Test” and “Configure test event”.

Step 2: Click “create new test event” and under Event template select “Amazon SNS Topic Notification”

Step 3: After selecting the SNS template, give a proper event name. Once you provide all values, click “Create”

Step 4: Once you create test event having SNS JSON event response template, under “Function code” click “drop-down arrow” and select the test event that you created. Once you select specific test event, click “test” to execute Lambda and wait for Lambda to execute the function and generate execution result

Step 5: Once Lambda executed, it will prompt the execution result that the Lambda function executed successfully [ gave 200 status ] & it sends a response or bot message to google chat webhook.

in my case this msg was added during publish of subject.

We have accomplished Lambda function containing custom code which integrated with Google chat webhook URL.

  • Now coming to AWS SNS

We will create a Simple notification service topic or SNS topic and create a SNS subscription that will integrate with AWS Lambda. As of now, we are creating SNS topic without SNS subscription attached to it.

Step 1: To create SNS topic, Under Topic of Amazon SNS, Click “topic” and under that, click on “create topic”

Step 2: Provide configuration details like “topic type”, “Topic Name” & “display name” and let other options be set as default.

Once topic created, you will see that there is no subscription defined or found under specific topic. Subscription is responsible for integration between AWS SNS topic and other resources like Lambda, email, etc. We’re now done with the AWS SNS topic part which is used to publish messages but there is no integration between AWS SNS & AWS Lambda. For that, we need an AWS SNS subscription that attaches to a specific SNS topic and AWS Lambda resource.

  • SNS Subscription for AWS SNS & Lambda Integration

Under this section, we will create a AWS SNS subscription which is basically responsible for integration between AWS SNS topic & AWS Lambda.

Step 1: Under the SNS dashboard, Click “create subscription”

Step 2: Provide topic ARN that we created, provide “AWS Lambda” as protocol, and select endpoint of AWS Lambda that we created

Step 3: Once SNS subscription created, it is configured under the specified topic that you provided while creating SNS subscription

Step 4: Check SNS topic subscription list, you will see Endpoint & Lambda protocol specified subscription

Step 5: Now, go to the Lambda dashboard and open the specified Lambda to check the trigger

You will see that after creating subscription with a specified Lambda protocol, it automatically creates a trigger for AWS Lambda as shown in the below diagram. We are almost done with everything. Let’s manually “publish message” to make sure everything is working fine from AWS SNS to AWS Lambda.

  • Manual publish message

AWS SNS topic support manual trigger to “Publish message” which takes basic information like subject, TTL & message body.

Step 1: Under SNS topic section, select SNS topic click “publish message” for a manual trigger from SNS

Step 2: Provide “subject” & “raw message” and let other options default. After providing values, click on “Publish message”.

After that, it will prompt for other messages and provide you the message ID which you can use to debug things on the AWS Lambda side.

Step 3: Once message published successfully, go to Google chat workspace to check whether you receiver notification or not.

Once message published successfully, the AWS SNS & Google chat workspace integration part completed.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store