Use step function to delay sns
{
"StartAt": "Delay",
"Comment": "Publish to SNS with delay",
"States": {
"Delay": {
"Type": "Wait",
"SecondsPath": "$.delay_seconds",
"Next": "Publish to SNS"
},
"Publish to SNS": {
"Type": "Task",
"Resource": "arn:aws:states:::sns:publish",
"Parameters": {
"TopicArn": "${SNSTopic}",
"Subject.$": "$.subject",
"Message.$": "$.message"
},
"End": true
}
}
}
Step function input json parameter
The step function role must be updated with the following permission: (don’t forget Version string must have single/double quotes)
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sns:Publish"
],
"Resource": [
"topicArn"
],
"Effect": "Allow"
},
{
"Action": [
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey"
],
"Resource": [
"kms key arn/alias arn"
],
"Effect": "Allow"
}
}
The role also must be added to the kms key policy.
Using CLI to invoke step function:
AWS stepfunctions start-execution --state-machine-arn $ARN --input '{\"delay_seconds\": 3,\"message_body\":{\"key\":\"Hello\"}}'
$ARN does not need quotes
Can use JQ to help with encoding: (bash only)
AWS stepfunctions start-execution --state-machine-arn $ARN --input "$(echo '{"my_key": "my_value"}' | jq -R . )"
Input from file:
aws stepfunctions start-execution --state-machine-arn $ARN --input "$(jq -R . input.json --raw-output)"
Using a profile do:
AWS –profile
Invoke step function from a lambda:
const AWS = require("aws-sdk");
AWS.config.update({ region: "eu-west-1" });
const stepFunction = new AWS.StepFunctions();
interface Params {
stateMachineArn: string;
input: string;
name: string;
}
exports.handler = async (event: any) => {
console.log(event);
const stepFunctionParams = {
stateMachineArn: process.env.STEP_FUNCTION_ARN,
input: JSON.stringify({
message: event.body,
}),
name: "name" + String(Date.now()),
};
try {
const stepFunctionResponse = await stepFunction
.startExecution(stepFunctionParams)
.promise();
return { statusCode: 200, body: "Success" };
} catch (e) {
console.log("Problem executing SF :", JSON.stringify(e));
return {
statusCode: 500,
body: "Problem executing step function : " + JSON.stringify(e),
headers: { "Access-Control-Allow-Origin": "*" },
};
}
};
Input/Output Path and catching error
ResultPath: null to preserve context