Deploy a Serverless backend for Slack using Infrastructure-as-code (IaaC)
One of my previous blog post covered how to build a Serverless backend for Slack using by using Lambda Function URL as a webhook. Since I wanted to focus on the application itself, the infrastructure setup part was simplified - using AWS CLI, the function was packaged as a zip file, configured and finally a Function URL was created along with the required permissions.
In this blog, you will end up deploying the same solution, but this time using IaaC (Infrastructure-as-code) with AWS Cloud Development Kit (CDK) which is a framework for defining cloud infrastructure in code and provisioning it through AWS CloudFormation. You can choose from a list of supported programming languages (at the time of writing - TypeScript, JavaScript, Python, Java, C#/.Net, and Go (in developer preview)) to define your infrastructure components as code, just like you would with any other application!
You will learn how to use the Go CDK library to deal with the infrastructure components:
- Define a Lambda function,
- Add a Lambda Function URL, and,
- Deploy the function as a Docker container (not a zip file)
By the end of this blog post, you should have the same setup as described in the earlier blog.
The code is available on GitHub, as always!
Pre-requisites
- Create an AWS account (if you do not already have one) and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- Install AWS CDK
- Setup Docker
- Install Go
- Install Git
- Create a Slack workspace if you don't have one.
- Create a GIHPY account (it's free!) and create an app. Each application you create will have its own API Key.
CDK for IaaC - quick walkthrough
The CDK code is pretty succinct but it gets the job done! Let's go through it quickly.
First, we define the function and it's packaging format (as a Docker container):
// environment variable for Lambda function
lambdaEnvVars := &map[string]*string{slackSecretEnvVar: jsii.String(slackSecret), giphyAPIKeyEnvVar: jsii.String(giphyAPIKey)}
function := awslambda.NewDockerImageFunction(stack, jsii.String("awsome-func-docker"), &awslambda.DockerImageFunctionProps{FunctionName: jsii.String(functionName), Environment: lambdaEnvVars, Code: awslambda.DockerImageCode_FromImageAsset(jsii.String("../function"), nil)})
Notice how NewDockerImageFunction has been used (traditionally, one would use NewFunction and refer to a zip file for deployment). In this case, we point to the folder where our function code resides (using DockerImageCode_FromImageAsset).
For the function to be packaged as a Docker image, I used the Go:1.x base image (see Dockerfile). But, you can explore other options as well. During deployment, the Docker image is built locally, pushed to a private ECR registry and finally the Lambda function is created - all this, with a few lines of code!
The Function URL bit is straightforward using NewFunctionUrl:
funcURL := awslambda.NewFunctionUrl(stack, jsii.String("awsome-func-url"), &awslambda.FunctionUrlProps{AuthType: awslambda.FunctionUrlAuthType_NONE, Function: function})
For the purposes of this sample app, we're using
NONE
as the authentication type. This means that the Lambda function URL will be publicly accessible
Create/Configure the command in Slack
Start by signing into your Slack Workspace and creating a new Slack App.
Once that's done, create a Slash Command - head to your app's settings page, and then click the Slash Commands feature in the navigation menu. You'll be presented with a button marked Create New Command, and when you click on it, you'll see a screen where you'll be asked to define your new Slash Command with the required information.
Enter the required information:
-
/awsome
for the Command. - In Request URL section, enter a dummy URL for now e.g. https://comingsoon.com
This is temporary and will be replaced by the Lambda Function URL after deployment
Finally, install the app to your workspace - click the Basic Information feature in the navigation menu, choose Install your app to your workspace and click Install App to Workspace. This will install the app to your Slack workspace to test your app and generate the tokens you need to interact with the Slack API.
As soon as you finish installing the app, the App Credentials will show up on the same page. You need to grab your Slack Signing Secret from there
Make a note of your app Signing Secret as you'll be using it later
Deploy the function using CDK and update Slack config
Clone the Github repo and move into the right directory:
git clone https://github.com/abhirockzz/awsome-slack-backend
cd awsome-slack-backend/function
Build the Go function:
GOOS=linux go build -o awsome
Deploy the function:
export SLACK_SIGNING_SECRET=<enter the slack signing secret>
export GIPHY_API_KEY=<enter the giphy API key>
cd ../cdk && cdk deploy
Make a note of the Lambda Function URL that you receive as an output
Go back to the Slack and update the configuration to reflect the Lambda Function URL.
Everything has been setup and configured. Now head over to your Slack workspace and invoke the Slack command you just configured! Try this:
/awsome serverless
Cleanup
Once you're done, you can delete the function and related resources:
cdk destroy
Wrap up!
In this blog post, we covered how to ease the deployment process for our Serverless backend using AWS CDK!
Top comments (0)