[AWS] Play with Step Functions (SAM + Lambda) Part.3 (Branch)

Review of Part.2

In Part2, we mainly did the following.

--Try passing fixed arguments to Lambda --Try passing an external argument to Lambda --Try passing a context object to Lambda --Try passing the return value of Lambda to the argument of another Task

This time, I will start from the continuation. If you start from here, https://github.com/hito-psv/sam-demo-005 It's okay to have the code of `git clone``, or you can try it from Part2.

This time's target

--Lambda returns random results --Change the task to be executed next depending on the result --When retrying, re-execute after a while

I would like to target. For the Lambda part, I would like to play around with the "Hello World" function created in Part2 and try various things.

Preparation

Modify the Lambda function

I would like to return some patterns of strings with random numbers in the current code.

hello_world/app.py


import logging
import random

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info(event)

    message_array = [
        "hello world",
        "retry"
    ]
    message = random.choice(message_array)
    logger.info(message)

    return {
        "statusCode": 200,
        "body": {
            "message": message,
        }
    }

Now the resulting message will

Either string of is returned.

Build / deploy

In this state, execute sam build, sam deploy --guided to deploy the Lambda function.

Change the behavior depending on the return value of the Lambda function

Modify state machine definition

First, modify the definition of the state machine. I will explain the correction contents later, so first I will try to correct it like this.

step_functions/state_machine.json


{
  "StartAt": "hello world 1",
  "States": {
    "hello world 1": {
      "Type": "Task",
      "Resource": "${HelloWorldFunction}",
      "Parameters": {
        "p1.$": $.p1,
        "p2.$": "$.p2",
        "p3": {
          "p3-1": 20,
          "p3-2": "xyz"
        },
        "all.$": "$"
      },
      "ResultPath": "$.hello_world_result",
      "OutputPath": "$",
      "Next": "check state"
    },
    "retry lambda": {
      "Type": "Task",
      "Resource": "${HelloWorldFunction}",
      "InputPath": "$",
      "ResultPath": "$.hello_world_result",
      "OutputPath": "$",
      "Next": "check state"
    },
    "check state": {
      "Type" : "Choice",
      "Choices": [
        {
          "Variable": "$.hello_world_result.body.message",
          "StringEquals": "hello world",
          "Next": "hello world 2"
        },
        {
          "Variable": "$.hello_world_result.body.message",
          "StringEquals": "retry",
          "Next": "wait state"
        }
      ],
      "Default": "fail state"
    },
    "wait state": {
      "Type": "Wait",
      "Seconds": 5,
      "Next": "retry lambda"
    },
    "hello world 2": {
      "Type": "Task",
      "Resource": "${HelloWorldFunction}",
      "InputPath": "$",
      "End": true
    },
    "fail state": {
      "Type": "Fail",
      "Cause": "No Matches!"
    }
  }
}
  1. "hello world 1" executes the "check state" state to the next state
  2. For "check state", if the value of "$ .hello_world_result.body.message" is "hello world", execute "hello world 2" in the next state.
  3. For "check state", if the value of "$ .hello_world_result.body.message" is "retry", execute "wait state" to the next state.
  4. For "check state", if the value of "$ .hello_world_result.body.message" is other than "hello world" and "retry", execute "fail state" in the next state (this time it cannot happen).
  5. "wait state" executes the state of "retry lambda" after 5 seconds.
  6. "retry lambda" executes the "check state" state after calling the lambda function
  7. "hello world 2" ends state execution after calling the lambda function
  8. "fail state" terminates state execution as an abnormal termination

It is described as. In "Choices"

--Variable: Check target --StringEquals: If the string is the same as this value --Next: State to be executed next

To specify. The operations that can be specified in the String Equals section are [AWS Step Functions Developer Guide](https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/amazon-states-language-" Please refer to choice-state.html).

Build / deploy

In this state, execute sam build and sam deploy --guided to deploy.

Checking the state machine

First, let's see what the visualized state machine looks like by updating the definition of the state machine.

sf1.png

As you changed, you can see that it branches off from "check state" and returns to "check state" after "retry lambda".

Try running a state machine

Let's run the state machine from the management console. Since the input values p1 and p2 are used for the correction from Part.2, please specify the input orchid as follows.

68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3432373939382f64303138383561632d323261362d623039372d646435622d3135383438333539616634372e706e67.png

--Specify a 9999 value for p1 --Specify the string "p2 strings." For p2

Let's see the result

First, let's check the visual flow. All the paths go through wonderfully (because it is random, there can be a flow of "hello world 1" → "check state" → "hello world 2").

sf2.png

Now, let's check if the Choice process is working properly. First, here is the return of the Lambda function called from the "hello world 1" state.

sf3.png

You can see that the message part of the body is "retry". As a result, the "wait state" is then executed, and after 5 seconds (checking the times # 10 and # 11), the "retry lambda state" is being executed.

sf4.png

Actually, after this, "retry" is returned twice more in a row, but "hello world" is returned the third time.

sf5.png

After that, you can see that the state of "hello world 2" is executed.

sf6.png

And the state ends as it is.

Newly added "Choice" state

There is an updated content on August 13, 2020.

Value test (type check)

Allows you to see the type of Variable in Choice.

Example


   "Variable": "$.foo",
   "IsNull|IsString|IsNumeric|IsBoolean|IsTimestamp": true|false

Existence check

Allows you to check for the existence of Variable itself.

Example


  "Variable": "$.foo",
  "IsPresent": true|false

Wildcard check

Allows you to perform a judgment check using wildcards.

Example


  "Variable": "$.foo",
  "StringMatches": "log-*.txt"

Comparison with another input field

Allows you to compare an input field with another input field. It can be used to check changes in the status.

Example


  "Variable": "$.foo",
  "StringEqualsPath": "$.bar"

Details

You can check it here.

AWS Step Functions adds updates to ‘choice’ state, global access to context object, dynamic timeouts, result selection, and intrinsic functions to Amazon States Language

Summary

This time I tried branching with Choice, but at this point it feels like a workflow at last. I think that it is possible to define a certain state machine only with the contents of Part.1 to Part.3 so far. Next time, I would like to call a service other than Lambda with a task definition.

Sample code repository

https://github.com/hito-psv/sam-demo-006

Recommended Posts

[AWS] Play with Step Functions (SAM + Lambda) Part.3 (Branch)
[AWS] Play with Step Functions (SAM + Lambda) Part.1 (Basic)
[AWS] Play with Step Functions (SAM + Lambda) Part.2 (Parameter)
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
Regular serverless scraping with AWS lambda + scrapy Part 1.8
Serverless scraping using selenium with [AWS Lambda] -Part 1-
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
AWS Step Functions to learn with a sample
Delete DynamoDB data after 5 minutes with AWS Step Functions
AWS Lambda with PyTorch [Lambda import]
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Play with a turtle with turtle graphics (Part 1)
[AWS] Create API with API Gateway + Lambda
Using Lambda with AWS Amplify with Go
Serverless scraping on a regular basis with AWS lambda + scrapy Part 1
Notify HipChat with AWS Lambda (Python)
[AWS] Using ini files with Lambda [Python]
Play handwritten numbers with python Part 2 (identify)
I want to play with aws with python
[AWS] Link Lambda and S3 with boto3
Connect to s3 with AWS Lambda Python
[AWS] Do SSI-like things with S3 / Lambda
Python + Selenium + Headless Chromium with aws lambda
I just did FizzBuzz with AWS Lambda
[Piyopiyokai # 1] Let's play with Lambda: Creating a Lambda function
Play with Lambda layer (python) for about 5 minutes
LINE BOT with Python + AWS Lambda + API Gateway
[AWS] Try tracing API Gateway + Lambda with X-Ray
I tried connecting AWS Lambda with other services
Infrastructure construction automation with CloudFromation + troposphere + AWS Lambda
I wanted to operate google spread sheet with AWS lambda, so I tried it [Part 2]