meowsephine
meowsephine

Reputation: 392

How can I set up an APIGateway method to return a value from a step function?

I have a state machine consisting of several lambdas that I've set up using a Cloudformation template that does some processing and ultimately saves a file to S3; I won't add that in here unless requested because it works properly. The final portion of the step function is a lambda that outputs the following:

{
   "output": {
      "statusCode": "200",
      "body": "{\"dataset-name\": \"inserted\"}",
      "headers": {
           "Content-Type": "application/json"
       }
    }
}

Here is the cloudformation template I have to set up an API method that calls this step function:

PostDatasetMethod:
Type: AWS::ApiGateway::Method
DependsOn: Resource
Properties:
  ApiKeyRequired: false
  AuthorizationType: NONE
  HttpMethod: POST
  Integration:
    Credentials: !Ref 'IamRoleArn'
    IntegrationHttpMethod: POST
    PassthroughBehavior: NEVER
    Type: AWS
    Uri: 'arn:aws:apigateway:us-east-1:states:action/StartExecution'
    IntegrationResponses:
      - StatusCode: 200
    RequestTemplates:
      application/json: !Sub |
        {
          "input": "$util.escapeJavaScript($input.json('$'))",
          "stateMachineArn": "${StateMachine}"
        }
  ResourceId: !Ref 'Resource'
  RestApiId: !Ref 'Api'

However, I'm getting the error

Execution failed due to configuration error: No match for output mapping and no default output mapping configured

when I call the API, even though the step function completes successfully, I think because I'm not sure how to set up the method and integration responses in the CloudFormation template. I just need the json above passed through as-is, similar to what is done by default for lambdas. What else do I need in the CloudFormation template in order for this API to work as intended?

Upvotes: 1

Views: 1172

Answers (1)

balsick
balsick

Reputation: 1201

Step Functions are called asynchronously and there is no natural way to do a synchronous call. This means that you cannot make your API Gateway Method return the output of the Step Functions.

Somebody suggested various ways to achieve this, maybe the most discussed one is this: Create a lambda function whose only purpose is to start the Step Functions' execution and than starts polling for the Step Functions' result.

Here's some info on getting Step Functions' result. Be careful, this can only work if the Step Functions' duration is less then 30 seconds because of API Gateway's execution limits.

Upvotes: 2

Related Questions