Step Functions集成DynamoDB

创建DynamoDB

先创建一张Dynamodb Table,表名:TransactionHistoryTable,只有一个Partition Key:TransactionId

image-20220408192703948


创建Step Functions

进入Step Functions服务,创建新的State machine

image-20220408170228823

workflow的JSON更新成如下:

{
  "Comment": "Transaction Processor State Machine",
  "StartAt": "ProcessTransaction",
  "States": {
    "ProcessTransaction": {
      "Type": "Pass",
      "Next": "StoreHistory"
    },
    "StoreHistory": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:putItem",
      "Parameters": {
        "TableName": "TransactionHistoryTable",
        "Item": {
          "TransactionId": {
            "S.$": "$.TransactionId"
          }
        }
      },
      "Retry": [
        {
          "ErrorEquals": ["States.ALL"],
          "IntervalSeconds": 1,
          "MaxAttempts": 3
        }
      ],
      "End": true,
      "ResultPath": "$.DynamoDB"
    }
  }
}


渲染效果如下:

image-20220408193129727

进入下一步,将state machine命名为sns-test,其他选项保持默认:

image-20220408193149083

下拉到底部,在role里自动会添加Step Functions访问DynamoDB的权限:

image-20220408193204211

点击创建。

测试

创建完成后,新建一个Execution:

image-20220408193416514

测试的Input如下:

{
    "TransactionId": "abcd123ds-aws-niubi"
}

image-20220408193443361

执行完成后,查看每个阶段的输入和输出。在StoreHistory阶段,成功将消息发送到DynamoDB,DynamoDB并将结果返回:

image-20220408193518401

执行完成后,到DynamDB 表里,发现成功插入一条记录。

image-20220408193808335

image-20220408193828486