Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Last active October 25, 2025 20:12
Show Gist options
  • Select an option

  • Save cheeyeo/d1882e476f63c594d8046a98fd934852 to your computer and use it in GitHub Desktop.

Select an option

Save cheeyeo/d1882e476f63c594d8046a98fd934852 to your computer and use it in GitHub Desktop.
Updated Step Function definition that uses JSONata variables and functions to perform data validations rather than lambdas
import json
def lambda_handler(event, context):
print(event)
registration_info = event['registration_info']
# Placeholder for actual availability check logic
spots_available = 20 # This should be dynamically determined, not hardcoded
if spots_available <= 0:
return {
'statusCode': 400,
'body': json.dumps('No spots available in the daycare.')
}
# If spots are available, return the registration_info without changes
return {
'statusCode': 200,
'body': json.dumps(registration_info)
}
{
"registration_info": {
"child": {
"firstName": "Mohamed",
"lastName": "Diallo",
"dateOfBirth": "2022-07-01"
},
"parents": {
"mother": {
"firstName": "Aicha",
"lastName": "Cisse",
"email": "aicha.cisse@example.com",
"phone": "123-456-7890"
},
"father": {
"firstName": "Ibrahim",
"lastName": "Diallo",
"email": "ibrahim.diallo@example.com",
"phone": "098-765-4321"
}
},
"daysOfWeek": [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"
],
"specialInstructions": "Mohamed has a peanut allergy."
}
}
{
"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"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment