简体   繁体   English

如何在无服务器框架的“serverless.yml”文件中获取 URL 端点详细信息作为变量?

[英]How to get URL endpoint detail as variable in Serverless Framework's `serverless.yml` file?

Using Serverless Framework to deploy AWS Lambda functions, Serverless creates (or receives) the specific URL endpoint string.使用无服务器框架部署 AWS Lambda 函数,无服务器创建(或接收)特定的 URL 端点字符串。 I want to use that string (as a variable) in another section of the serverless.yml specification file.我想在serverless.yml规范文件的另一部分中使用该字符串(作为变量)。

Is that URL endpoint available as a variable in serverless.yml ? URL 端点是否可以作为serverless.yml中的变量使用? The Serverless Framework documentation on AWS-related variables does not seem to answer that case. 关于 AWS 相关变量的无服务器框架文档似乎没有回答这种情况。


Details: my serverless.yml contains a provider: specification similar to:详细信息:我的serverless.yml包含一个provider:规范,类似于:

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

and a functions: section starting with:和一个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

After a之后

serverless deploy --profile [my-aws-profile]

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

Question: is there a variable in Serverless available that contains that 1a234bc5de , or 1a234bc5de.execute-api or perhaps even 1a234bc5de.execute-api.eu-west-1.amazonaws.com ?问题:Serverless 中是否有一个可用的变量包含1a234bc5de1a234bc5de.execute-api甚至1a234bc5de.execute-api.eu-west-1.amazonaws.com (Obviously, I can also construct the last two if I know the first.) (显然,如果我知道第一个,我也可以构建最后两个。)

With that variable, I can construct the full URL endpoint, which I need in another place in the serverless.yml file.使用该变量,我可以构建完整的 URL 端点,我需要在serverless.yml文件的另一个位置。

NB That 1a234bc5de isn't a dynamically generated random string - my current project is (per stage, per region) 'fixed' to the same string.注意1a234bc5de不是动态生成的随机字符串 - 我当前的项目(每个阶段,每个区域)“固定”到同一个字符串。 Perhaps that string is generated at AWS Lambda or AWS API Gateway?也许该字符串是在 AWS Lambda 或 AWS API 网关上生成的?

I was able to pass the URL and unique ID for the API Gateway endpoint to a Lambda function as environment variables as follows: 我能够将API网关端点的URL和唯一ID作为环境变量传递给Lambda函数,如下所示:

  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}

Thanks to goingserverless . 感谢无遗址

Here is a simpler and more modern alternative:这是一个更简单、更现代的替代方案:

provider:
  environment:
    API_URL: !Sub 'https://${ApiGatewayRestApi}.execute-api.${aws:region}.amazonaws.com/${sls:stage}'

Note: ask asked in the question, this applies for REST API , not HTTP API .注意:在问题中询问,这适用于REST API ,而不是HTTP ZDB974238714CA8DE64FZACE33

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 serverless.yml 文件中获取 AccountId 作为变量? - How do I get the AccountId as a variable in a serverless.yml file? 如何访问Serverless Framework的serverless.yml中的插件输出? - How to access plugin output in serverless.yml of Serverless Framework? 获取无服务器框架在我的serverless.yml文件中创建的API网关资源的ARN - Get the ARN for the API gateway resource that the serverless framework creates within my serverless.yml file 拆分 serverless.yml 文件 - Split serverless.yml file 从jenkinsfile引用Serverless.yml文件中的环境变量 - Referencing Environment Variable in Serverless.yml File from jenkinsfile 无服务器框架,如何在部署到 AWS 之前获得最终的 serverless.yml 版本(加载所有变量后)? - Serverless Framework, how can I get the final serverless.yml version (after load all variables) before deploy to AWS? 如何在serverless.yml上设置S3对象的元数据 - How to set metadata of a S3 object on serverless.yml cloudFormation 模板验证错误:如何拆分 serverless.yml 文件 - cloudFormation Template Validation Error: how to split serverless.yml file 如何将条件放入 serverless.yml 文件中? - How can I put condition in serverless.yml file? 如何在 serverless.yml 文件中引用 lambda 名称? - How can I reference lambda name in serverless.yml file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM