无服务器框架 Api 网关类型错误

Serverless Framework Api Gateway TypeError

我有一个无服务器模板,其中包含带有 API 网关的 lambda 函数。我在模板中包含了一个 api 键。一切部署都很好,但最后我仍然收到以下类型错误:

TypeError: Cannot read property 'apiKeys' of undefined

我真的找不到解决这个问题的好方法。这是我的无服务器模板示例,其中包含 apikeys:

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

我在这里错过了什么?

我猜您混淆了使用和不使用 serverless-add-api-key 插件配置 api 键的方式。以下是不使用插件的工作 serverless.yml。

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 部署输出低于 -

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

如果您仍想使用该插件,则需要获得正确的语法。您可以使用下面的 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