Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set "Use Lambda Proxy integration" in swagger for API-Gateway?

How to set Use Lambda Proxy integration in swagger for API-Gateway?

My current swagger is bellow but I would really like to setup the proxy integration. I would let me simplify a lot of things, not to mention I could remove the requestTemplates and responses blocks from the swagger definition.

I am attempting to setup the new Lambda Proxy mode from this blog post.

Current swagger:

  '/document':
    options:
      summary: CORS support
      description: |
        Enable CORS by returning correct headers
      consumes:
        - application/json
      produces:
        - application/json
      tags:
        - CORS
      x-amazon-apigateway-integration:
        type: mock
        requestTemplates:
          application/json: |
            {
              "statusCode" : 200
            }
        responses:
          "default":
            statusCode: "200"
            responseParameters:
              method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'"
              method.response.header.Access-Control-Allow-Methods : "'*'"
              method.response.header.Access-Control-Allow-Origin : "'*'"
            responseTemplates:
              application/json: |
                {}
      responses:
        200:
          description: Default response for CORS method
          headers:
            Access-Control-Allow-Headers:
              type: "string"
            Access-Control-Allow-Methods:
              type: "string"
            Access-Control-Allow-Origin:
              type: "string"
    x-amazon-apigateway-any-method:
      produces:
      - "application/json"
      responses:
        200:
          description: "200 response"
          schema:
            $ref: "#/definitions/Empty"
      x-swagger-router-controller: main
      x-lambda-function: ../../swiki/build/document
      x-amazon-apigateway-integration:
        type: aws
        httpMethod: POST
        uri: arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/@@LambdaFunctionDocument/invocations
        credentials: @@APIGatewayExecutionRole
        passthroughBehavior: "when_no_templates"
        requestTemplates:
          application/json: |
            #set($allParams = $input.params())
            {
              "bodyJson" : $input.json('$'),
              "format": "html",
              "params" : {
            #foreach($type in $allParams.keySet())
              #set($params = $allParams.get($type))
                "$type" : {
              #foreach($paramName in $params.keySet())
                  "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
                #if($foreach.hasNext),#end
              #end
                }
              #if($foreach.hasNext),#end
            #end
              },
              "stageVariables" : {
            #foreach($key in $stageVariables.keySet())
                "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
              #if($foreach.hasNext),#end
            #end
              },
              "context" : {
                "accountId" : "$context.identity.accountId",
                "apiId" : "$context.apiId",
                "apiKey" : "$context.identity.apiKey",
                "authorizerPrincipalId" : "$context.authorizer.principalId",
                "caller" : "$context.identity.caller",
                "cognitoAuthenticationProvider" : "$context.identity.cognitoAuthenticationProvider",
                "cognitoAuthenticationType" : "$context.identity.cognitoAuthenticationType",
                "cognitoIdentityId" : "$context.identity.cognitoIdentityId",
                "cognitoIdentityPoolId" : "$context.identity.cognitoIdentityPoolId",
                "httpMethod" : "$context.httpMethod",
                "stage" : "$context.stage",
                "sourceIp" : "$context.identity.sourceIp",
                "user" : "$context.identity.user",
                "userAgent" : "$context.identity.userAgent",
                "userArn" : "$context.identity.userArn",
                "requestId" : "$context.requestId",
                "resourceId" : "$context.resourceId",
                "resourcePath" : "$context.resourcePath"
              }
            }
        responses:
          default:
            statusCode: "200"
like image 753
Justin808 Avatar asked Sep 26 '16 01:09

Justin808


2 Answers

For Lambda Proxy integration, the required fields are

  x-amazon-apigateway-integration:
    type: aws_proxy
    httpMethod: <method>
    uri:  <function_uri>
    credentials: <optional_creds>

edit: typo edit2: fixed type edit3: add httpMethod

like image 185
jackko Avatar answered Dec 31 '22 19:12

jackko


Please see this blog post for a complete example of the 3 new features (greedy path, ANY method, proxy integration) for both HTTP and Lambda proxies:

---
swagger: "2.0"
info:
  version: "2016-09-23T22:23:23Z"
  title: "Simple Proxy Example - Ryan Green"
host: "zte3bswjjb.execute-api.us-east-1.amazonaws.com"
basePath: "/demo"
schemes:
- "https"
paths:
  /http/{proxy+}:
    x-amazon-apigateway-any-method:
      parameters:
      - name: "proxy"
        in: "path"
      x-amazon-apigateway-integration:
        type: "http_proxy"
        uri: "http://httpbin.org/{proxy}"
        httpMethod: "ANY"
        passthroughBehavior: "when_no_match"
        requestParameters:
          integration.request.path.proxy: "method.request.path.proxy"
  /lambda/{proxy+}:
    x-amazon-apigateway-any-method:
      parameters:
      - name: "proxy"
        in: "path"
      responses: {}
      x-amazon-apigateway-integration:
        type: "aws_proxy"
        uri: "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:[MY_ACCOUNT_ID]]:function:[MY_FUNCTION_NAME]]/invocations"
        passthroughBehavior: "when_no_match"
        httpMethod: "POST"
like image 36
RyanG Avatar answered Dec 31 '22 18:12

RyanG