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?

gefragt vor 2 Jahren1379 Aufrufe
1 Antwort
1
Akzeptierte Antwort

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
EXPERTE
Uri
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor einem Monat
  • That's exactly what I was looking for, now sure how I missed it. Thanks!

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen