Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

aws lambda - Serverless Framework Api Gateway TypeError

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?

question from:https://stackoverflow.com/questions/65908409/serverless-framework-api-gateway-typeerror

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...