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.
--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.
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.
In this state, execute sam build
, sam deploy --guided
to deploy the Lambda function.
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!"
}
}
}
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).
In this state, execute sam build
and sam deploy --guided
to deploy.
First, let's see what the visualized state machine looks like by updating the definition of the state machine.
As you changed, you can see that it branches off from "check state" and returns to "check state" after "retry lambda".
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.
--Specify a 9999 value for p1 --Specify the string "p2 strings." For p2
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").
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.
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.
Actually, after this, "retry" is returned twice more in a row, but "hello world" is returned the third time.
After that, you can see that the state of "hello world 2" is executed.
And the state ends as it is.
There is an updated content on August 13, 2020.
Allows you to see the type of Variable
in Choice
.
Example
"Variable": "$.foo",
"IsNull|IsString|IsNumeric|IsBoolean|IsTimestamp": true|false
Allows you to check for the existence of Variable
itself.
Example
"Variable": "$.foo",
"IsPresent": true|false
Allows you to perform a judgment check using wildcards.
Example
"Variable": "$.foo",
"StringMatches": "log-*.txt"
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"
You can check it here.
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.
https://github.com/hito-psv/sam-demo-006
Recommended Posts