Search code examples
amazon-web-servicesaws-api-gatewayserverless-frameworkaws-step-functions

How to use api gateway with openAPI to pass json to step function using serverless framework?


How do I invoke an AWS Step Function using an API Gateway POST request using openAPI and then pass the JSON on to a step function, all defined using the serverless framework?

Below is what i've got so far, i've given the correct permissions to the execution role but still i'm receiving a 500 error on postman.

I would also like to be able to pass receive a json payload and then pass it on the step function?

Any pointers appreciated, thanks

Serverless.yml file:

service: webhook-json

frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs18.x

  
openApiIntegration:
    autoMock: true
    package: true
    inputFile: serverless.doc.yml
    outputFile: api.yml


resources:
  Resources:
    ApiGatewayRestApi:
      Type: AWS::ApiGateway::RestApi
      Properties:
        ApiKeySourceType: HEADER
        Body: ~ #autogenerated by plugin
        Description: "Some Description"
        FailOnWarnings: false
        Name: dev-some-name
        EndpointConfiguration:
          Types:
            - REGIONAL
    ApiGatewayDeployment:
      Type: AWS::ApiGateway::Deployment
      Properties:
        RestApiId:
          Ref: ApiGatewayRestApi
        StageName: dev
    testRole:
      Type: AWS::IAM::Role
      Properties:
        RoleName: testRole
        Path: /
        AssumeRolePolicyDocument: 
          Version: '2012-10-17'
          Statement: 
            - Effect: Allow
              Principal:
                Service:
                  - apigateway.amazonaws.com
              Action: 
                - sts:AssumeRole
        Policies:
          - PolicyName: allow-execution
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
                - Effect: Allow
                  Action: states:StartExecution
                  Resource: '*'

                    
                
    
stepFunctions:
  stateMachines:
    hellostepfunc1:
      name: testermachine
      definition:
        Comment: "test machine"
        StartAt: StartState
        States:
          StartState:
            Type: Pass
            End: true

plugins:
  - serverless-step-functions
  - serverless-openapi-documentation
  - serverless-openapi-integration-helper

OpenAPI schema:

# ./schema.yml
openapi: 3.0.0
info:
  description: test
  version: 1.0.0
  title: testing
paths:
  /api:
    post:
      x-amazon-apigateway-integration:
        credentials:
          Fn::GetAtt: [ testRole, Arn ]
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:states:action/StartExecution
        httpMethod: POST
        type: aws
        responses:
          default:
            statusCode: 200
      summary: test
      responses:
        200:
          description: Success

Solution

  • I've taken a look at the code and saw some issues.

    You are missing a path to the uri field in x-amazon-apigateway-integration.

    Also missing request mapping template in the OpenAPI schema, I believe you need this to map JSON payload from POST request to input for the step function.

    Something like this

        requestTemplates:
          application/json: |
            {
              "input": "$input.json('$')"
            }
        responses:
          default:
            statusCode: 200
      summary: test
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object