You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术咨询:AWS CDK是否支持为AWS Step Functions指定ResultSelector?如何在将Payload中的Lambda输出传递给ResultPath前进行截断?

AWS Step Functions with AWS CDK: ResultSelector Support & Payload Truncation

1. Does AWS CDK support specifying ResultSelector for AWS Step Functions?

Absolutely! AWS CDK fully supports defining ResultSelector for Step Functions tasks. Every task construct (like LambdaInvoke, S3PutObject, etc.) in the CDK includes a resultSelector property that lets you define key-value pairs to transform a task's output before it gets passed to ResultPath.

Here’s a quick TypeScript example using a LambdaInvoke task:

import * as cdk from 'aws-cdk-lib';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import * as lambda from 'aws-cdk-lib/aws-lambda';

const app = new cdk.App();
const stack = new cdk.Stack(app, 'StepFunctionsResultSelectorStack');

const myLambda = new lambda.Function(stack, 'MyLambda', {
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: 'index.handler',
  code: lambda.Code.fromInline(`
    exports.handler = async () => {
      return { message: "Hello from Lambda", data: "This is a very long string that we might want to truncate later" };
    };
  `),
});

const lambdaInvoke = new tasks.LambdaInvoke(stack, 'InvokeLambda', {
  lambdaFunction: myLambda,
  // Define ResultSelector to shape the output
  resultSelector: {
    'lambdaMessage.$': '$.Payload.message',
    'originalData.$': '$.Payload.data'
  },
  // Control where the transformed result is stored
  resultPath: '$.lambdaResult'
});

const stateMachine = new sfn.StateMachine(stack, 'MyStateMachine', {
  definitionBody: sfn.DefinitionBody.fromChainable(lambdaInvoke),
});

2. How to truncate Lambda output before passing it via ResultPath?

Great question! You can use ResultSelector together with Step Functions’ built-in intrinsic functions to truncate your Lambda output. The $util.string.substring() function is ideal for cutting down string values—just specify the start index and your desired end index (use a large end index if you want to cap the string at a fixed length).

Let’s adjust the previous example to truncate the data field from the Lambda output:

const lambdaInvokeWithTruncation = new tasks.LambdaInvoke(stack, 'InvokeLambdaWithTruncation', {
  lambdaFunction: myLambda,
  resultSelector: {
    'lambdaMessage.$': '$.Payload.message',
    // Truncate the data string to its first 20 characters
    'truncatedData.$': "$util.string.substring($.Payload.data, 0, 20)"
  },
  // Store the truncated result in the state input
  resultPath: '$.processedLambdaOutput'
});

For more complex truncation needs—like shortening arrays—you can use other intrinsic functions like $util.list.slice(). Remember: ResultSelector transforms the task output first, then ResultPath places that modified output into the state machine’s overall data flow. This order ensures your truncation happens before the result is merged into the workflow.

内容的提问来源于stack exchange,提问作者Yunhan Zou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 22:13:12