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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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
ProfileFunction.Arn should match the function name you've defined in the Resources section of your SAM template.
I solved the 'unable to parse swagger' by using DefinitionBody instead of DefinitionUri in the AWS::Serverless::Api definition. See discussion and example.
Is it possible to define x-amazon-apigateway-integration.uri dynamically? How I can match it with function from SAM template?
I tried this , i get this permissions error. ``` Execution failed due to configuration error: Invalid permissions on Lambda function ``` Any suggestions on this please.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.