serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

Serverless offline is not using apiKeys specified in serverless.yml

Open adithepnarula opened this issue 5 years ago • 4 comments

Bug Report

If i start the lambda with sls offline, I am unable to make a post request to endpoint /api/test. I am getting an unauthorized 403 error. However, I can successfully make a post request if run sls offline --apiKey my-api-key. Is this the intended behavior? I would assume that sls offline would read and register the apiKeys provided in the serverless.yml file and that I wouldn't have to run sls offline --apiKey my-api-key.

service: test

provider:
  name: aws
  runtime: python3.7
  apikeys:
      - value: my-api-key

functions:
  hello:
    handler: handler.main
    events:
      - http:
          method: post
          path: /api/test
          private: true

plugins:
  - serverless-python-requirements
  - serverless-offline

custom:
  pythonRequirements:
    dockerizePip: non-linux
    dockerSsh: true
    useStaticCache: false
  serverless-offline:
    httpPort: 4001

adithepnarula avatar Apr 22 '20 20:04 adithepnarula

facing same issue

parimalyeole1 avatar May 15 '20 11:05 parimalyeole1

I'm also seeing this, would appreciate an update on intended behavior.

KidA001 avatar May 15 '20 21:05 KidA001

Also this behavior is not respected. No matter what, it generates an apiKey randomly and I have to use that one on a private flagged http function

https://www.serverless.com/framework/docs/providers/aws/events/apigateway#setting-api-keys-for-your-rest-api

  # api keys (values must be at least 20chars long)
  apiGateway:
    apiKeys:
      - name: ${self:service.name}-${opt:stage, 'dev'}-apiKey
        value: ${ssm:/aws/reference/secretsmanager/myapp/dev/apiKey} 

bitsofinfo avatar Dec 24 '21 18:12 bitsofinfo

This may not help everyone, but you can explicitly set an apiKey to be used by serverless offline.

custom:
  serverless-offline:
    apiKey: ${env:API_KEY}

provider:
  name: aws
  apiGateway:
    apiKeys:
      - ${env:API_KEY}

jasonlttl avatar Jan 10 '22 19:01 jasonlttl

I want to tackle this issue next. it looks like the flag (option) for setting an apiKey is really not needed, as it can be done with the serverless config itself (as your example is outlining above). it is possible that the apikeys key/value pair in the provider.apiGateway section was not introduced yet in serverless when support was implemented in serverless-offline.

dnalborczyk avatar Sep 12 '22 14:09 dnalborczyk