amazon web services - AWS CDK: Properly Parsing JSON-formatted SSM String Parameter for Lambda Configuration - Stack Overflow

Question:I'm using AWS CDK (v2) with TypeScript to configure Lambda functions using values stored

Question:

I'm using AWS CDK (v2) with TypeScript to configure Lambda functions using values stored in SSM String parameters with JSON format. While the parameter stores valid JSON, I'm having trouble properly parsing the values during synthesis.

Current Setup Parameter Format: Stored as a String parameter with JSON:

{"small":256,"medium":512,"large":768,"extraLarge":1024}

Parameter Creation:

new ssm.StringParameter(this, 'LambdaMemory', {
  parameterName: `${stage}-LambdaMemory`,
  stringValue: JSON.stringify({
    small: 256,
    medium: 512,
    large: 768,
    extraLarge: 1024
  }),
  type: ssm.ParameterType.STRING
});

What I've Tried Approach 1: Direct JSON parsing

const config = JSON.parse(
  ssm.StringParameter.valueForStringParameter(this, `${stage}-LambdaMemory`)
);

Issue: Fails during synthesis with token resolution errors

Approach 2: Token-aware parsing

const paramValue = ssm.StringParameter.valueForStringParameter(this, `${stage}-LambdaMemory`);
const config = {
  small: cdk.Token.asNumber(cdk.Fn.select(1, cdk.Fn.split('"small":', cdk.Fn.select(0, cdk.Fn.split(',"medium":', paramValue))))),
  medium: cdk.Token.asNumber(cdk.Fn.select(1, cdk.Fn.split('"medium":', cdk.Fn.select(0, cdk.Fn.split(',"large":', paramValue))))),
  // ...similar for other fields
};

Issue: Results in invalid values (e.g., scientific notation like -1.888e+289)

How can I properly parse JSON-formatted SSM parameters during CDK synthesis?

I'd like to have something like this:

const performWorkLambda = new NodejsFunction(this, 'PerformWorkLambda', {
  entry: join(__dirname, '../../src/lambdas/performWork/index.ts'),
  handler: 'handler',
  functionName: `${id}-PerformWorkLambda`,
  environment: {
    ...environment,
    PERSIST_QUEUE_URL: AUDIT_QUEUE_URL,
    UPDATE_QUEUE_URL,
    AUDIT_TABLE_NAME
  },
  role: lambdaExecutionRole,
  memorySize: lambdaMemoryConfig.large,
  timeout: cdk.Duration.seconds(
    lambdaTimeoutConfig.extraLarge
  )
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744242198a4564756.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信