Skip to content

@aws-cdk/aws-lambda-nodejs: esbuildArgs alias must use : instead of = #25385

@abaschen

Description

@abaschen

Describe the bug

I want to reference libs in my layers with import { somefunc } from '@layer/somelib';.
When setting the alias build argument for esbuild, it throws an error :

const opts = {
            bundling: {
                externalModules: ['@aws-sdk/*', 'aws-lambda', '@layer/*'],
                esbuildArgs: {
                    '--alias': '@layer=',
                },
            },
        };

It will generate a bundling step like this:

esbuild --bundle src/index.js  --outfile=dist/index.js --alias=@layer=
✘ [ERROR] Invalid build flag: "--alias=@layer="

  Use "--alias:@layer=" instead of "--alias=@layer=". Flags that can be re-specified multiple times
  use ":" instead of "=".

By replacing the = with : it solves the issue

Expected Behavior

esbuildArgs key/value needs : when it can be re-specified.

Current Behavior

esbuildArgs['--alias'] is using = which leads to a bundle error.

Full stack:

    at AssetStaging.bundle (node_modules/aws-cdk-lib/core/lib/asset-staging.js:2:603)
    at AssetStaging.stageByBundling (node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:4540)
    at stageThisAsset (node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:1901)
    at Cache.obtain (node_modules/aws-cdk-lib/core/lib/private/cache.js:1:242)
    at new AssetStaging (node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:2296)
    at new Asset (node_modules/aws-cdk-lib/aws-s3-assets/lib/asset.js:1:736)
    at AssetCode.bind (node_modules/aws-cdk-lib/aws-lambda/lib/code.js:1:4628)
    at new Function (node_modules/aws-cdk-lib/aws-lambda/lib/function.js:1:2829)
    at new NodejsFunction (node_modules/aws-cdk-lib/aws-lambda-nodejs/lib/function.js:1:1171)
    at new MyConstruct (stack/MyConstruct .ts:18:9)

Reproduction Steps

CDK:

new NodejsFunction(this, 'test-function', {
            entry: 'src/functions/test/index.ts',
            architecture: Architecture.ARM_64,
            runtime: Runtime.NODEJS_18_X,
            memorySize: 128,
            tracing: Tracing.ACTIVE,
            handler: 'index.handler',
            timeout: Duration.seconds(30),
            retryAttempts: 0,
            logRetention: RetentionDays.ONE_DAY,
            bundling: {
                minify: process.env.NODE_ENV === 'production',
                banner: 'import { createRequire } from \'module\'; const require = createRequire(import.meta.url);',
                mainFields: ['module', 'main'],
                target: 'node18',
                externalModules: ['@aws-sdk/*', 'aws-lambda', '@layer/*'],
                esbuildArgs: {
                    '--alias': '@layer=',
                },
                format: OutputFormat.ESM,
            },
        });

Lambda

// src/functions/test/index.ts
import { ping } from '@layer/echo';

Layer

// dist/layers/echo/index.js
export{console.log as ping};

Possible Solution

esbuildArgs key/value needs : when it can be re-specified; from documentation:

CLI: If you are using the command-line API, it may be helpful to know that the flags come in one of three forms: --foo, --foo=bar, or --foo:bar. The form --foo is used for enabling boolean flags such as --minify, the form --foo=bar is used for flags that have a single value and are only specified once such as --platform=, and the form --foo:bar is used for flags that have multiple values and can be re-specified multiple times such as --external:.

Additional Information/Context

No response

CDK CLI Version

2.76.0 (build 78c411b)

Framework Version

No response

Node.js Version

Node.js v19.8.1

OS

WSL2 Ubuntu

Language

Typescript

Language Version

Typescript (5.0.4)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions