Invoking a lambda function from Slack

Introduction

Pawan soni
4 min readJul 3, 2021

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.

--

--

Pawan soni
0 Followers

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