Back to Garden
Guides|January 22, 2024

Integrating AWS Lambda and AWS SQS with Slack: A Step-by-Step Guide

#AWS#Serverless#Messaging

AWS Lambda is a serverless computing platform that lets you run your code without provisioning or managing servers. Slack, on the other…

AWS Lambda is a serverless computing platform that lets you run your code without provisioning or managing servers. Slack, on the other hand, is a real-time communication tool that helps you keep track of important updates, messages, and notifications. By integrating these two platforms, you can create an automatic and real-time notification system for your applications. In this article, we will show you how to integrate AWS Lambda with Slack and send messages to a Slack channel through an AWS Simple Queue Service (SQS) topic.

Step 1: Preparation

Before getting started, you need to create an SQS topic and a Slack Webhook URL. The SQS topic is a message queue that acts as a buffer between your application and the Slack channel. The Slack Webhook URL is a unique URL that is used to send messages to your Slack channel.

Step 2: Create a Lambda Function

Go to the AWS Lambda dashboard  
Click on “Create function”  
Select “Author from scratch”  

Fill in the required fields and click on “Create function”.

Step 3: Writing the Lambda Function

Now that we have our SQS topic and Slack webhook set up, we can start writing our Lambda function. Our Lambda function will trigger whenever an event is sent to our SQS topic and it will then send a message to our Slack channel via the webhook.

Here’s what the code for the Lambda function should look like:

import https from 'https';  
const doPostRequest = (record) => {  
  const data = { text: record }  
  return new Promise((resolve, reject) => {  
    const webhookUrl = process.env.SLACK_WEBHOOK_URL;  
    const options = {  
      hostname: 'hooks.slack.com',  
      port: 443,  
      path: webhookUrl,  
      method: 'POST',  
      headers: {  
        'Content-Type': 'application/json'  
      },  
    };  
    //create the request object with the callback with the result  
    const req = https.request(options, (res) => {  
      resolve(JSON.stringify(res.statusCode));  
    });  
    // handle the possible errors  
    req.on('error', (e) => {  
      reject(e.message);  
    });  
    //do the request  
    req.write(JSON.stringify(data));  
    //finish the request  
    req.end();  
  });  
};  
export const handler = async (event) => {  
  await doPostRequest(event.Records\[0\].body)  
    .then(result => console.log(\`Status code: ${result}\`))  
    .catch(err => console.error(\`Error doing the request for the event: ${JSON.stringify(event)} => ${err}\`));  
};

Let’s break down the code:

We first import the https module that we’ll use to send the POST request to Slack.

In the doPostRequest function, we define the data that we want to send to Slack. In this case, it’s the message contained in the SQS event (record).

We then return a promise that sends a POST request to the Slack webhook URL (which we’ll set as an environment variable). We also set up some options for the request, such as the hostname and port, and the headers.

In the handler function, we call the doPostRequest function and handle any potential errors.

Step 4: Deploying the Lambda Function

Once we have our code written, it’s time to deploy the Lambda function. To do this, we can use the AWS CLI or the AWS Console. Once the function is deployed, we need to set up the environment variable for the Slack webhook URL.

Step 5: Testing the System

To test our system, we can send a message to the SQS topic. If everything is set up correctly, the Lambda function should trigger and send a message to our Slack channel.

Conclusion

By using AWS SQS and a Lambda function, we can easily send messages from any system or application to a Slack channel. This approach abstracts the Slack implementation from the code, making it easy to update or change the notification system in the future if needed. By following the steps outlined in this article, you should be able to set up a similar system for your own use case.