Creating a Step Functions state machine that uses Lambda
In this tutorial, you will create a single-step workflow using Amazon Step Functions to invoke an Amazon Lambda function.
Note
In Step Functions, a workflow is called a state machine, which is a series of event-driven steps. Each step in a workflow is called a state. A Task
state represents a unit of work that another Amazon service, such as Amazon Lambda, performs. A Task
state can call any Amazon service or API. For more information, see:
Lambda is well-suited for Task
states, because Lambda functions are serverless and easy to write. You can write code in the
Amazon Web Services Management Console or your favorite editor. Amazon handles the details of providing a computing environment for your function and running it.
Topics
Step 1: Create a Lambda function
Your Lambda function receives event data and returns a greeting message.
Important
Ensure that your Lambda function is under the same Amazon account and Amazon Region as your state machine.
-
Open the Lambda console
and choose Create function. -
On the Create function page, choose Author from scratch.
-
For Function name, enter
HelloFunction
. -
Keep the default selections for all other options, and then choose Create function.
-
After your Lambda function is created, copy the function's Amazon Resource Name (ARN) displayed in the upper-right corner of the page. To copy the ARN, click
. The following is an example ARN:
arn:aws-cn:lambda:us-east-1:123456789012:function:
HelloFunction
-
Copy the following code for the Lambda function into the Code source section of the
HelloFunction
page.export const handler = async(event, context, callback) => { callback(null, "Hello from " + event.who + "!"); };
This code assembles a greeting using the
who
field of the input data, which is provided by theevent
object passed into your function. You add input data for this function later, when you start a new execution. Thecallback
method returns the assembled greeting from your function. -
Choose Deploy.
Step 2: Test the Lambda function
Test your Lambda function to see it in operation.
-
Choose Test.
-
For Event name, enter
HelloEvent
. -
Replace the Event JSON data with the following.
{ "who": "Amazon Step Functions" }
The
"who"
entry corresponds to theevent.who
field in your Lambda function, completing the greeting. You will input the same input data when you run your state machine. -
Choose Save and then choose Test.
-
To review the test results, under Execution result, expand Details.
Step 3: Create a state machine
Use the Step Functions console to create a state machine that invokes the Lambda function that you created in step 1.
-
Open the Step Functions console
and choose Create state machine. Important
Ensure that your state machine is under the same Amazon account and Region as the Lambda function you created earlier.
-
On the Choose authoring method page, choose Design your workflow visually.
-
For Type, keep the default selection (Standard).
-
Choose Next. This will open Workflow Studio.
-
From the States browser on the left, choose the Actions panel.
-
Drag and drop the Amazon Lambda Invoke API into the empty state labelled Drag first state here.
-
-
In the Inspector panel on the right, configure the Lambda function:
-
In the API Parameters section, choose the Lambda function that you created earlier in the Function name dropdown list.
-
Retain the default selection in the Payload dropdown list.
-
-
Choose Next.
-
On the Review generated code page, review the state machine's Amazon States Language (ASL) definition, which is automatically generated based on your selections in the Actions and Inspector panel.
-
Choose Next.
-
Enter a Name for your state machine, for example,
.LambdaStateMachine
Note
State machine, execution, and activity names must be 1–80 characters in length, must be unique for your account and Amazon Region, and must not contain any of the following:
-
Whitespace
-
Wildcard characters (
? *
) -
Bracket characters (
< > { } [ ]
) -
Special characters (
: ; , \ | ^ ~ $ # % & ` "
) -
Control characters (
\\u0000
-\\u001f
or\\u007f
-\\u009f
).
If your state machine is of type Express, you can provide the same name to multiple executions of the state machine. Step Functions generates a unique execution ARN for each Express state machine execution, even if multiple executions have the same name.
Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.
-
-
In Execution role, under the Permissions section, choose Create new role.
-
Choose Create state machine.
Step 4: Start a new execution
After you create your state machine, you start an execution.
-
On the
LambdaStateMachine
page, choose Start execution.The Start execution dialog box is displayed.
-
(Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.
Note
Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.
-
In the execution input area, replace the example data with the following.
{ "who" : "Amazon Step Functions" }
"who"
is the key name that your Lambda function uses to get the name of the person to greet. -
Choose Start Execution.
A new execution of your state machine starts, and a new page showing your running execution is displayed.
-
To view the results of your execution, choose the Execution output tab.
Note
You can also pass payloads while invoking Lambda from a state machine. For more information and examples about invoking Lambda by passing payload in the
Parameters
field, see Invoke Lambda with Step Functions.