gpt4 book ai didi

How to create different stages or environments in single serverless.yml file using serverless framework?(如何使用无服务器框架在单个无服务器.yml文件中创建不同的阶段或环境?)

转载 作者:bug小助手 更新时间:2023-10-24 17:54:38 27 4
gpt4 key购买 nike



I am new to using serverless framework ,I wanted to create three different environments dev,Qa,prod. How do i create in single serverless.yml file ? The provider is AWS.

我是使用无服务器框架的新手,我想创建三个不同的环境dev、qa、prod。如何在单个serverless.yml文件中创建?供应商是AWS。


更多回答
优秀答案推荐

You can pass a stage CLI option that can be interpolated wherever needed in your serverless.yml file. For example, take the following CLI command:

您可以传递Stage CLI选项,该选项可以根据需要在serverless.yml文件中的任何位置进行插补。例如,使用以下CLI命令:


serverless --stage dev deploy

This can be accessed in serverless.yml with ${opt:stage}. I usually include this under provider:

可以使用${opt:Stage}在serverless.yml中访问它。我通常会将以下内容包括在提供程序下:


provider:
stage: ${opt:stage}

Then you can get the value of the stage option anywhere in serverless.yml using ${self:provider.stage}.

然后,您可以在serverless.yml中的任何位置使用${self:Provider.age}获取Stage选项的值。


When lambdas are deployed, their ARNs are automatically constructed with prefixes of the service name (as defined by the service key in serverless.yml), the stage, and the lambda name, like the following:

在部署lambdas时,它们的ARN会自动使用服务名称(由serverless.yml中的服务键定义)、阶段和lambda名称的前缀构建,如下所示:


arn:aws:lambda:us-east-1:010101010101:function:myservice-dev-mylambdaname

So you can simply run the deploy CLI command for the other two stages/environments you want, and you'll have a set of separate resources for each environment.

因此,您只需对所需的其他两个阶段/环境运行Deploy CLI命令,即可为每个环境提供一组单独的资源。



While @223seneca's answer is correct, the recommended option is to use ${sls:stage} instead. This is shorthand for ${opt:stage, self:provider.stage, "dev"}, as described in the offical doco at https://www.serverless.com/framework/docs/providers/aws/guide/variables/#referencing-serverless-core-variables
This allows for cases where you use the provider block to always have a specific stage, as well as supporting normal usage of passing in the stage.

虽然@223seneca的答案是正确的,但推荐的选项是使用${sls:Stage}。这是${opt:Stage,Self:Provider.Stage,“dev”}的简写,如https://www.serverless.com/framework/docs/providers/aws/guide/variables/#referencing-serverless-core-variables的官方文档中所述。这允许您在使用提供程序块始终具有特定阶段的情况下,以及支持在阶段中传递的正常使用。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com