Use Lambda Layers To Post To Slack

Use Lambda Layers To Post To SlackChris PlankeyBlockedUnblockFollowFollowingFeb 23I have tried a few different ways of reporting Lambda errors to Slack, but haven’t found a reusable solution that gave all of the information I desired.

I decided to solve that problem by creating my own Lambda layer.

This solution doesn’t highlight the use of error logging, but is dynamic enough that you can just pass an error message into the layer.

For this to be useful to you, make sure you are familiar with the following:1.

AWS Lambda2.

Node JS3.


SlackCreate The Slack AppThe first thing you will need to do is create a Slack application that will leverage Incoming Webhooks.

Visit: https://api.


com and click the ‘Start Building’ button.

Slack API HomepageYou will be prompted to enter an App Name and to map the Application to an existing workspace:Creating A New Slack AppClick ‘Create App’ and on the next screen, select ‘Incoming Webhooks’.

Select Incoming WebhooksOn the next screen, toggle the ‘Activate Incoming Webhooks’ to ‘On’ then scroll down and click ‘Add New Webhook to Workspace’.

Once you do that, you will have to map the webhook to a channel and then you will receive a URL for your webhook.

Activating A New WebhookGreat, now we need to create our Lambda Layer that can POST to this webhook.

Copy your webhook and paste it somewhere safe, we will need it later.

Create the Lambda LayerThe file structure of your Lambda Layer is going to be the most important part of this process.

When you’re done, the file structure should look like this:slack-lambda-layer||__nodejs | |__node_modules | |__posttoslack | |-index.

js |-package.

jsonLets begin in a clean workspace, navigate to your terminal and execute the following:$ mkdir slack-lambda-layer$ cd slack-lambda-layer$ mkdir nodejs$ cd nodejs$ mkdir node_modules$ cd node_modulesThe above file structure is very important, if you do not create the nodejs directory, your Lambda will not be able to find the module.

Once you are in the node_modules folder you can create a new directory.

This directory will be the name of the package you import.

$ mkdir posttoslack$ cd posttoslack$ touch index.

js $ npm initYou can just hit enter through the npm init prompts, note the entry point is index.


Open index.

js with a text editor and paste the following:Typically, I would use Request Promise for making HTTP calls, but knowing AWS provides the https package in the lambda runtime, I wanted to keep it simple.

You could easily build on this to include the request-promise package in your Lambda layer if desired.

Double check that your directory structure looks something like this:slack-lambda-layer||__nodejs | |__node_modules | |__posttoslack | |-index.

js |-package.

jsonNow you will need to zip the nodejs directory and upload it to AWS.

Once zipped, navigate to your Lambda console in AWS and click ‘Layers’ then ‘Create Layer’.

Create A New Lambda LayerHere you can name the layer whatever you want.

The only important fields on this screen are the source code and runtime.

If you needed to create a lambda layer for a different runtime, use Amazon’s documentation to see if your file structure would need to change.

Now you should have a new Lambda Layer.

Your New Lambda LayerThe last step is for us to create a Lambda function that will use this layer.

Create The Lambda FunctionLet’s navigate back to the Lambda console and create a new function.

Remember, the runtime needs to be 8.

10 for our layer to work correctly.

Create Your Lambda FunctionYou now have the ability to add layers to your Lambda.

Click on the ‘layers’ button under your function name, and then click on ‘Add a layer’.

Add A Layer To Your LambdaPick your newly created layer and click ‘Add’.

Select The Lambda LayerMake sure to save your Lambda and switch to the code view.

You should see there is a layer associated with your lambda now.

Update the source code of the lambda to require the new layer and leverage it.

You will need the Slack Webhook URL from earlier to use as parameters for the function.

const pts = require('posttoslack');exports.

handler = async(event, context) => { return pts.



com", "/services/XXXXXXXXX/YYYYYYYYY/XXXXXXXXXXXXXXXXXXXXXXXX", 'hello from lambda');};If all went well, when you test your lambda you should get a new Slack message:Let me know your thoughts, how to make it better, or any questions you may have!.. More details

Leave a Reply