aws-step-functions-s3-payload
v2.0.2
Published
Utility class to read and write payloads in AWS Step Functions
Downloads
6
Readme
AWS Step Function S3 Payload
Usage
ASL Definition:
{
"Comment": "Your Awesome State Machine",
"StartAt": "first-step",
"States": {
"first-step": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"FunctionName": "arn:aws:lambda:[AWS_REGION]:[AWS_ACCOUNT_ID]:function:[LAMBDA_FUNCTION_NAME]",
"Payload": {
"input.$": "$",
"context": {
"StateMachine.$": "$$.StateMachine.Name",
"Execution.$": "$$.Execution.Name",
"State.$": "$$.State.Name"
}
}
},
"End": true
}
}
}
Lambda Handler
Ensure that you set the environment variable S3_BUCKET
to a valid bucket name.
const S3PayloadUtils = require('@katunch/aws-step-functions-s3-payload');
module.exports.handler = async (event, context) => {
const payload = await S3PayloadUtils.getPayloadFromS3(event);
let result = {};
// do your work here...
const payloadUrl = await S3PayloadUtils.savePayloadToS3(event.context, result);
return {
payloadUrl: payloadUrl
}
}
Invocation
const { DateTime } = require('luxon');
const {SFNClient, InvokeStateMachineCommand} = require('@aws-sdk/client-sfn');
const stepFunctions = new SFNClient();
const STATEMACHINE_ARN='[YOUR STATE MACHINE ARN]';
const invokeStateMachine = async () => {
const startTime = DateTime.now();
const executionParams = {
stateMachineArn: STATEMACHINE_ARN,
input: JSON.stringify({
'payloadUrl': 's3://your-bucket/key/to/file.json'
}),
name: `${startTime.toFormat('yyyyMMdd-HHmmss')}-${startTime.toMillis()}-${invoiceIds.length}`
};
console.log(`Starting State Machine Execution: ${executionParams.name}`);
const invokeCommand = new InvokeStateMachineCommand(executionParams);
const sfResult = await stepFunctions.send(invokeCommand);
return sfResult;
}