Step Functions中DynamoUpdateItem的JSONPath参数类型验证报错排查
Step Functions DynamoUpdateItem 架构验证错误解决办法
我在用AWS CDK编写Step Functions的基础设施代码时,遇到了DynamoUpdateItem任务的架构验证错误。代码意图是用Step Functions执行上下文里的$$.Execution.StartTime更新DynamoDB表的LastRun字段,但一直报错。
原代码如下:
const updateDDB = new tasks.DynamoUpdateItem(this, 'Update item into DynamoDB', { key: { ['Task'] : tasks.DynamoAttributeValue.fromString('Processing') }, table: table, updateExpression: 'SET LastRun = :label', expressionAttributeValues: { ':label.$': DynamoAttributeValue.fromString(JsonPath.stringAt('$$.Execution.StartTime')), }, resultPath: JsonPath.DISCARD, });
收到的错误提示:
"The value for the field ':label.$' must be a STRING that contains a JSONPath but was an OBJECT at /States/Update item into DynamoDB/Parameters"
我试过把键写成[':label.$'],给JsonPath.stringAt()结果加.toString(),甚至用JSON.stringify()处理,但都没用,还是弹出相同的非字符串类型错误。
正确解决方式
问题出在expressionAttributeValues的特殊键写法:当键带有.$后缀时,对应的值必须是原始的JSONPath字符串,不能用DynamoAttributeValue.fromString()包装。Step Functions会自动把JSONPath对应的结果转换成DynamoDB需要的字符串类型。
修正后的代码:
const updateDDB = new tasks.DynamoUpdateItem(this, 'Update item into DynamoDB', { key: { ['Task'] : tasks.DynamoAttributeValue.fromString('Processing') }, table: table, updateExpression: 'SET LastRun = :label', expressionAttributeValues: { ':label.$': '$$.Execution.StartTime', // 直接使用原始JSONPath字符串 }, resultPath: JsonPath.DISCARD, });
原因说明
- 带
.$的键是Step Functions的特殊语法,用来标识这是一个JSONPath引用,此时值必须是纯字符串形式的路径,用DynamoAttributeValue包装后会被识别为对象,触发架构验证失败。 - 无需手动转换类型,Step Functions会自动将
$$.Execution.StartTime对应的字符串值映射为DynamoDB的字符串属性。
内容的提问来源于stack exchange,提问作者DevBob
相关产品推荐
相关产品推荐

