Using the AWS CDK to send a serverless Slack message
In this particular tutorial, I am sharing how I used the AWS SDK to send a basic Slack message using a cron job.
This is a simple proof-of-concept that I am planning to take further by swapping out the "send Slack message" with a number of other future project plans I have for do x.
In order to follow along, you'll should have the following:
Setting up the infra dependencies
In a new file, we need to setup the npm package:
These dependencies are all essentially used for the CloudFormation file synthesis, with the addition of
dotenv so that we can add in any secrets we want locally and not store it on git.
.gitignore file and add the following:
This will keep out the pesky secrets we don't want stored in Git.
Setting up TypeScript
You can initialise with your preferred settings, however this is a pretty handy one you can add after creating a
Setting up the infrastructure code
Create a simple
index.ts file and add the following code:
So how does it work? We are simply importing all the required CDK packages to create a lambda function which occurs within the
constructor for the
The main lambda function code comes from here:
In the second argument, the
code property takes a few options, but here we are using
fromBucket. This allows us to bundle the dependencies that we need from NPM packages.
The lambda code itself is added from this code snippet:
We are telling the CDK here to bundle the
lambda folder in the root of our project and store it in a bucket as an asset. We will create this folder in the next section.
To set up the cron job, we have the following code:
This basically just says to invoke the lambda function every minute. See the AWS docs for more info.
The last bit of importance here is to note that we are adding an environment variable
SLACK_CHANNEL that is being loading from the
.env file through
require("dotenv").config() at the beginning of the file.
SLACK_CHANNEL env var we are using here is simply just a URL for a Slack webhook to send into our file. You can find out how to create that on the Slack API documentation.
.env file and add that webhook:
Creating the Lambda function
Create a new
lambda folder to use. Then, change into that folder an initialise a new
npm project and add
We will use axios to send the request to the Slack webhook. This is mainly to demonstration for how to bundle an npm package for Lambda with the CDK.
Create a new file
slack-message.js here where we will write the function. You can name the file whatever you want, but it needs to match the
handler: "slack-message.handler" property we wrote in the CDK configuration.
Add the following code to that
In the above, I basically have two helper functions for Slack
postToChannel which creates a basic text message in the appropriate format for Slack from the API documentation and uses
axios to make that request for us.
exports.handler is our function that we want Lambda to invoke when it has been called. Here, we will simply call
sendText("Lambda Cron job message") to send out message into Slack!
The name of the export also aligns to the
handler: "slack-message.handler" property in our CDK setup (it basically goes
Running the CDK
We are now ready to run the CDK!
Once the above is done, we can synthesize, deploy or destory using the
You can run
cdk help for more information, but basically what we will want to do is run the following:
You may also be required to run
cdk bootstrap if it mentions it.
If successful, we will now have our lambda function deployed to AWS! Check Slack within the next minute or so to confirm things are working as expected.
Successful Slack message from the invoked lambda function
Congratulations! You can now run
cdk destroy to teardown the AWS resources (unless you are so inclined to receiving that message every minute on Slack and enjoying the bill).
This can be reapplied for you to do whatever Cron jobs you would like! Life is fantastic. Happy coding!
See the completed project on GitHub.
Resources and Further Reading
A list of further reading or resources referenced throughout the post.
- How Zalando use lambda cron in Java
- AWS CDK GitHub example for lambda-cron
- AWS Lambda Reference
- Cron examples in AWS
- Creating a serverless app with the AWS CDK
- Warning for CDK env var parsing
- Synthesizing and destroying resources
- AWS CDK Credential Provider
- NPM website
- Setup AWS Credentials
- Slack Webhook Documentation
- Project repo for okeeffed/lambda-cron-cdk-example
Image credit: Caspar Camille Rubin
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.