Invoking a lambda function from Slack

We use lambda functions for variety of ways to make our life easier, since they are serverless, We need some kind of way to trigger them like Http End point, cloud watch events, SQS, SNS etc or from the console itself.
However, It is not that tough to trigger / invoke the lambda from the console, but It would really be much simpler if we could just send some messages in a slack channel and it invokes the lambda function and fulfils requirement.
In this article I have tried to explain this whole process of invoking lambda from slack using AWS Chatbot.

Overview

  • Configuring AWS Chatbot to access the slack workspace.
  • Configuring the channel to interact with AWS Chatbot.
  • Configuring the role for the Chatbot.
  • Inviting the AWS Chatbot in the channel.
  • Create a simple lambda function.
  • Invoke this lambda function from slack channel.

Configuring AWS chatbot to access the slack workspace

Open AWS chatbot in the AWS console

AWS Chatbot on Console

Select Slack in the chat client and hit on configure client, then you will be redirected to slack page where you have to select the work space where you can allow the access of AWS chatbot in that workspace.

Configuring the channel to interact with AWS chatbot

Here provide the details about the channel you will be using to interact with AWS chatbot, and the role that the chatbot will assume when you hit commands from the Slack channel.
For private channel, You need to specify the channel Id, The steps to retrieve it is in the below snip.

Configuring the role for the chatbot

We will create a new role for this chatbot to assume and make sure you provide minimal read only permissions to this role and map that role to this chatbot.
We can create the role from the IAM section and select AWS Chatbot in Select your use case then hit Next: Permissions.

Provide the necessary permission to this role like (Invoke lambda function), by attaching the policies and create the role by following the next steps.

Inviting the AWS chatbot in the channel

Possibly you would have created a private / public channel for this, You can invite the aws chatbot by sending the below message in the channel you configured before.

/invite @aws

Then send below message to confirm

@aws help

You will see something like below:

Response when you send @aws help

From this point, you have configured the AWS chatbot in your slack channel and you can use this to invoke lambda functions, Alarms and do some other read only operations.

Creating a lambda function

You can create a simple lambda function from the console itself, Create a function with runtime as node 14 and name as test_function. (This configuration is for the sake of example, you could use any runtime and name)
Paste below code in the function.

exports.handler = async (event) => {

console.log(`Event in lambda function ${JSON.stringify(event)}`);
console.log(`Processing`);

const response = {
statusCode: 200,
event,
body: JSON.stringify('Hello from Lambda!'),
};

return response;
};

And hit Deploy.

Invoke this lambda function from slack channel

Send below message in the slack channel to invoke the lambda function we just created.

@aws  lambda invoke test_function --payload {"key":"value"} --region ap-south-1

Just make sure you are passing the correct region and the role has permission to invoke the lambda function.

After sending above message, You will get below response.

Hit Yes and your lambda function will be executed with similar response below.

Output: When lambda is executed successfully

This is how we can actually invoke / run a lambda function from the slack channel itself without visiting the console or hitting any http endpoint.

Conclusion

I had some use cases where I needed some way to invoke lambda function without using API gateway outside of console, that’s when I found this and wanted to share it so that It could help other people looking for similar use case. I have referenced this beautiful doc from AWS itself.
Feel free to comment / connect if you have any suggestions, questions or just to say hello, I’ll be more than happy to take them.

--

--

--

Swift learner || Developer || JavaScript || NodeJS || Cloud Services || AWS || Lambda || Cloud Solutions

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Slack app Block Kit migration guide — Part 1

Spatial Experiences

Why should every product manager learn to code?

⏰ANIMVERSE’S IDO ON ONUS & BSC STATION WILL BE READY IN A FEW HOURS

How to send welcome emails after user registration in Laravel?

Why You Must Break Down IT and Business Silos During a Transformation

3 Things I Learned While Building My First Rails App

Why Android is the best platform for on-demand App Development?

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
Pawan soni

Pawan soni

Swift learner || Developer || JavaScript || NodeJS || Cloud Services || AWS || Lambda || Cloud Solutions

More from Medium

The Art of Building Lambda Apps

How to call a Lambda Function via API Gateway on Aws Services from the mobile client by using…

Building Sustainable Software and IoT Products

Custom Lenses for the AWS Well-Architected Tool