Creating a Step Functions state machine that uses Lambda
In this tutorial, you will create a single-step workflow using AWS Step Functions to invoke an AWS Lambda function.
Note
Step Functions is based on state machines and tasks. In Step Functions, state machines are called workflows, which are a series of event-driven steps. Each step in a workflow is called a state. For example, a Task state represents a unit of work that another AWS service performs, such as calling another AWS service or API. Instances of running workflows performing tasks are called executions in Step Functions.
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
AWS Management Console or your favorite editor. AWS handles the details of providing a computing environment for your function and running it.
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 AWS account and AWS 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. The following is an example ARN:
arn:aws: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": "AWS 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
Make sure that your state machine is under the same AWS account and Region as the Lambda function you created earlier.
-
In the Choose a template dialog box, select Blank.
-
Choose Select to open Workflow Studio in Design mode.
-
In the States browser on the left, make sure you've chosen the Actions tab. Then, drag and drop the AWS 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.
-
Keep the default selection in the Payload dropdown list.
-
-
(Optional) Choose Definition to view the state machine's Amazon States Language (ASL) definition, which is automatically generated based on your selections in the Actions tab and Inspector panel.
-
Specify a name for your state machine. To do this, choose the edit icon next to the default state machine name of MyStateMachine. Then, in State machine configuration, specify a name in the State machine name box.
For example, enter the name
LambdaStateMachine
.Note
Names of state machines, executions, and activity tasks must not exceed 80 characters in length. These names must be unique for your account and AWS 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 accepts names for state machines, executions, activities, and labels that contain non-ASCII characters. Because such characters will not work with Amazon CloudWatch, we recommend using only ASCII characters so you can track metrics in CloudWatch.
-
-
(Optional) In State machine configuration, specify other workflow settings, such as state machine type and its execution role.
For this tutorial, keep all the default selections in State machine settings.
-
Choose Create.
-
In the Confirm role creation dialog box, choose Confirm to continue.
You can also choose View role settings to go back to State machine configuration.
Note
If you delete the IAM role that Step Functions creates, Step Functions can't recreate it later. Similarly, if you modify the role (for example, by removing Step Functions from the principals in the IAM policy), Step Functions can't restore its original settings later.
Step 4: Run the state machine
After you create your state machine, you can run it.
-
On the State machines page, choose LambdaStateMachine.
-
Choose Start execution.
The Start execution dialog box is displayed.
-
(Optional) Enter a custom execution name to override the generated default.
Non-ASCII names and logging
Step Functions accepts names for state machines, executions, activities, and labels that contain non-ASCII characters. Because such characters will not work with Amazon CloudWatch, we recommend using only ASCII characters so you can track metrics in CloudWatch.
-
In the Input area, replace the example execution data with the following.
{ "who" : "AWS Step Functions" }
"who"
is the key name that your Lambda function uses to get the name of the person to greet. -
Choose Start Execution.
Your state machine's execution starts, and a new page showing your running execution is displayed.
-
The Step Functions console directs you to a page that's titled with your execution ID. This page is known as the Execution Details page. On this page, you can review the execution results as the execution progresses or after it's complete.
To review the execution results, choose individual states on the Graph view, and then choose the individual tabs on the Step details pane to view each state's details including input, output, and definition respectively. For details about the execution information you can view on the Execution Details page, see Execution details overview.
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 an AWS Lambda function with Step Functions.