如何在 Serverless Framework 的 `serverless.yml` 文件中获取 URL 端点详细信息作为变量?

编程入门 行业动态 更新时间:2024-10-27 06:23:44
本文介绍了如何在 Serverless Framework 的 `serverless.yml` 文件中获取 URL 端点详细信息作为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

Using Serverless Framework to deploy AWS Lambda functions, Serverless creates (or receives) the specific 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 variables does not seem to answer that case.

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

Details: my serverless.yml contains a provider: specification similar 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/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/staging/round/control.

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

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? (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 I need in another place in the serverless.yml file.

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

N.B. That 1a234bc5de isn't a dynamically generated random string - my current project is (per stage, per region) 'fixed' to the same string. Perhaps that string is generated at AWS Lambda or AWS 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/'
            - ${opt:stage, self:provider.stage}

感谢 goingserverless.

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

更多推荐

[db:关键词]

本文发布于:2023-04-23 04:22:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:详细信息   变量   文件   如何在   Serverless

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!