7

I am trying to deploy a lambda function and API gateway . I create a .net core web API project with AWS CLI . Deploying only the function and creating the API gateway and resource manually on aws web console does work.

If I do include the API gateway in the template, after doing SAM package deploying through web console or CLI I get the following error:

"No integration defined for method (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: ....)"

Is anything wrong or missing here?

SAM package command:

sam package  --template-file  sam-profile.yaml --output-template-file serverless-output.yaml  --s3-bucket testapp-fewtfvdy-lambda-deployments

SAM Template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  ProfileFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: testapp.Profile.NetCoreVS::testapp.Profile.NetCoreVS.LambdaEntryPoint::FunctionHandlerAsync
      Runtime: dotnetcore2.0
      MemorySize : 128
      Timeout : 5
      CodeUri: bin/Release/netcoreapp2.0/publish
      Events:
        ProfileAny:
          Type: Api
          Properties:
            RestApiId: !Ref ProfileApiGateway
            Path: /profile/v1
            Method: GET

  ProfileApiGateway:
    DependsOn: ProfileFunction
    Type: 'AWS::Serverless::Api'
    Properties:
      StageName: Prod
      DefinitionUri: './swagger.yaml'

swagger.yaml:

---
swagger: '2.0'
info:
  version: v1
  title: ProfileAPI
paths:
  "/profile/v1":
    get:
      tags:
      - Values
      operationId: ProfileV1Get
      consumes: []
      produces:
      - text/plain
      - application/json
      - text/json
      parameters: []
      responses:
        '200':
          description: Success
          schema:
            type: array
            items:
              type: string
definitions: {}

.net core method:

[Route("profile/v1")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2","value_new3" };
        }


    }
jhurtas
  • 555
  • 1
  • 6
  • 17

1 Answers1

15

Your swagger definition is missing x-amazon-apigateway-integration.

This should provide that integration layer for you:

---
swagger: '2.0'
info:
  version: v1
  title: ProfileAPI
paths:
  "/profile/v1":
    get:
      tags:
      - Values
      operationId: ProfileV1Get
      consumes: []
      produces:
      - text/plain
      - application/json
      - text/json
      parameters: []
      responses:
        '200':
          description: Success
          schema:
            type: array
            items:
              type: string
      x-amazon-apigateway-integration:
        httpMethod: post
        type: aws_proxy
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ProfileFunction.Arn}/invocations
definitions: {}

Note that the httpMethod for x-amazon-apigateway-integration is always POST, since API Gateway always makes POST requests to Lambda regardless of what method your API route is using.

Tom
  • 1,660
  • 8
  • 16
  • I tried this and it resulted in this error: Unable to parse swagger document because of a malformed integration at path /profile/v1. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException – jhurtas Jun 19 '18 at 03:35
  • `ProfileFunction.Arn` should match the function name you've defined in the `Resources` section of your SAM template. – Jordan Ryan Moore Sep 21 '18 at 16:47
  • 1
    I solved the 'unable to parse swagger' by using DefinitionBody instead of DefinitionUri in the AWS::Serverless::Api definition. See [discussion](https://github.com/awslabs/serverless-application-model/issues/5#issuecomment-277046512) and [example](https://github.com/awslabs/serverless-application-model/tree/master/examples/2016-10-31/api_swagger_cors). – Fred Liporace Dec 31 '18 at 15:53
  • Is it possible to define x-amazon-apigateway-integration.uri dynamically? How I can match it with function from SAM template? – Mr Jedi Jul 24 '20 at 11:45
  • I tried this , i get this permissions error. ``` Execution failed due to configuration error: Invalid permissions on Lambda function ``` Any suggestions on this please. – Karthik Vadla Aug 04 '20 at 17:16