Invoke AWS Lambda from Slack

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code only when needed and scales automatically, from a few requests per day to thousands per second. AWS Lambda in our task is invoked using the API Gateway.

Slack is a channel-based messaging platform. With Slack, people can work together more effectively, connect all their software tools and services, and find the information they need to do their best work — all within a secure, enterprise-grade environment.

Invoke lambda function through API Gateway from Slack

This blog covers on how to invoke AWS Lambda function through API Gateway when there is a new message posted to any Slack channel.

Here are a few business cases where this can be used:

  1. If a Quality Analyst wants to trigger an integration test by sending a message via Slack.
  2. If a Developer wants to get logs of a Load Balancer which is managed by the Security Team.

Here are the steps to be followed….

Step1: First let’s configure Slack

Create a workspace in, then create a slack app at in the newly created workspace and then go to OAuth & Permissions tab, add channels:history, channels:read, chat:write, im:history & mpim:history OAuth scope to Bot Token Scopes and User Token Scopes. Now install your app to workspace and allow necessary permissions, then User OAuth Token & Bot User OAuth Token are generated.

Bot and User Token Scopes

OAuth & Permissions page

And also keep the copy of verification token which is present in the Basic Information tab of your app.

Access Verification Token from Basic Information page

Step 2: Now let’s move on to AWS Lambda configuration part

Let’s create a Lambda function using Author from scratch lambda function template. Select runtime as Node.js and Create a new role with basic Lambda permissions.

Creating Lambda function in AWS Lambda Console

Now your lambda function is created. In the code tab, change index.js file as follows.

//Verify Url -
function verify(data, callback) {
    if (data.token === VERIFICATION_TOKEN) callback(null, data.challenge);
    else callback("verification failed");   
//Print the slack message on the console
function process(data,context, callback) {
    console.log('context:', JSON.stringify(context));
// Lambda handler
exports.handler = (data, context, callback) => {
    switch (data.type) {
        case "url_verification": verify(data, callback); break;
        case "event_callback": process(data,context, callback);break;
        default: callback(null,"Hello from Lambda");
[APP VERIFICATION TOKEN]: Access this token in the basic information tab of the slack app created in Step 1(Refer to Access Verification Token from Basic Information page figure in Step 1).
[SLACK ACCESS TOKEN]: Access Bot User OAuth Token from OAuth Tokens for Your Team in OAuth & Permissions page(Refer to OAuth & Permissions page figure in Step 1).

Step 3: In this step, API Gateway is configured

Add a REST API gateway trigger to the lambda function and select Security as Open. In the additional settings, change the deployment stage name to prod(Optional).

After the trigger is added, go to API gateway console, add method POST to the resource. After creating POST method, then select Deploy API option from the Actions menu.

Add POST method to the SlackLambda resource

Select Deploy API

Select the Deployment stage which was created in the earlier step, then click deploy

Deploy API to prod stage

Step 4: Slack Configuration post AWS Lambda/API Gateway configuration

From Slack App(, select the newly created app. Then go to Event Subscriptions and Enable Events. In the Request URL textbox, paste the API gateway URL which can be accessed from the configuration tab of Lambda function.

Enabling EVents in Event Subscription page

Proceed once the URL is verified. Scroll down and subscribe to message:channels, & message.mpim bot events and events on behalf of users. Then go back to OAuth & Permissions tab and Reinstall app to workspace.


Now let’s test it!

Open and Launch the workspace where the app is created then post the message to a slack channel or a direct message.

Message posted to slack channel

Now check the message and metadata of the message in the cloud-watch log console.


To conclude, When someone posts a message to a slack channel, the API gateway triggers the lambda function and cloud watch logs has a record of all the Lambda invocations.

We have come to an end of this blog. Hope you liked it, please do share it with your friends. Happy Reading!

Siya Amonkar is a DevOps Engineer who likes to explore new tools and technologies. She can be reached out on LinkedIn or via email

Varsha is a software engineer with overall experience of 2 years, who loves to play with devops tools and is a cloud computing enthusiast. She can be reached out on LinkedIn or via email