[英]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 中是否有一个可用的变量包含
1a234bc5de
或1a234bc5de.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.