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.
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.
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.
-
In the Basic information section, configure your Lambda function:
-
For Function name, enter
HelloFunction
. -
For Runtime, choose Node.js 14.x.
-
In Change default execution role, choose Create a new role with basic Lambda permissions.
-
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.exports.handler = (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.
-
In the Configure test event dialog box, enter
HelloEvent
in the Event name box. -
Replace the example 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 Create.
-
On the
HelloFunction
page, choose Test to test your Lambda function using the new data.The results of the test are displayed in the Execution results tab.
-
Choose the Execution results tab to see the output.
Step 3: Create a State Machine
Use the Step Functions console
-
Open the Step Functions console
and choose Create a 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, retain the default selection, that is, 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 and its name:
-
Choose Configuration, and then edit the State name, if required.
-
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
).
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.