Created
October 21, 2025 11:38
-
-
Save cheeyeo/c01f69c64488d9662af44f85512cb916 to your computer and use it in GitHub Desktop.
Example of a state machine in AWS using step functions
This file contains hidden or 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
| import datetime | |
| import json | |
| def lambda_handler(event, context): | |
| registration_info = event['registration_info'] | |
| print(registration_info) | |
| dob = registration_info['child']['dateOfBirth'] | |
| today = datetime.date.today() | |
| dob_date = datetime.datetime.strptime(dob, '%Y-%m-%d').date() | |
| age = today.year - dob_date.year - ((today.month, today.day) < (dob_date.month, dob_date.day)) | |
| if age < 2 or age > 5: | |
| return { | |
| 'statusCode': 400, | |
| 'body': 'Child is not within age range for this daycare' | |
| } | |
| # Modifies the input data | |
| registration_info['child']['age'] = age | |
| return { | |
| 'statusCode': 200, | |
| 'body': json.dumps({'registration_info': registration_info}) | |
| } |
This file contains hidden or 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
| import json | |
| import datetime | |
| def lambda_handler(event, context): | |
| registration_info = event['registration_info'] | |
| required_fields = ['child', 'parents', 'daysOfWeek'] | |
| for field in required_fields: | |
| if field not in registration_info: | |
| return { | |
| 'statusCode': 400, | |
| 'body': f"Missing required field: {field}" | |
| } | |
| return { | |
| "statusCode": 200, | |
| "body": json.dumps(registration_info) | |
| } |
This file contains hidden or 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
| 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) | |
| } |
This file contains hidden or 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
| { | |
| "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." | |
| } | |
| } |
This file contains hidden or 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
| { | |
| "Comment": "A simple AWS Step Functions state machine that automates a daycare registration workflow.", | |
| "StartAt": "Check Information", | |
| "States": { | |
| "Check Information": { | |
| "Type": "Task", | |
| "Resource": "arn:aws:states:::lambda:invoke", | |
| "Output": "{% $states.result.Payload %}", | |
| "Arguments": { | |
| "FunctionName": "arn:aws:lambda:eu-west-2:XXXXX:function:CheckInformation:$LATEST", | |
| "Payload": "{% $states.input %}" | |
| }, | |
| "Assign": { | |
| "statusCode": "{% $states.result.Payload.statusCode %}" | |
| }, | |
| "Retry": [ | |
| { | |
| "ErrorEquals": [ | |
| "Lambda.ServiceException", | |
| "Lambda.AWSLambdaException", | |
| "Lambda.SdkClientException", | |
| "Lambda.TooManyRequestsException" | |
| ], | |
| "IntervalSeconds": 1, | |
| "MaxAttempts": 3, | |
| "BackoffRate": 2, | |
| "JitterStrategy": "FULL" | |
| } | |
| ], | |
| "Next": "Information Check Result" | |
| }, | |
| "Information Check Result": { | |
| "Type": "Choice", | |
| "Choices": [ | |
| { | |
| "Condition": "{% $statusCode = 200 %}", | |
| "Next": "Check age range" | |
| }, | |
| { | |
| "Condition": "{% $statusCode = 400 %}", | |
| "Next": "Notify Missing Info" | |
| } | |
| ] | |
| }, | |
| "Notify Missing Info": { | |
| "Type": "Fail", | |
| "Error": "InformationIncomplete", | |
| "Cause": "The parent did not provide complete information." | |
| }, | |
| "Check age range": { | |
| "Type": "Task", | |
| "Resource": "arn:aws:states:::lambda:invoke", | |
| "Output": "{% $states.result.Payload %}", | |
| "Arguments": { | |
| "FunctionName": "arn:aws:lambda:eu-west-2:XXXXXXX:function:CheckAgeRange:$LATEST", | |
| "Payload": "{% $states.context.Execution.Input %}" | |
| }, | |
| "Assign": { | |
| "ageStatusCode": "{% $states.result.Payload.statusCode %}", | |
| "modifiedBody": "{% $states.result.Payload.body %}" | |
| }, | |
| "Retry": [ | |
| { | |
| "ErrorEquals": [ | |
| "Lambda.ServiceException", | |
| "Lambda.AWSLambdaException", | |
| "Lambda.SdkClientException", | |
| "Lambda.TooManyRequestsException" | |
| ], | |
| "IntervalSeconds": 1, | |
| "MaxAttempts": 3, | |
| "BackoffRate": 2, | |
| "JitterStrategy": "FULL" | |
| } | |
| ], | |
| "Next": "Age Check Result" | |
| }, | |
| "Age Check Result": { | |
| "Type": "Choice", | |
| "Choices": [ | |
| { | |
| "Condition": "{% $ageStatusCode = 200 %}", | |
| "Next": "Check Spots Availability" | |
| }, | |
| { | |
| "Condition": "{% $ageStatusCode = 400 %}", | |
| "Next": "Notify Invalid Age" | |
| } | |
| ] | |
| }, | |
| "Notify Invalid Age": { | |
| "Type": "Fail", | |
| "Error": "InvalidAge", | |
| "Cause": "The child does not meet the age requirements." | |
| }, | |
| "Check Spots Availability": { | |
| "Type": "Task", | |
| "Resource": "arn:aws:states:::lambda:invoke", | |
| "Output": "{% $states.result.Payload %}", | |
| "Arguments": { | |
| "FunctionName": "arn:aws:lambda:eu-west-2:xxxxxx:function:CheckSpotAvailable:$LATEST", | |
| "Payload": "{% $modifiedBody %}" | |
| }, | |
| "Assign": { | |
| "checkStatusCode": "{% $states.result.Payload.statusCode %}" | |
| }, | |
| "Next": "Spot Availability Result" | |
| }, | |
| "Spot Availability Result": { | |
| "Type": "Choice", | |
| "Choices": [ | |
| { | |
| "Condition": "{% $checkStatusCode = 200 %}", | |
| "Next": "Confirm Registration" | |
| }, | |
| { | |
| "Condition": "{% $checkStatusCode = 400 %}", | |
| "Next": "Notify No Spots" | |
| } | |
| ] | |
| }, | |
| "Notify No Spots": { | |
| "Type": "Fail", | |
| "Error": "NoSpots", | |
| "Cause": "There are no spots available." | |
| }, | |
| "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