To Nha Notes | Oct. 20, 2023, 8:42 a.m.
Here’s an example of a Step Function definition that invokes a Lambda function with parameters:
{
"Comment": "Invoke Lambda function with parameters",
"StartAt": "InvokeLambda",
"States": {
"InvokeLambda": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "MyLambdaFunction",
"Payload": {
"key1.$": "$.inputKey1",
"key2.$": "$.inputKey2"
}
},
"End": true
}
}
}
In this example, the InvokeLambda state is a Task that represents a unit of work performed by a state machine. The Resource field contains the ARN of the Lambda function to invoke when the state is entered. The Parameters field is used to provide input to the Lambda function. The FunctionName parameter specifies the name of the Lambda function to invoke, and the Payload parameter specifies the input payload for the Lambda function.
The "key1.$": "$.inputKey1" and "key2.$": "$.inputKey2" syntax means that the values of inputKey1 and inputKey2 from the original input to the state machine will be passed to the Lambda function as key1 and key2.
Here’s an example of a simple AWS Lambda function in Python that could be invoked by this state machine:
def lambda_handler(event, context):
# Extract parameters from event
key1 = event.get('key1')
key2 = event.get('key2')
# Perform some operation
result = key1 + key2
# Return result
return {
'statusCode': 200,
'body': result
}
In this example, the lambda_handler function is invoked when the Lambda function is triggered. It extracts key1 and key2 from the event object, performs some operation (in this case, addition), and returns the result.
Please replace "MyLambdaFunction" with your actual AWS Lambda function name. Also, make sure that your AWS Step Functions have necessary permissions to invoke your Lambda function. You can do this by attaching an IAM role with appropriate permissions to your state machine.