Skip to content

feature request: cdklocal to use esbuild for local builds #11351

@dreamorosi

Description

@dreamorosi

Is there an existing issue for this?

  • I have searched the existing issues

Feature description

The NodejsFunction construct from AWS CDK allows you to build and deploy TypeScript functions.

Example:

import { Stack, type StackProps, CfnOutput, RemovalPolicy } from 'aws-cdk-lib';
import type { Construct } from 'constructs';
import { Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
import { LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs';

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

    const fnName = 'LogeventFn';
    const logGroup = new LogGroup(this, 'MyLogGroup', {
      logGroupName: `/aws/lambda/${fnName}`,
      removalPolicy: RemovalPolicy.DESTROY,
      retention: RetentionDays.ONE_DAY,
    });
    const fn = new NodejsFunction(this, 'MyFunction', {
      functionName: fnName,
      logGroup,
      runtime: Runtime.NODEJS_20_X,
      entry: './src/index.ts',
      handler: 'handler',
      tracing: Tracing.ACTIVE,
      bundling: {
        minify: true,
        mainFields: ['module', 'main'],
        sourceMap: true,
        format: OutputFormat.ESM,
        banner:
          "import { createRequire } from 'module';const require = createRequire(import.meta.url);",
      },
    });

    new CfnOutput(this, 'FunctionArn', {
      value: fn.functionArn,
    });
  }
}

The construct relies on esbuild to transform and bundle your code so that it can be deployed on AWS Lambda. By default the construct will attempt to use Docker to bundle your code, however when esbuild is installed in the project it will perform these actions directly on the host - which is much faster and for most JavaScript cases just fine.

When working with LocalStack, even with esbuild installed in my project, LocalStack always uses Docker.

Ideally, I would like cdklocal to behave the same as when using cdk directly in this regard.

How to reproduce in CDK:

mkdir /tmp/test; cd /tmp/test
cdk init sample-app --language=typescript
npm i -D esbuild

then copy the stack above, and create a sample function.

finally, deploy with cdk deploy and observe that docker is never used.

🧑‍💻 Implementation

No response

Anything else?

See here for CDK docs that explain the behavior I described: https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk-lib/aws-lambda-nodejs#local-bundling

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: integration/cdkIssues related to AWS Cloud Development Kitstatus: backlogTriaged but not yet being worked ontype: featureNew feature, or improvement to an existing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions