|
{ |
|
"Comment": "A simple AWS Step Functions state machine that automates a daycare registration workflow that uses JSONata to replace lambda validation functions", |
|
"StartAt": "Check Information", |
|
"States": { |
|
"Check Information": { |
|
"Type": "Parallel", |
|
"Branches": [ |
|
{ |
|
"StartAt": "Check Identity", |
|
"States": { |
|
"Check Identity": { |
|
"Type": "Pass", |
|
"End": true, |
|
"Output": { |
|
"isInformationValid": "{% ($count($states.input.registration_info.daysOfWeek) > 0 and $not($states.input.registration_info.daysOfWeek = null)) and $not($states.input.registration_info.child = null) and $not($states.input.registration_info.parents = null) %}" |
|
} |
|
} |
|
} |
|
}, |
|
{ |
|
"StartAt": "Check Age Range", |
|
"States": { |
|
"Check Age Range": { |
|
"Type": "Pass", |
|
"End": true, |
|
"Output": { |
|
"Age": "{% $number($now('[Y0001]')) - $number($fromMillis($toMillis($states.input.registration_info.child.dateOfBirth), '[Y0001]')) %}" |
|
} |
|
} |
|
} |
|
} |
|
], |
|
"Assign": { |
|
"inputPayload": "{% $states.context.Execution.Input %}", |
|
"Age": "{% $states.result.Age %}", |
|
"isPayloadValid": "{% $states.result.isInformationValid %}", |
|
"isValidAge": "{% $number($states.result.Age) >= 2 and $number($states.result.Age) < 5 %}" |
|
}, |
|
"Next": "Information Check Result" |
|
}, |
|
"Information Check Result": { |
|
"Type": "Choice", |
|
"Choices": [ |
|
{ |
|
"Condition": "{% $isPayloadValid and $isValidAge %}", |
|
"Next": "Check Spots Availability" |
|
}, |
|
{ |
|
"Condition": "{% $isValidAge = false %}", |
|
"Next": "Age Invalid" |
|
} |
|
], |
|
"Default": "Notify Missing Info" |
|
}, |
|
"Age Invalid": { |
|
"Type": "Fail", |
|
"Error": "InvalidAge", |
|
"Cause": "The age must be greater than 2 but less than 5." |
|
}, |
|
"Notify Missing Info": { |
|
"Type": "Fail", |
|
"Error": "InformationIncomplete", |
|
"Cause": "The parent did not provide complete information." |
|
}, |
|
"Check Spots Availability": { |
|
"Type": "Task", |
|
"Resource": "arn:aws:states:::lambda:invoke", |
|
"Output": "{% $states.result.Payload %}", |
|
"Arguments": { |
|
"FunctionName": "arn:aws:lambda:eu-west-2:XXXX:function:CheckSpotAvailable:$LATEST", |
|
"Payload": "{% $inputPayload %}" |
|
}, |
|
"Assign": { |
|
"checkStatusCode": "{% $states.result.Payload.statusCode %}" |
|
}, |
|
"Next": "Spot Availability Result" |
|
}, |
|
"Spot Availability Result": { |
|
"Type": "Choice", |
|
"Choices": [ |
|
{ |
|
"Condition": "{% $checkStatusCode = 200 %}", |
|
"Next": "Add Registration" |
|
}, |
|
{ |
|
"Condition": "{% $checkStatusCode = 400 %}", |
|
"Next": "Notify No Spots" |
|
} |
|
] |
|
}, |
|
"Notify No Spots": { |
|
"Type": "Fail", |
|
"Error": "NoSpots", |
|
"Cause": "There are no spots available." |
|
}, |
|
"Add Registration": { |
|
"Type": "Task", |
|
"Resource": "arn:aws:states:::dynamodb:putItem", |
|
"Arguments": { |
|
"TableName": "arn:aws:dynamodb:eu-west-2:XXXX:table/TestRegistration", |
|
"Item": { |
|
"PK": { |
|
"S": "{% $uuid() %}" |
|
}, |
|
"SK": { |
|
"S": "name" |
|
}, |
|
"email": { |
|
"S": "{% $inputPayload.registration_info.parents.mother.email %}" |
|
}, |
|
"name": { |
|
"S": "{% $inputPayload.registration_info.child.firstName & ' ' & $inputPayload.registration_info.child.lastName %}" |
|
}, |
|
"dateOfBirth": { |
|
"S": "{% $inputPayload.registration_info.child.dateOfBirth %}" |
|
}, |
|
"age": { |
|
"N": "{% $string($Age) %}" |
|
}, |
|
"specialInstructions": { |
|
"S": "{% $inputPayload.registration_info.specialInstructions %}" |
|
}, |
|
"timestamp": { |
|
"S": "{% $now() %}" |
|
} |
|
} |
|
}, |
|
"Next": "Confirm Registration" |
|
}, |
|
"Confirm Registration": { |
|
"Type": "Succeed", |
|
"Comment": "Registration succeeded!" |
|
} |
|
}, |
|
"QueryLanguage": "JSONata" |
|
} |