Invoke Lambda with Step Functions
Step Functions can control certain AWS services directly from the Amazon States Language. For more information about working with AWS Step Functions and its integrations, see the following:
For more information about managing state input, output, and results, see Input and Output Processing in Step Functions.
Supported AWS Lambda APIs:
-
-
Supported Parameters
The following includes a Task
state that invokes a Lambda function.
{
"StartAt":"CallLambda",
"States":{
"CallLambda":{
"Type":"Task",
"Resource":"arn:aws:states:::lambda:invoke",
"Parameters":{
"FunctionName":"MyFunction"
},
"End":true
}
}
}
The following includes a Task
state that implements the callback service integration pattern.
{
"StartAt":"GetManualReview",
"States":{
"GetManualReview":{
"Type":"Task",
"Resource":"arn:aws:states:::lambda:invoke.waitForTaskToken",
"Parameters":{
"FunctionName":"get-model-review-decision",
"Payload":{
"model.$":"$.new_model",
"token.$":"$$.Task.Token"
},
"Qualifier":"prod-v1"
},
"End":true
}
}
}
When you invoke a Lambda function, it will wait for a job to complete (.sync), even
if
you did not specify that. However, it is possible to call Lambda asynchronously using
the
InvocationType
parameter, as seen in the following example:
{
"Comment": "A Hello World example of the Amazon States Language using Pass states",
"StartAt": "Hello",
"States": {
"Hello": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "echo",
"InvocationType": "Event"
},
"End": true
}
}
}
To invoke a Lambda function, you can also call the resource Amazon Resource Name (ARN)
directly in
the "Resource"
string.
{
"StartAt":"CallFunction",
"States":{
"CallFunction": {
"Type":"Task",
"Resource":"arn:aws:lambda:us-east-1:123456789012:function:HelloFunction",
"End": true
}
}
}
When you invoke Lambda using an ARN directly from the "Resource"
field,
you can't specify .waitForTaskToken
. The state input is passed to Lambda as
the payload. The output of the function is nested inside a dictionary of metadata.
For example:
{ "ExecutedVersion":"$LATEST", "Payload":"
YOUR OUTPUT
", "SdkHttpMetadata":{ "HttpHeaders":{ "Connection":"keep-alive", "Content-Length":"4", "Content-Type":"application/json", "Date":"Fri, 26 Mar 2021 07:42:02 GMT", "X-Amz-Executed-Version":"$LATEST", "x-amzn-Remapped-Content-Length":"0", "x-amzn-RequestId":"0101aa0101-1111-111a-aa55-1010aaa1010", "X-Amzn-Trace-Id":"root=1-1a1a000a2a2-fe0101aa10ab;sampled=0" }, "HttpStatusCode":200 }, "SdkResponseMetadata":{ "RequestId":"6b3bebdb-9251-453a-ae45-512d9e2bf4d3" }, "StatusCode":200 }
You can invoke a specific Lambda function version or alias by specifying those options
in the ARN in the Resource
field. See the following in the Lambda
documentation:
For information on how to configure IAM when using Step Functions with other AWS services, see IAM Policies for Integrated Services.