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

在AWS Step Functions/CDK中组合JsonPath listAt与stringAt生成单个数组

在AWS CDK中为ECS Run Task步骤拼接命令数组

需求场景

使用AWS Step Functions编排流程,其中包含调用ECS Fargate任务的步骤。需要将Step输入中的reference字段作为command数组的首个参数,再拼接indexes数组的所有元素。

当前Step输入示例:

{
  "indexes": [
    "s3://bucket/test1.bam",
    "s3://bucket/test2.bam"
  ],
  "reference": "hg38"
}

现有仅传递indexes的代码:

new EcsRunTask(this, "Job", {
  integrationPattern: IntegrationPattern.RUN_JOB,
  cluster: fargateCluster,
  taskDefinition: taskDefinition,
  containerOverrides: [
    {
      command: JsonPath.listAt("$.indexes"),
      // 其他配置...
    }
  ]
});

错误尝试及问题

  1. 直接数组展开写法
command: [
  JsonPath.stringAt("$.reference"),
  ...JsonPath.listAt("$.indexes"),
]

报错:不能在数组中使用JsonPath字段,必须在对象中使用

  1. 结合JsonPath.array和展开写法
command: JsonPath.array(
  JsonPath.stringAt("$.reference"),
  ...JsonPath.listAt("$.indexes")
) as any

报错:解析错误:解析错误:解析错误:在标量字符串上下文中找到编码的列表令牌字符串。使用'Fn.select(0, list)'(而非'list[0]')从令牌列表中提取元素。

正确解决方案

使用JsonPath.concat方法将单个元素数组与目标数组合并,符合CDK的JsonPath语法规则:

new EcsRunTask(this, "Job", {
  integrationPattern: IntegrationPattern.RUN_JOB,
  cluster: fargateCluster,
  taskDefinition: taskDefinition,
  containerOverrides: [
    {
      command: JsonPath.concat(
        JsonPath.array(JsonPath.stringAt("$.reference")),
        JsonPath.listAt("$.indexes")
      ),
      // 其他配置...
    }
  ]
});

原理说明

CDK的JsonPath系统不支持直接在数组字面量中混合使用JsonPath表达式或展开操作,必须通过其提供的concat方法完成数组合并。该方法会生成Step Functions能正确解析的状态机定义,将reference作为首个元素,后续拼接indexes的所有元素。

内容的提问来源于stack exchange,提问作者Andrew Patterson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 14:32:42