By using AWS re:Post, you agree to the Terms of Use
/Customise emitted SNS message in state machine/

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 Answers
1
Accepted Answer

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
    }
EXPERT
answered 8 days ago
  • That's exactly what I was looking for, now sure how I missed it. Thanks!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions