Step Functions集成SQS

Step Functions可以直接集成SQS, 本节实验我们将把Step Functions里的数据传输到SQS。

创建SQS

进入SQS服务,创建队列,命名为purchase_queue:

image-20220408170058554

创建完成后,拿到它的URL,用于下一步使用:

image-20220408170146919

i.e.  https://sqs.us-east-1.amazonaws.com/145197526627/purchase_queue

创建Step Functions

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

image-20220408170228823

workflow的JSON更新成如下,注意把QueueUrl替换为上一步获取到的URL:

{
    "Comment": "Transaction Process State Machine using SQS",
    "StartAt": "ProcessTransaction",
    "States": {
      "ProcessTransaction": {
        "Type": "Pass",
        "Next": "BroadcastToSqs"
      },
      "BroadcastToSqs": {
        "Type": "Task",
        "Resource": "arn:aws:states:::sqs:sendMessage",
        "Parameters": {
          "QueueUrl": "https://sqs.us-east-1.amazonaws.com/145197526627/purchase_queue",
          "MessageBody": {
            "TransactionId.$": "$.TransactionId",
            "Type.$": "$.Type"
          }
        },
        "End": true
      }
    }
  }
  

渲染效果如下:

image-20220408170333956

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

image-20220408170354901

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

image-20220408170422295

点击创建。

测试

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

image-20220408170438526

测试的Input如下:

{
 "TransactionId":"abc",
 "Type": "Purchase"
}

image-20220408170452452

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

image-20220408170518865

在SQS中看到有新的消息进来:

image-20220408170646626

查看消息体:

image-20210322161005998