What is the basic difference between AWS step functions vs lambda?
I am currently engaged in a particular task that is related to designing a workflow for an e-commerce platform where orders need to be processed. The process involves validating the order, checking inventions, sending notifications, and updating the database. How can u decide whether to use AWS step functions or AWS lambda for orchestrating and executing these tasks?
In the context of AWS, here are the differences given between both:-
AWS Step functions
You can use the step functions if the workflow involves multiple state transitions, conditional logic, error handling, and coordination between different AWS services or custom codes.
You can define the workflow as a state machine by using an AWS step functions JSON-based state language.
Here is the example given of a step functions state machine definition in JSON:-
{
“Comment”: “Order Processing Workflow”,
“StartAt”: “ValidateOrder”,
“States”: {
“ValidateOrder”: {
“Type”: “Task”,
“Resource”: “arn:aws:lambda:us-east-1:123456789012:function:ValidateOrderFunction”,
“Next”: “CheckInventory”,
“Retry”: [
{
“ErrorEquals”: [“ValidationError”],
“IntervalSeconds”: 5,
“MaxAttempts”: 3,
“BackoffRate”: 1.5
}
],
“Catch”: [
{
“ErrorEquals”: [“InvalidInputError”],
“Next”: “HandleInvalidInput”
}
]
},
“CheckInventory”: {
“Type”: “Task”,
“Resource”: “arn:aws:lambda:us-east-1:123456789012:function:CheckInventoryFunction”,
“Next”: “SendNotifications”
},
“SendNotifications”: {
“Type”: “Task”,
“Resource”: “arn:aws:lambda:us-east-1:123456789012:function:SendNotificationsFunction”,
“Next”: “UpdateDatabase”
},
“UpdateDatabase”: {
“Type”: “Task”,
“Resource”: “arn:aws:lambda:us-east-1:123456789012:function:UpdateDatabaseFunction”,
“End”: true
},
“HandleInvalidInput”: {
“Type”: “Fail”,
“Cause”: “Invalid input provided”,
“Error”: “InvalidInputError”
}
}
}
AWS lambda
You can use the lambda function for individual, stateless tasks that can run independently and don’t require complex coordination.
Here is the example given of a Lambda function for processing orders:-
Import json
Def lambda_handler(event, context):
Order = event[‘order’]
# Validate the order
Validated_order = validate_order(order)
# Check inventory
Inventory_check_result = check_inventory(validated_order)
# Send notifications
Send_notifications(inventory_check_result)
# Update database
Update_database(inventory_check_result)
Return {
‘statusCode’: 200,
‘body’: json.dumps(‘Order processed successfully’)
}
Def validate_order(order):
# Perform validation logic
If not order:
Raise Exception(‘ValidationError’, ‘Order is empty’)
Return order
Def check_inventory(order):
# Perform inventory check logic
# Simulated result
Return {‘item’: ‘Product A’, ‘available’: True, ‘quantity’: 10}
Def send_notifications(inventory_result):
# Perform notification sending logic
Notification_message = f”Product {inventory_result[‘item’]} is available!”
Print(notification_message)
Def update_database(inventory_result):
# Perform database update logic
# Simulated update
Print(f”Updating database with inventory result: {inventory_result}”)