Lambda Function Trigger Enabled Using Code Pipeline.

Why are you doing a lambda function trigger enabled using pipeline?

For the AWS service to invoke your function directly, you need to create a trigger using the Lambda console. A trigger is a resource you configure to allow another AWS service to invoke your function when certain events or conditions occur. Your function can have multiple triggers. Each trigger acts as a client invoking your function independently, and each event that Lambda passes to your function has data from only one trigger. By using the code pipeline we enabled our lambda function trigger when we needed it.

What is the benefit?

People don’t need to add lambda function roles permission manually and don’t need to enable trigger manually because, after policy gets attached to the particular roles then we can enable trigger and it happens by using pipeline whenever we need every time automation happens.

Continue reading “Lambda Function Trigger Enabled Using Code Pipeline.”

AWS LAMBDA – Here’s Everything You Need to Know!

What is Serverless?

To understand what AWS Lambda is, we have to first understand all about serverless architecture. The serverless architecture is a way to build and run applications and services without having to manage infrastructure. Your application still runs on servers. When you run a serverless application, you get the benefit of not worrying about OS setup, patching, or scaling of servers that you would have to consider when you run your application on a physical server.

Serverless applications or platforms have four characteristics:

  • No server management
  • Flexible scaling
  • No idle capacity
  • High availability
Continue reading “AWS LAMBDA – Here’s Everything You Need to Know!”

AWS SNS Integration with Communication Platform [Google Chat]

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.

Solution

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.

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 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.

  • 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 “<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.

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

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

url = "<WEBHOOK-URL>"

All examples & code functions present on google & AWS official document:

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.

$ ls

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.

 

  • 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 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.

Final thought

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.