Skip to content

(aws-lambda): ephemeral storage validation error reports [object Object] instead of the received size #38621

Description

@badmintoncryer

Describe the bug

When ephemeralStorageSize on lambda.Function is outside the allowed range, the validation error interpolates the Size object directly into the message. Size has no toString(), so the value is rendered as [object Object] and there is no way to tell from the error which value was actually passed.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

The error states the size that was received, e.g.:

Ephemeral storage size must be between 512 and 10240 MB, received 100 MiB.

Current Behavior

«EphemeralStorageOutOfRange» Ephemeral storage size must be between 512 and 10240 MB, received [object Object].

Reproduction Steps

new lambda.Function(this, 'Fn', {
  runtime: lambda.Runtime.NODEJS_20_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline('exports.handler = async () => {};'),
  ephemeralStorageSize: cdk.Size.mebibytes(100), // below the 512 MiB minimum
});

Run cdk synth.

The underlying behaviour can also be seen without synthesizing:

$ node -e "const cdk=require('aws-cdk-lib/core'); console.log(String(cdk.Size.mebibytes(100)))"
[object Object]

Possible Solution

packages/aws-cdk-lib/aws-lambda/lib/function.ts builds the message with the Size object itself:

if (props.ephemeralStorageSize && !props.ephemeralStorageSize.isUnresolved()
  && (props.ephemeralStorageSize.toMebibytes() < 512 || props.ephemeralStorageSize.toMebibytes() > 10240)) {
  throw new ValidationError(lit`EphemeralStorageOutOfRange`, `Ephemeral storage size must be between 512 and 10240 MB, received ${props.ephemeralStorageSize}.`, this);
}

The range check on the line above already converts the same value with .toMebibytes(); only the message is missing the conversion. Calling .toMebibytes() in the message as well fixes it, and introduces no new failure path (a Size that cannot be converted into whole mebibytes already throws in the condition, and the value is guaranteed to be resolved by the !isUnresolved() guard).

Additional Information/Context

The root cause is that Size in core has no toString(), unlike Duration, which has both toString() and toHumanString(). Any Size interpolated into a string therefore renders as [object Object]. aws-lambda/lib/function.ts appears to be the only place in aws-cdk-lib today where that actually happens, so this issue is scoped to the Lambda message; whether Size should gain a toString() of its own is a separate discussion.

AWS CDK Library version (aws-cdk-lib)

2.261.0 (also reproduces on main)

AWS CDK CLI version

2.1131.0

Node.js Version

v20.19.2

OS

macOS 14.6.1

Language

TypeScript

Language Version

TypeScript (7.0.2)

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-lambdaRelated to AWS Lambdaneeds-triageThis issue or PR still needs to be triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions