Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

AWS lambda state machine and api-gateway

Probably a beginner question, I set my lambda concurrency to 1, only one at a time, when I call the lambda twice, I get the error "Internal Server Error", instead I would like to have a more precise message.

So I setup a state machine, but I still get "Internal Server Error". What I have:

api-gateways ==> (State Machine ? => Lambda )

Can it work this way ? Below the state machine json

{
    "Comment": "Example of a workflow which invokes your Lambda function, implements retries, and catches errors. Learn more at https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-creating-lambda-state-machine.html",
    "StartAt": "Call update lambda",
    "States": {
        "Call update lambda": {
            "Type": "Task",
            "Resource": "arn:aws:states:::lambda:invoke",
            "Parameters": {
                "FunctionName": "aem-update:$LATEST",
                "Payload": {
                    "Input.$": "$"
                }
            },
            "Catch": [
                {
                    "ErrorEquals": [
                        "States.ALL"
                    ],
                    "Next": "CatchFallback"
                }
            ],
            "End": true
        },
        "CatchFallback": {
            "Type": "Pass",
            "Result": "This is a fallback from a custom Lambda function exception",
            "End": true
        }
    }
}
question from:https://stackoverflow.com/questions/65887789/aws-lambda-state-machine-and-api-gateway

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The API Gateway Integration with StepFunction is an asynchronous call with StartExecution.

You invoke the statemachine by providing an arn, something like below, provided you have created necessary resources:

curl -X POST -d '{"input": "{}","name": "MyExecution","stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld"}' https://a1b2c3d4e5.execute-api.us-east-1.amazonaws.com/alpha/execution

The execution ARN and its epoch date are returned, as shown in the following example.

{
"executionArn":"arn:aws:states:us-east-1:123456789012:execution:HelloWorld:MyExecution",
"startDate":1.486772644911E9
}

I assume you have created another endpoint in API for DescribeExecution, where you provide the above executionArn and fetch the result of the execution. Something like below:

$ curl -s -X POST -d '{"executionArn": "arn:aws:states:eu-central-1:1234567890:execution:mystatemachine:MyExecution10"}' https://1234abcdef.execute-api.eu-central-1.amazonaws.com/v1/getexecution|jq .
{
  "executionArn": "arn:aws:states:eu-central-1:1234567890:execution:mystatemachine:MyExecution10",
  "input": "{}",
  "inputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "name": "MyExecution10",
  "output": ""This is a fallback from a custom Lambda function exception"",
  "outputDetails": {
    "__type": "com.amazonaws.swf.base.model#CloudWatchEventsExecutionDataDetails",
    "included": true
  },
  "startDate": 1612006859.079,
  "stateMachineArn": "arn:aws:states:eu-central-1:1234567890:stateMachine:mystatemachine",
  "status": "SUCCEEDED",
  "stopDate": 1612006859.279,
  "traceHeader": "Root=1-601545cb-2ca62e87242d4cf21724f7e4;Sampled=1"
}

And as you can see, I am getting the correct message from the CatchFallback.

My State Machine execution and corresponding output generated by CatchFallback

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...