Last active
January 9, 2023 14:53
-
-
Save wparad/d8372ad9b2ace45d4755593363659276 to your computer and use it in GitHub Desktop.
Medium: AWS Step Function handler (https://dev.to/wparad/aws-step-functions-advanced-30k2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async onEvent(trigger) { | |
logger.log({ title: 'Starting new Triggered work', level: 'INFO', trigger }); | |
if (trigger.context && trigger.context.StateMachine) { | |
const processorId = | |
`${trigger.context.StateMachine.Name}|${trigger.context.State.Name}`; | |
const payload = trigger.context.Execution.Input; | |
const parameters = trigger.parameters; | |
const context = trigger.context.State; | |
const processors = { | |
'StepFunctionName|Start': () => handler.start(payload, parameters, context), | |
'StepFunctionName|Verify': () => handler.verify(payload, parameters, context), | |
'StepFunctionName|Cleanup': () => handler.cleanup(payload, parameters, context) | |
}; | |
if (!processors[processorId]) { | |
logger.log({ title: 'Step Function processor does not exist for type', | |
level: 'ERROR', trigger, payload, stepFunctionContext }); | |
return {}; | |
} | |
try { | |
const result = await processors[processorId](); | |
return result; | |
} catch (error) { | |
if (error.code !== 'ForceRetryExecution' | |
&& error.message !== 'ForceRetryExecution') { | |
logger.log({ title: 'Error handling StepFunction trigger', | |
level: 'CRITICAL', error, trigger, payload, stepFunctionContext }); | |
} | |
if (stepFunctionContext && stepFunctionContext.RetryCount > 10) { | |
logger.log({ title: 'Too many retries, log as ERROR.', | |
level: 'ERROR', error, trigger, payload, stepFunctionContext }); | |
} | |
throw error; | |
} | |
} | |
// Handle other types ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment