Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SAM template/cloudformation No integration defined for method (Service: AmazonApiGateway

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


    }
like image 943
jhurtas Avatar asked Jun 15 '18 22:06

jhurtas


People also ask

Which section of the AWS serverless application model template is required and identifies an AWS CloudFormation template file as an AWS Sam template file?

The declaration Transform: AWS::Serverless-2016-10-31 is required for AWS SAM template files. This declaration identifies an AWS CloudFormation template file as an AWS SAM template file.


1 Answers

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.

like image 66
Tom Avatar answered Oct 13 '22 20:10

Tom