How to make APIs in API Gateway private using api keys in Serverless?

I have a Fastapi app that I have hosted on API Gateway using Serverless & gitlab CI/CD. I am trying to generate an API key and make all the endpoints private.

I am following the following links:

  1. https://www.serverless.com/framework/docs/providers/aws/events/apigateway/#setting-api-keys-for-your-rest-api
  2. Set up API keys using the API Gateway console - Amazon API Gateway

The current serverless.yml file I have:

provider:
  name: aws
  runtime: python3.7
  stage: ${opt:stage, 'dev'}
  region: ${env:AWS_DEFAULT_REGION}
  lambdaHashingVersion: 20201221
  endpointType: REGIONAL
  apiGateway:
    apiKeys:
      - ${self:provider.stage}-pricesServiceKey
    shouldStartNameWithService: true

functions:
  my-lambda-fn:
    handler: app.main.handler
    timeout: 30
    memorySize: 150
    events:
      - http: ANY /
          private: true
      - http: 'ANY /{proxy+}'
          private: true

When I do this, I get the following error

error

The indentation seems to be absolutely correct. I have rechecked it.

What is the mistake that I am doing?

Can you try this instead:

MyFunc:
  handler: .build/api/api.yourFunc
  memorySize: 150
  events:
    - http:
        path: /
        method: any
        private: true
1 Like

Hey, yes. I figured that out and solved the problem. Thanks for your time :slight_smile:

1 Like