0

I have a serverless template which includes a lambda function with API gateway. I've included an api key to the template. Everything deploys fine, BUT I still receive the following type error at the end:

TypeError: Cannot read property 'apiKeys' of undefined

I can't really find any good solution to this problem. Here is a sample of my serverless template where I include the apikeys:

plugins:
  - serverless-add-api-key
provider:
  name: aws
  timeout: 30 # Because API Gateway
  runtime: python3.8
  region: us-east-1
  apiGateway:
    apiKeys:
      - MyKey

What am I missing here?

1 Answer 1

2
+50

I guess you are mixing up the ways in which we configure api keys with and without the serverless-add-api-key plugin. Following is a working serverless.yml without using the plugin.

service: serverless-api-key

provider:
  name: aws
  stage: prod
  timeout: 30
  runtime: nodejs12.x
  apiGateway:
    apiKeys:
      - MyKey
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: get

sls deploy output below -

Service Information
service: serverless-api-key
stage: prod
region: ap-south-1
stack: serverless-api-key-prod
resources: 14
api keys:
  MyKey: Aeeblv4djg2AmlXXXXXXXXXXXXXX2fk9l4iCDRf
endpoints:
  GET - https://xxxxxxxx.execute-api.ap-south-1.amazonaws.com/prod/hello
functions:
  hello: serverless-api-key-prod-hello
layers:
  None

If you still want to use the plugin, you need to get the correct syntax. You can use the below serverless.yml -

service: serverless-api-key-2

plugins:
  - serverless-add-api-key
custom:
  apiKeys:
    - name: MyKey33

provider:
  name: aws
  stage: prod
  region: ap-south-1
  timeout: 30
  runtime: nodejs12.x
functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: get
Sign up to request clarification or add additional context in comments.

1 Comment

That did it! Thanks for your assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.