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.

(aws-lambda-nodejs): export esbuild asset code bundler

See original GitHub issue

Can’t use aws-lambda-nodejs’ capabilities in Lambda@Edge.

Use Case

If you want to use a NodeJS lambda in edge, you need to bundle the code by yourself. It’d be ideal to export the esbuild bundling functionality so anybody can create the AssetCode with esbuild and use it on any other lambda functions such as Lambda@Edge.

Proposed Solution

Just exporting the class Bundling in @aws-cdk/aws-lambda-nodejs would be enough. As a workaround, I did this, which is heavily based on the current implementation but uses esbuild’s JS API:

import * as lambda from "@aws-cdk/aws-lambda";
import * as cdk from "@aws-cdk/core";
import * as esbuild from "esbuild";
import findUp from "find-up";
import * as path from "path";

const findDepsLockFilePath = (input: string) => {
  const lockFilePath = findUp.sync(["package-lock.json", "yarn.lock"], {
    cwd: input,
  });

  if (!lockFilePath) {
    throw new Error("Couldn't find a lock file (package-lock.json or yarn.lock)");
  }

  return lockFilePath;
};

/**
 * Converts a Lambda runtime to an esbuild node target.
 */
function toTarget(runtime: lambda.Runtime): string {
  const match = runtime.name.match(/nodejs(\d+)/);

  if (!match) throw new Error("Cannot extract version from runtime.");

  return `node${match[1]}`;
}

export interface EsbuildBundlingProps {
  input: string;
  runtime: lambda.Runtime;
  external?: string[];
  define?: {
    [key: string]: string;
  };
}

export class EsbuildBundling {
  public static bundle(options: EsbuildBundlingProps): lambda.AssetCode {
    const depsLockFilePath = findDepsLockFilePath(options.input);

    return lambda.Code.fromAsset(path.dirname(depsLockFilePath), {
      assetHashType: cdk.AssetHashType.OUTPUT,
      bundling: new EsbuildBundling(options),
    });
  }

  public readonly image: cdk.DockerImage;

  public readonly local: cdk.ILocalBundling;

  constructor(private readonly props: EsbuildBundlingProps) {
    this.image = cdk.DockerImage.fromRegistry("dummy");
    this.local = {
      tryBundle(outputDir) {
        console.log({ outputDir, input: props.input });
        esbuild.buildSync({
          entryPoints: [props.input],
          bundle: true,
          platform: "node",
          target: toTarget(props.runtime),
          outfile: `${outputDir}/index.js`,
          external: ["aws-sdk", ...(props.external || [])],
          define: props.define,
          // minify: true,
        });
        return true;
      },
    };
  }
}

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:open
  • Created 2 years ago
  • Reactions:16
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
eladbcommented, May 2, 2021

I am unassigning and marking this issue as p2, which means that we are unable to work on this immediately.

We use +1s to help prioritize our work, and are happy to revaluate this issue based on community feedback. You can reach out to the cdk.dev community on Slack to solicit support for reprioritization.

3reactions
hugomalletcommented, Jun 2, 2022

I don’t really understand why it’s low priority. How do you guys do with v2 ? Any workaround ? In the meantime can you at least fix this please ? https://github.com/aws/aws-cdk/issues/15661#issuecomment-1020356712 Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

API - esbuild
Allow overwrite; Analyze; Asset names; Banner; Charset; Chunk names; Color ... If you really need to bundle code that does this, you will...
Read more >
BundlingOptions — AWS Cloud Development Kit 1.183. ...
Build arguments to pass when building the bundling image. The charset to use for esbuild's output. By default esbuild's output is ASCII-only.
Read more >
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 >
Bundling your Node.js app for AWS Lambda
Jump to the esbuild section if you are looking for details on how to configure your ... Function('hello-lambda', { code: new pulumi.asset.
Read more >
AWS CDK - One-Step S3 Websites with esbuild
I wrote previously about using aws-lambda-nodejs with esbuild to write ... heavily on the core and assets constructs, will bundle code, ...
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