Search code examples
aws-lambdaaws-api-gatewaystate-machineaws-step-functions

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
        }
    }
}

Solution

  • 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