I am trying to pass value dynamically to MessageAttribute parameter while doing an SNS publish from a Step function.
reference: https://docs.aws.amazon.com/step-functions/latest/dg/connect-sns.html
As per the example provided in the documentation if you want to publish to SNS from a Step function:
{
"StartAt": "Publish to SNS",
"States": {
"Publish to SNS": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:aws:sns:us-east-1:111222333444:myTopic",
"Message.$": "$.input.message",
"MessageAttributes": {
"my attribute no 1": {
"DataType": "String",
"StringValue": "value of my attribute no 1"
},
"my attribute no 2": {
"DataType": "String",
"StringValue": "value of my attribute no 2"
}
}
},
"End": true
}
}
}
Now suppose my input to the state machine is as follows:
"SNSDetails": {
"attribute1": "some value",
"attribute2": "some other value",
}
How can I dynamically access the $.SNSDetails.attribute1 in the "StringValue" of "my attribute no 1" and similarly access $.SNSDetails.attribute2 in the StringValue of "my attribute no 2" instead of hardcoding it?
Ultimately I want the state machine to translate the value of "my attribute no 1" as "some value" and value of "my attribute no 2" as "some other value"
Thanks in advance.
You can do so by appending .$ to the StringValue attribute name: this will instruct the engine to evaluate the attribute value as a JSON Path.
Given this input payload:
{
"input": {
"message": "Hello world"
},
"SNSDetails": {
"attribute1": "some value",
"attribute2": "some other value",
}
}
...and this StepFunction code:
{
"StartAt": "Publish to SNS",
"States": {
"Publish to SNS": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "arn:aws:sns:us-east-1:111222333444:myTopic",
"Message.$": "$.input.message",
"MessageAttributes": {
"my attribute no 1": {
"DataType": "String",
"StringValue.$": "$.SNSDetails.attribute1"
},
"my attribute no 2": {
"DataType": "String",
"StringValue.$": "$.SNSDetails.attribute2"
}
}
},
"End": true
}
}
}
...it will correctly be resolved.
You can easily check the result in the Execution event history (TaskScheduled details) on your AWS console (Step Functions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With