Tutorial 2: Define the first service integration using a Lambda function - 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 (PDF).

Tutorial 2: Define the first service integration using a Lambda function

In this tutorial, you learn how to define the first service integration for your workflow. You use the Task state named Get credit limit to invoke a Lambda function. Within Task states, you can use the Amazon SDK integrations that Step Functions supports.

To define the first service integration for your workflow, first create a Lambda function. Then, update your workflow to specify the service integration with the Lambda function. The Lambda function used in this tutorial returns a randomly generated integer representing the credit limit that an applicant has applied for.

Step 1: Create and test the Lambda function

You can write code for the function in the Amazon Web Services Management Console or your favorite editor. In the following steps, you create a Node.js Lambda function titled RandomNumberforCredit.

Important

Make sure that the workflow prototype you created in Tutorial 1 is under the same Amazon Web Services Region as the Lambda function you’ll create in this tutorial.

  1. In a new tab or window, open the Lambda console and create a Node.js 16.x Lambda function titled RandomNumberforCredit. For information about creating a Lambda function using the console, see Create a Lambda function in the console in the Amazon Lambda Developer Guide.

  2. On the RandomNumberforCredit page, choose index.mjs and replace the existing code in the Code source area with the following code.

    export const handler = async function(event, context) { const credLimit = Math.floor(Math.random() * 10000); return (credLimit); };
  3. From the Function overview section, copy the Amazon Resource Name of the Lambda function and save it in a text file. You’ll need the function ARN while specifying the service integration for the Get credit limit state. The following is an example ARN:

    arn:aws-cn:lambda:us-west-2:123456789012:function:HelloWorld
  4. Choose Deploy and then choose Test to deploy the changes and see the output of the Lambda function.

Step 2: Update the workflow – configure the Get credit limit state

In the Step Functions console, you’ll update your workflow to specify service integration with the RandomNumberforCredit Lambda function that you created in Step 1.

  1. Open the Step Functions console window containing the workflow prototype you created in Tutorial 1.

  2. Choose the Get credit limit state, and in the Configuration tab, do the following:

    1. For Integration type, keep the default selection of Optimized.

      Using Step Functions, you can integrate with other Amazon Web Services and orchestrate them in your workflows. For more information about service integrations and their types, see Using Amazon Step Functions with other services.

    2. For Function name, choose the RandomNumberforCredit Lambda function from the dropdown list.

    3. Keep the default selections for rest of the items.

  3. Keep this window open and proceed to the next tutorial for further actions.

Note

In this tutorial, you learned how to integrate with a Lambda function within a Task state in your workflows. You can also use other supported Amazon SDK integrations in the Task state by specifying the service name and API call, as shown in the following syntax:

arn:aws-cn:states:::aws-sdk:serviceName:apiAction

For more information, see Using Amazon Step Functions with other services.

Next steps

In the next tutorial, you’ll implement conditional logic in your workflow. Conditional logic in Step Functions state machines behaves similar to an if-else statement in most common programming languages. You’ll use conditional logic in your workflow to determine the execution path based on conditional information.