As a developer or tech geek, when technology is part of your lifestyle or work, we definitely look forward to exploring all developer things. APIs & libraries are one of the important things we generally look for.
Why is it so? Because, when we use that specific technology on a daily basis, we definitely want to automate most of the things. For that, we try to explore its functional part just to make our work easy. We can use that functional part [ API/Library ], to make an automation script or application.
As the name mentioned “simple”, AWS SNS is very straightforward and has an uncomplicated setup [as it does not have complex configuration while setting up]. But with plain configuration and straightforward 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 complication we need something which integrates with AWS SNS to solve these kinds of issue.
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 Chatbot. AWS Chatbot is an interactive agent that makes it easy to monitor and interact with your AWS resources in your Slack channels and Amazon chime chat rooms. With AWS Chatbot, you can receive alerts, run commands to return diagnostic information, invoke AWS Lambda functions, and create AWS support cases. But the main flaw of AWS Chatbots is that it only supports Slack & Amazon chime.
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.
AWS SNS topic
AWS SNS subscription
Google chat webhook URLs
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 room, click on the “+” sign under “ROOMS”.
Step 2: Provide Google chat room name and let other options be set as default.
Step 3: Once room created, click on drag down menu of Google chat room 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.
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 “<NAME>.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 room which is triggered by Lambda handler.
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.
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 python_code.zip .
Note down the python code file name & handler name that we specified. In our 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.
it is giving a warning that code & specified libraries should now be greater than “10MB”, otherwise we need to upload code using the S3 bucket. For the time being, the size of zip bundle is not greater than 10MB.
Once uploading process completed, it will give a pop-up of “code change”, click “OK”. AWS Lambda automatically unzip the code bundle and place content into the root directory of Lambda function which is “opstree-function” in our case.
Step 2: Check all uploaded files & folders and also check the code that you specified.
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
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.
Step 6: At the moment, Lambda executed successfully which means, it trigger bot message to Google chat room webhook URL. Check Google chat room for testing response
We have accomplished Lambda function containing custom code which integrated with Google chat webhook URL.
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 above 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 room to check whether you receiver notification or not.
Once message published successfully, the AWS SNS & Google chat room integration part completed.
The above representation is not fully configured because only manual triggers take place. It is basically use to check the response & behaviour and also use to check the working of resources and their integration . We need to integrate AWS SNS topic to specific AWS resources like Cloudwatch alarm to automatically generate message depending on the criteria and relay those generated message through AWS SNS which will later transfer to Lambda function in the JSON format which trigger the Webhook URL. The above blog/context is for Google chat webhook URL which can be replace by other communication platform webhook URL which definitely require few changes in custom code.