question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CDK automated zip creation of Lambda function with custom library dependencies

See original GitHub issue

Use Case

AWS CDK currently supports 3 kinds of “Assets”:

  • InlineCode - useful for one-line-lambdas
  • AssetCode - one-file lambdas without dependencies
  • S3Code - existing lambda packages already uploaded in an S3 bucket

There is, however, no support for more complex lambda function which require third party dependencies. So, in this case I need to zip by myself my Lambda function with all it’s custom libraries. However, I’m also working with Chalice, and there they solved that issue by automating the zip creation (which is awesome!).

While doing research on the matter, I stumbled across this: https://gitlab.com/josef.stach/aws-cdk-lambda-asset This great guy solved the matter in less than 150 lines by creating a new construct which creates a zip for you from your lambda and adds only the custom libraries (he doesn’t add the aws libraries).

I think it would benefit the community a lot if CDK took the same approach as Chalice in the matter and formalized this process, instead of having each developers add a few steps per Lambda function to the build process in their projects. It would be more native and easier to maintain.

Another bonus here is that his solution allows to keep track of dependencies and version control since you have a requirements.txt file.

Proposed Solution

Implement a new construct which works in the same matter this guy wrote: https://gitlab.com/josef.stach/aws-cdk-lambda-asset

He described his solution as such:

CDK AssetCode which builds lambda function and produces a ZIP file with dependencies.
    Lambda function is built either in Docker or natively when running on Linux.

There is, however, no support for more complex lambda function which require third party dependencies.
This repository presents one possible approach to lambda packaging.
The construct is aware of libraries bundled in the AWS lambda runtime and automatically removes those for you to save space.
It also counts with compiled C dependencies such as NumPy and takes care of library stripping.

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:45
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
w2fongcommented, Jun 4, 2020

Here is a simple solution I found. First I created a top-level directory to store all my lambda functions. Second, I added package.json with some quick scripts which allow me to install npm modules for all my lambdas store at a top-level lambda directory. I then use the “aws-s3-assets” to manage and upload the lambda bundle and this easily worked with lambda.Code.fromBucket.

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as assets from '@aws-cdk/aws-s3-assets';
import * as path from 'path';

export class CdkStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const myLambdaAsset = new assets.Asset(this, 'lambda.zip', {
      path: path.join(__dirname, '..', '..', 'lambda', 'myLambdaFunction'),
    });

    const myLambdaFunction = new lambda.Function(this, 'myLambdaFunction', {
      code: lambda.Code.fromBucket(myLambdaAsset.bucket, myLambdaAsset.s3ObjectKey),
      runtime: lambda.Runtime.NODEJS_12_X,
      handler: 'index.handler',
    });

  }
}
1reaction
esteban-uocommented, Oct 20, 2020

#7898 relies on docker and its for nodejs, why closing it? seems to be for now the best solution is using what josef.stach created.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Ways To Bundle a Lambda Function Within an AWS CDK ...
This post explains how to bundle or include a Lambda function within a CDK construct if your function requires external dependencies.
Read more >
aws-cdk/aws-lambda module - AWS Documentation
This construct library allows you to define AWS Lambda Functions. const fn = new lambda.Function(this, 'MyFunction', { runtime: lambda.Runtime.
Read more >
Deploying AWS Lambda layers with Python - Medium
Create a folder to store the Lambda function code and the dependencies for the Layer. cd ~/aws-lambda-layer-cdk mkdir lambda/layer mkdir lambda/ ...
Read more >
How can I zip Node Lambda dependencies in AWS CDK stack?
It automatically transpiles and bundles the Lambda code and dependencies. Instead of defining a package.json for every Lambda function, dependencies can be ...
Read more >
How to Create an AWS Lambda Function with CDK ... - YouTube
In this video, I walk you through the steps to create an AWS Lambda function using CDK. I walk you through the process...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found