【问题标题】:How to pass api endpoint parameter to lambda function in serverless如何在无服务器中将 api 端点参数传递给 lambda 函数
【发布时间】:2021-12-20 20:07:40
【问题描述】:

我在 API 网关后面创建了一个 lambda 函数,并且我也在尝试为它实现健康检查,但现在我需要分两步完成:

  1. 运行serverless deploy,这样它就会为api网关输出端点。
  2. 手动将端点插入到 healthcheck.environment.api_endpoint 参数中,然后再次运行serverless deploy

functions:
  app:
    handler: wsgi_handler.handler
    events:
      - http:
          method: ANY
          path: /
      - http:
          method: ANY
          path: '{proxy+}'

  healthcheck:
    environment:
      api_endpoint: '<how to reference the api endpoint?>'
    handler: healthcheck.handler
    events:
        - schedule: rate(1 minute)

custom:
  wsgi:
    app: app.app
    pythonBin: python3
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux

有没有办法在创建时获取对 api 网关的引用,以便可以将它作为环境变量传递给 healthcheck 应用程序?我能想到的替代方案基本上是创建一个特定的 serverless.yaml 仅用于健康检查目的。

【问题讨论】:

    标签: aws-lambda aws-api-gateway serverless-framework wsgi


    【解决方案1】:

    我注意到我可以在 lambda 中重建端点,并像这样获取 id:

      healthcheck:
        environment:
          api_endpoint: !Ref ApiGatewayRestApi
          region: ${env:region}
        handler: healthcheck.handler
        events:
            - schedule: rate(1 minute)
    

    然后重构:

    def handler(event, context):
        api_id = os.environ['api_endpoint']
        region = os.environ['region']
        endpoint = f"https://{api_id}.execute-api.{region}.amazonaws.com/dev"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-26
      • 2017-06-17
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多