Skip to content

Bug: getParameters reports incorrect stack trace when transforms fails #3950

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dreamorosi opened this issue May 20, 2025 · 3 comments
Open
Assignees
Labels
bug Something isn't working confirmed The scope is clear, ready for implementation good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one parameters This item relates to the Parameters Utility

Comments

@dreamorosi
Copy link
Contributor

dreamorosi commented May 20, 2025

Expected Behavior

When receiving a TransformParameterError I should be able to see the correct stack trace that points me to where the error was thrown.

Current Behavior

The error thrown when there's a transform error in the getMultiple code path is wrapping the original TransformParameterError error in another instance of itself, mangling the stack trace and making it harder to identify the issue.

{
  "errorType": "TransformParameterError",
  "errorMessage": "Unable to transform value using 'auto' transform: Unable to transform value using 'auto' transform: Unexpected token 'm', \"my-parameter-value\" is not valid JSON",
  "name": "TransformParameterError",
  "stack": [
    "TransformParameterError: Unable to transform value using 'auto' transform: Unable to transform value using 'auto' transform: Unexpected token 'm', \"my-parameter-value\" is not valid JSON",
    "    at n.getMultiple (file:///var/task/index.mjs:2:6279)",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
    "    at async file:///var/task/index.mjs:2:10917"
  ]
}

should be something like (notice the correct transformValue location):

{
  "errorType": "TransformParameterError",
  "errorMessage": "Unable to transform value using 'auto' transform: Unexpected token 'm', \"my-parameter-value\" is not valid JSON",
  "name": "TransformParameterError",
  "stack": [
    "TransformParameterError: Unable to transform value using 'auto' transform: Unexpected token 'm', \"my-parameter-value\" is not valid JSON",
    "    at transformValue (/node_modules/@aws-lambda-powertools/parameters/lib/esm/base/transformValue.js:58:19)",
    "    at n.getMultiple (/node_modules/@aws-lambda-powertools/parameters/lib/esm/base/BaseProvider.js:126:40)",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
    "    at async <anonymous> (/src/index.ts:8:20)"
  ]
}

Code snippet

import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';
import { getParameters } from '@aws-lambda-powertools/parameters/ssm';

const parameterPrefix = getStringFromEnv({
  key: 'SSM_PARAMETER_PREFIX',
});

const parameters = await getParameters(parameterPrefix, {
  transform: 'auto',
  throwOnTransformError: true,
});

export const handler = async () => {
  console.log('Parameters:', parameters);
  return {
    statusCode: 200,
    body: JSON.stringify(parameters),
  };
};

Steps to Reproduce

  1. Deploy a stack with two parameters
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';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Effect, PolicyStatement } from 'aws-cdk-lib/aws-iam';

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

    const fnName = 'ParamsFn';
    const logGroup = new LogGroup(this, 'MyLogGroup', {
      logGroupName: `/aws/lambda/${fnName}`,
      removalPolicy: RemovalPolicy.DESTROY,
      retention: RetentionDays.ONE_DAY,
    });

    const myParameter = new StringParameter(this, 'MySsmParameter', {
      parameterName: '/my/custom/parameter.json',
      stringValue: JSON.stringify({
        key1: 'value1',
      }),
      // You can specify other properties like description, tier, etc.
    });
    const myOtherParameter = new StringParameter(this, 'MyOtherSsmParameter', {
      parameterName: '/my/custom/parameter2.json',
      stringValue: 'my-parameter-value',
      // You can specify other properties like description, tier, etc.
    });

    const fn = new NodejsFunction(this, 'MyFunction', {
      functionName: fnName,
      logGroup,
      runtime: Runtime.NODEJS_20_X,
      entry: './src/index.ts',
      handler: 'handler',
      tracing: Tracing.ACTIVE,
      environment: {
        SSM_PARAMETER_PREFIX: '/my/custom/',
      },
      bundling: {
        minify: true,
        mainFields: ['module', 'main'],
        sourceMap: true,
        format: OutputFormat.ESM,
        banner:
          "import { createRequire } from 'module';const require = createRequire(import.meta.url);",
      },
    });
    fn.addToRolePolicy(
      new PolicyStatement({
        actions: ['ssm:GetParametersByPath'],
        resources: ['*'],
        effect: Effect.ALLOW,
      })
    );

    /* myParameter.grantRead(fn);
    myOtherParameter.grantRead(fn); */

    new CfnOutput(this, 'FunctionArn', {
      value: fn.functionArn,
    });
  }
}
  1. Add the function shown above
  2. Run it and observe the error formatting

Possible Solution

We should change this statement to just throw error.

Powertools for AWS Lambda (TypeScript) version

latest

AWS Lambda function runtime

22.x

Packaging format used

npm

Execution logs

@dreamorosi dreamorosi added bug Something isn't working good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one parameters This item relates to the Parameters Utility confirmed The scope is clear, ready for implementation labels May 20, 2025
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) May 20, 2025
@dreamorosi
Copy link
Contributor Author

Note

This issue is open for contributions, and ideal for first time contributors. If you're interested in working on this, please leave a comment below so that we can assign it to you. Feel free to ask any questions if needed.

@VatsalGoel3
Copy link
Contributor

@dreamorosi, I can pick this up

@dreamorosi
Copy link
Contributor Author

Nice, go for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working confirmed The scope is clear, ready for implementation good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one parameters This item relates to the Parameters Utility
Projects
Status: Working on it
Development

No branches or pull requests

2 participants