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?

已提問 2 年前檢視次數 1382 次
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!

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南