Creating a Step Functions State Machine That Uses Lambda - Amazon Step Functions
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

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.

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.

  1. Open the Lambda console and choose Create function.

  2. On the Create function page, choose Author from scratch.

  3. In the Basic information section, configure your Lambda function:

    1. For Function name, enter HelloFunction.

    2. For Runtime, choose Node.js 14.x.

    3. In Change default execution role, choose Create a new role with basic Lambda permissions.

    4. Choose Create function.

    5. 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 
                                        copy Amazon Resource Name
                                    . The following is an example ARN:

      arn:aws-cn:lambda:us-east-1:123456789012:function:HelloFunction
  4. 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 the event object passed into your function. You add input data for this function later, when you start a new execution. The callback method returns the assembled greeting from your function.

  5. Choose Deploy.

Step 2: Test the Lambda Function

Test your Lambda function to see it in operation.

  1. Choose Test.

  2. In the Configure test event dialog box, enter HelloEvent in the Event name box.

  3. Replace the example data with the following.

    { "who": "Amazon Step Functions" }

    The "who" entry corresponds to the event.who field in your Lambda function, completing the greeting. You will input the same input data when you run your state machine.

  4. Choose Create.

  5. 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.

  6. Choose the Execution results tab to see the output.

Step 3: Create a State Machine

Use the Step Functions console to create a state machine that invokes the Lambda function that you created earlier in Step 1.

  1. 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.

  2. On the Choose authoring method page, choose Design your workflow visually.

  3. For Type, retain the default selection, that is, Standard.

  4. Choose Next. This will open Workflow Studio.

  5. From the States browser on the left, choose the Actions panel.

    1. Drag and drop the Amazon Lambda Invoke API into the empty state labelled Drag first state here.

      
                                    States browser
  6. In the Inspector panel on the right, configure the Lambda function and its name:

    1. Choose Configuration, and then edit the State name, if required.

    2. In the API Parameters section, choose the Lambda function that you created earlier in the Function name dropdown list.

    3. Retain the default selection in the Payload dropdown list.

  7. Choose Next.

  8. 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.

  9. Choose Next.

  10. 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.

  11. In Execution role, under the Permissions section, choose Create new role.

  12. Choose Create state machine.

Step 4: Start a New Execution

After you create your state machine, you start an execution.

  1. On the LambdaStateMachine page, choose Start execution.

    The Start execution dialog box is displayed.

  2. (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.

  3. 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.

  4. Choose Start Execution.

    A new execution of your state machine starts, and a new page showing your running execution is displayed.

  5. To view the results of your execution, choose the Execution output tab.