1
votes

What options do I have if I want to redistribute a Lambda function as a CDK construct? By this, I mean a construct that someone else could use in their project that might define one or more Lambda functions.

Assuming that the CDK construct is written in Typescript, the construct itself can be packaged for use in other CDK projects written in Typescript and redistributed via npm, or it could be transpiled into other JSII-supported languages such as Python or Java and published at an appropriate module location, such as Maven Central etc.

However, how can the implementation of the Lambda be redistributed via that CDK module (i.e. the code that will be executed when the Lambda fires)?

The options I can think of are:

  1. Write the Lambda in a language such as JS or Python and define the Lambda inline within the CDK construct. Limited to small functions and rules out Lambdas written in other languages, such as Golang, Rust or Java.
  2. The publisher of the construct saves the Lambda implementation (as a zip) somewhere in S3 and have the construct point at that location. This would mean that the person publishing the module would be responsible for hosting the implementation zip and assumes that the person consuming the construct trusts what has been written in the implementation.
  3. What other options are available, if any?
1
You can probably see how it is done internally by cdk. CustomResource SdkCall deploys it’s own lambda under the hood. Also there’s tooling for log group lifecycle management that deploys a lambda under the hood. - moltar

1 Answers

1
votes

Couldn't the implementation be distributed via npm alongside the CDK module? Provided the CDK construct can resolve the asset relative to itself (e.g. via __dirname) then it should just work, shouldn't it?

There's an example here where the author creates a redistributable lambda layer using the same approach.

export class NodeJwtLayer extends LayerVersion {
  constructor(scope: Construct, id: string = "NodeJwtLayer") {
    super(scope, id, {
      code: new AssetCode(__dirname + "/nodejs.zip")