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.

[lambda-nodejs] Deploy additional files alongside bundled output

See original GitHub issue

I want to be able to push up some additional files along with the bundled output. One way of doing this would be to expose a hook that allows augmenting the bundling commands.

Use Case

I’m trying to deploy a lambda function that uses the Prisma database client. They have a guide using the serverless cli to deploy to lambda but I was hoping to use cdk since I have other infrastructure to manage as well.

The prisma database client is generated based on a schema.prisma file that itself is generated by introspecting the database. It actually automatically performs this generation on install, so my hope was that I could

  • Specify nodeModules: ["@prisma/client", "@prisma/cli"]
  • Bundle up the rest of the function and dependencies
  • When install occurs, the client get generated as normal

The problem is that the schema.prisma file isn’t copied to the output directory and I don’t think there’s a way for me to accomplish that. Ideally I would like to make use of the bundling functionality while still having some control over what else gets deployed.

Proposed Solution

Perhaps expose a hook to inject custom commands? This is the area of interest. I got something working hacking around node_modules where all I need to do is insert a cp schema.prisma ${options.outputDir} in between parcelCommand and depsCommand. So I was thinking a function like this:

const fn = new lambda.NodejsFunction(this, "MyCDKFunction", {
  nodeModules: ["@prisma/client", "@prisma/cli"],
  handler: "handler",
  entry: "lib/index.ts",
  modifyBundleCommand: ({ parcelCommand, depsCommand, options }) => {
    return [parcelCommand, `cp schema.prisma ${options.outputDir}`, depsCommand];
  }   
});

The default would just be ({ parcelCommand, depsCommand }) => [parcelCommand, depsCommand] to maintain the default behavior.

  • 👋 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 3 years ago
  • Reactions:5
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
jogoldcommented, Nov 15, 2020

We could have something like this:

Add a additionalCommands prop of type ICommand:

interface ICommand {
  beforeBundling(outputDir: string): string;
  afterBundling(outputDir: string): string;
}
new lambda.NodejsFunction(this, "MyFunction", {
  // ...
  additionalCommands: {
    beforeBundling(outputDir: string): string {
      // return a command to run before bundling
      // it can use the `outputDir` to copy to the right output folder
    },
    afterBundling(outputDir: string): string {
      // return a command to run after bundling
    },
  }
})
1reaction
jogoldcommented, Nov 16, 2020

Quick question: Isn’t the outputDir always equal to /asset-output

Not when doing local bundling https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda-nodejs#local-bundling

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deploy Node.js Lambda functions with .zip file archives
Updating a function with additional dependencies · Open a command line terminal or shell. Ensure that the Node. · Create a folder for...
Read more >
Deploy additional files with Node Lambda Function AWS CDK
If each function requires different files however, then the bundling option is just as easy.
Read more >
How to deploy a Node.js application to AWS Lambda - Dashbird
In this article, we'll show you how to deploy a Node.js application to AWS Lambda with the help of the Serverless Framework.
Read more >
Bundling a Node.js Function for AWS Lambda with Webpack
One of the major challenges when working with AWS Lambda is bundling all your node_modules into one zip file. Most simple examples rely...
Read more >
Package your NodeJS Lambda functions individually with ...
Increased complexity - bundling your code means more steps before running your code locally using aws sam local and more deployment time steps ......
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