本文介绍了如何在 Serverless Framework 的 `serverless.yml` 文件中获取 URL 端点详细信息作为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用无服务器框架部署 AWS Lambda 函数,无服务器创建(或接收)特定的 URL 端点字符串.我想使用该字符串(作为变量)serverless.yml 规范文件的另一部分.

Using Serverless Framework todeploy AWS Lambda functions, Serverless creates (or receives) thespecific URL endpoint string. I want to use that string (as a variable)in another section of the serverless.yml specification file.

该 URL 端点是否可用作 serverless.yml 中的变量?关于 AWS 相关变量的无服务器框架文档似乎没有回答这种情况.

Is that URL endpoint available as a variable in serverless.yml?The Serverless Framework documentation on AWS-related variablesdoes not seem to answer that case.

详细信息:我的 serverless.yml 包含一个 provider: 规范类似于:

Details: my serverless.yml contains a provider: specificationsimilar to:

provider:
  name: aws
  runtime: python3.6
  memorySize: 512
  region: ${opt:region, 'eu-west-1'}
  profile: ${opt:profile, 'default'}
  stage: ${opt:stage, 'staging'}

和一个 functions: 部分以:

functions:
  round-control:
    name: ${self:provider.stage}-round-control
    runtime: nodejs8.10
    handler: round/control/app.lambdaHandler
    events:
      - http:
          path: round/control
          method: get

之后

无服务器部署 --profile [my-aws-profile]

Lambda 函数sample-experiments-staging-round-control据报告在端点可用https://1a234bc5de.execute-api.eu-west-1.amazonaws.com/staging/round/control.

the Lambda function sample-experiments-staging-round-controlis reported to be available at endpointhttps://1a234bc5de.execute-api.eu-west-1.amazonaws.com/staging/round/control.

问题:Serverless 中是否有可用的变量包含1a234bc5de1a234bc5de.execute-api 甚至1a234bc5de.execute-api.eu-west-1.amazonaws.com?(显然,如果我知道第一个,我也可以构造后两个.)

Question: is there a variable in Serverless available that containsthat 1a234bc5de, or 1a234bc5de.execute-api or perhaps even 1a234bc5de.execute-api.eu-west-1.amazonaws.com?(Obviously, I can also construct the last two if I know the first.)

使用该变量,我可以构建完整的 URL 端点,需要在 serverless.yml 文件中的另一个位置.

With that variable, I can construct the full URL endpoint, which Ineed in another place in the serverless.yml file.

注意1a234bc5de 不是动态生成的随机数字符串 - 我当前的项目(每个阶段,每个区域)固定"为相同的字符串.也许该字符串是在 AWS Lambda 或AWS API 网关?

N.B. That 1a234bc5de isn't a dynamically generated randomstring - my current project is (per stage, per region) 'fixed' tothe same string. Perhaps that string is generated at AWS Lambda orAWS API Gateway?

推荐答案

我能够将 API 网关端点的 URL 和唯一 ID 作为环境变量传递给 Lambda 函数,如下所示:

I was able to pass the URL and unique ID for the API Gateway endpoint to a Lambda function as environment variables as follows:

  mylambda:
    handler: mylambda.handler
    runtime: python3.7
    events:
    - http:
        path: id
        cors: true
    environment:
      APIG_UID: !Ref "ApiGatewayRestApi"
      APIG_URL:
        !Join
          - ''
          - - 'https://'
            - !Ref ApiGatewayRestApi
            - '.execute-api.'
            - ${opt:region, self:provider.region}
            - '.amazonaws.com/'
            - ${opt:stage, self:provider.stage}

感谢 goingserverless.

这篇关于如何在 Serverless Framework 的 `serverless.yml` 文件中获取 URL 端点详细信息作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:13