DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Build Slack App for Audit Requests
  • Docker Bake: A Modern Approach to Container Building
  • Container Checkpointing in Kubernetes With a Custom API
  • Leveraging Seekable OCI: AWS Fargate for Containerized Microservices

Trending

  • Grafana Loki Fundamentals and Architecture
  • How to Format Articles for DZone
  • Ensuring Configuration Consistency Across Global Data Centers
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Package and Deploy a Lambda Function as a Docker Container With AWS CDK

Package and Deploy a Lambda Function as a Docker Container With AWS CDK

Deploy a Serverless backend for Slack using Infrastructure-as-code (IaaC).

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
May. 09, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
36.4K Views

Join the DZone community and get the full member experience.

Join For Free

One of my previous blog posts 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 its packaging format (as a Docker container):

Go
 
// 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:

Go
 
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 the Request URL section, enter a dummy URL for now e.g. https://example.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 to 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 the Slack Config

Clone the Github repo and move into the right directory:

Shell
 
git clone https://github.com/abhirockzz/awsome-slack-backend
cd awsome-slack-backend/function

Build the Go function:

Go
 
GOOS=linux go build -o awsome

Deploy the function:

Go
 
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 Slack and update the configuration to reflect the Lambda Function URL.

Everything has been set up and configured. Now head over to your Slack workspace and invoke the Slack command you just configured! Try this:

Shell
 
/awsome serverless

Clean Up

Once you're done, you can delete the function and related resources:

Shell
 
cdk destroy

In this blog post, we covered how to ease the deployment process for our Serverless backend using AWS CDK!

AWS app CDK (programming library) Docker (software) Slack (software)

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Build Slack App for Audit Requests
  • Docker Bake: A Modern Approach to Container Building
  • Container Checkpointing in Kubernetes With a Custom API
  • Leveraging Seekable OCI: AWS Fargate for Containerized Microservices

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!