Customise emitted SNS message in state machine

0

I have the following state machine step:

    "Step Name": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "${TopicARN}",
        "Message.$": "States.JsonToString($)",
        "MessageAttributes": {
          "type": {
            "DataType": "String",
            "StringValue": "$.type"
          },
          "id": {
            "DataType": "String",
            "StringValue": "$.id"
          }
        }
      },
      "End": true
    }

which works fine but I'd like to customise the body of the SNS message, somehow like this:

    "Step Name": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "${TopicARN}",
        "Message.$": {
          "id": "$.id",
          "param1": "$.param1",
          "param2": "$.param2",
          "metadata": "$"
        },
        "MessageAttributes": {
          "type": {
            "DataType": "String",
            "StringValue": "$.type"
          },
          "id": {
            "DataType": "String",
            "StringValue": "$.id"
          }
        }
      },
      "End": true
    }

However it does not work because Message has to be a String. I was hoping to wrap my object in States.JsonToString but that also doesn't seem to work.

Is there a way to achieve this or will I have to resort to using a lambda instead of the SNS integration?

1回答
1
承認された回答

To specify that a parameter use a path to reference a JSON node in the input, end the parameter name with .$ (from doc).

In your case:

    "Step Name": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "${TopicARN}",
        "Message": {
          "id.$": "$.id",
          "param1.$": "$.param1",
          "param2.$": "$.param2",
          "metadata.$": "$"
        },
        "MessageAttributes": {
          "type": {
            "DataType": "String",
            "StringValue.$": "$.type"
          },
          "id": {
            "DataType": "String",
            "StringValue.$": "$.id"
          }
        }
      },
      "End": true
    }
profile pictureAWS
エキスパート
Uri
回答済み 2年前
profile picture
エキスパート
レビュー済み 1ヶ月前
  • That's exactly what I was looking for, now sure how I missed it. Thanks!

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ