Pablo
Pablo

Reputation: 2154

REST API openapi spec AWS API Gateway integration with the serverless.com framework

I'm trialing the serverless.com with AWS as the provider. I'd like to do a simple hello world app with API Gateway and Lambda, where I publish the API from an openapi spec.

I have seen in this forum post that I need to declare the spec as a resource in the serverless.yml file, but when doing so I get the following error when doing sls deploy:

I think I have mostly correct, except that in the openapi spec file I don't know how to correctly refer to the URI for the API I am creating. Is that the reason I am getting the error?

  Serverless Error ---------------------------------------

  An error occurred: ApiGatewayRestApi - Errors found during import:
    Unable to put integration on 'GET' for resource at path '/hello': Invalid function ARN or invalid uri (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: ...).

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              12.16.2
     Framework Version:         1.67.3
     Plugin Version:            3.6.6
     SDK Version:               2.3.0
     Components Version:        2.29.3

I am trying the simplest example I can think of:

# serverless.yml
service: accounts-api

provider:
  name: aws
  apiName: accounts
  runtime: nodejs12.x
  stage: dev
  region: ap-southeast-2

functions:
  hello:
    handler: functions/handler.hello
    events:
      - http:
           path: /accounts
           method: GET

# The resources attribute will be sent directly to CloudFormation in raw format
resources:
    Resources:
      ApiGatewayRestApi:
        Type: 'AWS::ApiGateway::RestApi'
        Properties:
          Name: ${self:provider.apiName}-${self:provider.stage}
          Body:
            ${file(specs/simple-spec.yml)}
      ApiGatewayDeployment:
        Type: AWS::ApiGateway::Deployment
        Properties:
          RestApiId:
            Ref: ApiGatewayRestApi
          StageName: ${self:provider.stage}

and the openapi spec:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Hello API
  description: Returns a hello world message

paths:
  /hello:
    get:
      description: Returns a hello world message              
      responses:
        '200':
          description: Successful response
      security:
      - api_key: []
      x-amazon-apigateway-auth:
        type: none          
      x-amazon-apigateway-integration:
         x-amazon-apigateway-integration:
         type: aws_proxy
         uri: arn:aws:apigateway:ap-southeast-2:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-southeast-2:idAccount:function:hello/invocations
         httpMethod: GET
         passthroughBehavior: when_no_templates
         payloadFormatVersion: 1.0

Upvotes: 1

Views: 1305

Answers (1)

Alex Lungu
Alex Lungu

Reputation: 1114

In your serverless.yml file you are creating the lambda in region: ap-southeast-2 but inside the openapi definition (uri: arn:aws:apigateway:ap-northeast-1:lambda...) you are referencing a different region: ap-northeast-1

Upvotes: 3

Related Questions