Schrittfunktionen, die als Workflow-Service angekündigt wurden. Andere Workflow-Services umfassen Simple Workflow (SWF). Für neue Anwendungen wird jedoch empfohlen, Schrittfunktionen zu berücksichtigen. Wenn Sie Ihre Anforderungen nicht erfüllen, können Sie SWF ausprobieren.
Derzeit sind 6 Arten von Blaupausen als Beispiele in den Schrittfunktionen verfügbar. Wenn Sie sich nur diesen Code ansehen, scheinen Sie die erforderlichen Mindestspezifikationen zu verstehen.
Schauen Sie sich zuerst die vorbereitete Blaupause an und erstellen Sie eine Zustandsmaschine mit allen am Ende definierten Zuständen.
Blueprint Hello World
{
"Comment": "A Hello World example of the Amazon States Language using a Pass state",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Pass",
"Result": "Hello World!",
"End": true
}
}
}
Wait State
{
"Comment": "An example of the Amazon States Language using wait states",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "wait_using_seconds"
},
"wait_using_seconds": {
"Type": "Wait",
"Seconds": 10,
"Next": "wait_using_timestamp"
},
"wait_using_timestamp": {
"Type": "Wait",
"Timestamp": "2015-09-04T01:59:00Z",
"Next": "wait_using_timestamp_path"
},
"wait_using_timestamp_path": {
"Type": "Wait",
"TimestampPath": "$.expirydate",
"Next": "wait_using_seconds_path"
},
"wait_using_seconds_path": {
"Type": "Wait",
"SecondsPath": "$.expiryseconds",
"Next": "FinalState"
},
"FinalState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
Retry Failure
{
"Comment": "A Retry example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Retry": [
{
"ErrorEquals": ["HandledError"],
"IntervalSeconds": 1,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.TaskFailed"],
"IntervalSeconds": 30,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 5,
"BackoffRate": 2.0
}
],
"End": true
}
}
}
Parallel
{
"Comment": "An example of the Amazon States Language using a parallel state to execute two branches at the same time.",
"StartAt": "Parallel",
"States": {
"Parallel": {
"Type": "Parallel",
"Next": "Final State",
"Branches": [
{
"StartAt": "Wait 20s",
"States": {
"Wait 20s": {
"Type": "Wait",
"Seconds": 20,
"End": true
}
}
},
{
"StartAt": "Pass",
"States": {
"Pass": {
"Type": "Pass",
"Next": "Wait 10s"
},
"Wait 10s": {
"Type": "Wait",
"Seconds": 10,
"End": true
}
}
}
]
},
"Final State": {
"Type": "Pass",
"End": true
}
}
}
Catch Failure
{
"Comment": "A Catch example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Catch": [
{
"ErrorEquals": ["HandledError"],
"Next": "CustomErrorFallback"
},
{
"ErrorEquals": ["States.TaskFailed"],
"Next": "ReservedTypeFallback"
},
{
"ErrorEquals": ["States.ALL"],
"Next": "CatchAllFallback"
}
],
"End": true
},
"CustomErrorFallback": {
"Type": "Pass",
"Result": "This is a fallback from a custom lambda function exception",
"End": true
},
"ReservedTypeFallback": {
"Type": "Pass",
"Result": "This is a fallback from a reserved error code",
"End": true
},
"CatchAllFallback": {
"Type": "Pass",
"Result": "This is a fallback from a reserved error code",
"End": true
}
}
}
Choice State
{
"Comment": "An example of the Amazon States Language using a choice state.",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "ChoiceState"
},
"ChoiceState": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.foo",
"NumericEquals": 1,
"Next": "FirstMatchState"
},
{
"Variable": "$.foo",
"NumericEquals": 2,
"Next": "SecondMatchState"
}
],
"Default": "DefaultState"
},
"FirstMatchState": {
"Type" : "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnFirstMatch",
"Next": "NextState"
},
"SecondMatchState": {
"Type" : "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnSecondMatch",
"Next": "NextState"
},
"DefaultState": {
"Type": "Fail",
"Cause": "No Matches!"
},
"NextState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
Die folgenden 7 Arten von Zuständen sind verfügbar.
Erstellen Sie die folgende Zustandsmaschine mit allen 7 Zustandstypen.
JSON Die obige Zustandsmaschine ist in JSON wie folgt definiert.
{
"Comment": "Sample Step functions flow",
"StartAt": "Process1",
"States": {
"Process1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process1",
"Next": "ChoiceStep"
},
"ChoiceStep": {
"Type": "Choice",
"Choices": [{
"Variable": "$.processResult",
"StringEquals": "Process2a",
"Next": "SuccessProcess"
}, {
"Variable": "$.processResult",
"StringEquals": "Process2b",
"Next": "Process2b-1"
}]
},
"SuccessProcess": {
"Type": "Succeed"
},
"Process2b-1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Next": "Process2b-2"
},
"Process2b-2": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Catch": [{
"ErrorEquals": [
"HandledError"
],
"Next": "FailedProcess"
}],
"Next": "Process3"
},
"FailedProcess": {
"Type": "Fail"
},
"Process3": {
"Type": "Wait",
"Seconds": 30,
"Next": "Process4"
},
"Process4": {
"Type": "Parallel",
"Next": "Process5",
"Branches": [
{
"StartAt": "Process4a",
"States": {
"Process4a": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"End": true
}
}
},
{
"StartAt": "Process4b-1",
"States": {
"Process4b-1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Retry": [{
"ErrorEquals": [
"HandledError"
],
"IntervalSeconds": 3,
"MaxAttempts": 5,
"BackoffRate": 1.5
}],
"Next": "Process4b-2"
},
"Process4b-2": {
"Type": "Pass",
"Result": {
"data1": 12345,
"data2": -12345
},
"ResultPath": "$.test-data",
"Next": "Process4b-3"
},
"Process4b-3": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"End": true
}
}
}
]
},
"Process5": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process3",
"End": true
}
}
}
Bereiten Sie die folgenden drei Labda-Funktionen vor.
def lambda_handler(event, context):
event["step"] = 1
event["processResult"] = "Process2b"
return event
def lambda_handler(event, context):
event["step"] += 1
return event
def lambda_handler(event, context):
print event
return event
Wählen Sie in der Verwaltungskonsole [Schrittfunktionen] aus.
Wählen Sie Erste Schritte.
Geben Sie Folgendes ein und wählen Sie [Zustandsmaschine erstellen]. Give a name to your state machine: SampleStateMachineWithVariableOfTypes
Geben Sie den JSON aus dem vorherigen ein.
Wenn Sie die Ladetaste der Vorschau drücken, wird der Status der Zustandsmaschine angezeigt.
IAM role for your state machine executions: StatesExecutionRole-us-east-1
{
"processResult": "Process2b"
}
Output
output: [{"processResult": "Process2b", "step": 4},{"test-data": {"data1": 12345, "data2": -12345}, "processResult": "Process2b", "step": 5}]
Tips
jq
überprüfen.python sample.py
wie folgt debuggen können.def lambda_handler(event, context):
event["step"] = 1
event["processResult"] = "Process2b"
return event
if __name__ == "__main__":
lambda_handler("","")
――Ich werde wütend, wenn ich eine zusätzliche Aufgabe definiere
Recommended Posts