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
전문가
검토됨 한 달 전
  • That's exactly what I was looking for, now sure how I missed it. Thanks!

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠