完成
serverless deploy
后,如何设置将无服务器应用程序部署到变量的 URL。这对我很有用,因为我可以在将来使用这个 URL 来传递到我的 Javascript 网站。
对于那些需要这个的人,我最终做了以下事情:
URL="$(serverless info --verbose | grep ServiceEndpoint | sed s/ServiceEndpoint\:\ //g)"
将变量 URL 设置为您的无服务器应用程序端点。
serverless-build-client
的插件,可能会有所帮助。在我的项目中,我的客户端是它自己的无服务器框架项目,并且在环境变量部分中,我交叉引用来自另一个堆栈的端点
provider:
environment:
REACT_APP_ENDPOINT: ${cf:my-backend.ServiceEndpoint}
此插件将使用
serverless.yml
文件集中的环境变量构建您的客户端。该插件的重点是这个
const environment = this.serverless.service.provider.environment;
Object.keys(environment).forEach(variable => {
process.env[variable] = environment[variable];
});
// later
spawn("yarn", ["build"]);
serverless-stack-output
的插件,它将所有 serverless.yml
输出写入 json 文件。其中一个输出是 ServiceEndpoint
,我编写了一个自定义脚本来从 json 文件中获取该值,并在构建之前设置环境变量
就我而言,我不仅需要服务端点,还需要完整的 URL,包括部署到我的堆栈的每个集成 API 网关的 Lambda 函数的路径。我已经在here写下了完整的解决方案并附有解释,但总而言之:
functions:
my-shareable-func:
name: my_shareable_func
architecture: arm64
handler: path/to/my_shareable_func/index.handler
memorySize: 128
runtime: nodejs14.x
events:
- http:
path: /share-me
method: get
cors: true
resources:
Outputs:
ShareableFuncEndpoint:
Description: URL for the my-shareable-func API endpoint
Export:
Name: ShareableFuncEndpoint
Value:
Fn::Join:
- ""
- - "https://"
- Ref: ApiGatewayRestApi
- ".execute-api.${self:provider.region}.amazonaws.com"
- "/${self:provider.stage}"
- "/share-me" # this should arguably be a custom variable
my_shareable_func_endpoint=$(
aws cloudformation describe-stacks \
--stack-name "my-service-name" \
--query 'Stacks[*].Outputs[?OutputKey==`ShareableFuncEndpoint`].OutputValue' \
--no-paginate \
--no-cli-pager \
--output text
)
echo "Shareable function endpoint: $my_shareable_func_endpoint"