Step Functions集成SNS

创建SNS

进行SNS页面,点击Create topic:

image-20220408190348164

选择Standard类型,命名为topic-for-step-function并点击创建:

image-20220408190757559

topic创建完成后,添加订阅:

image-20220408190817168

类型选择Email,并添加个人邮箱作为Endpoint

image-20220408190847433

邮箱里会收到确认邮件,点击其中的链接进行确认。

回到topic页面,复制topic ARN用于下一步使用:

image-20220408191019088

创建Step Functions

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

image-20220408170228823

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

{
  "Comment": "Transaction Process State Mahcine using SNS",
  "StartAt": "ProcessTransaction",
  "States": {
    "ProcessTransaction": {
      "Type": "Pass",
      "Next": "BroadcastToSNS"
    },
    "BroadcastToSNS": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "arn:aws:sns:us-east-1:145197526627:topic-for-step-function",
        "Message": {
          "TransactionId.$": "$.TransactionId",
          "Type.$": "$.Type",
          "Source": "Step Function!"
        }
      },
      "End": true
    }
  }
}

渲染效果如下:

image-20220408191920332

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

image-20220408191948084

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

image-20220408192002875

点击创建。

测试

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

image-20220408192036287

测试的Input如下:

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

image-20220408170452452

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

image-20220408192138330

在邮箱中也接收到新的邮件:

image-20220408192441387