无服务器 CLI --stage 参数不会传递到“provider.stage”

发布于 2025-01-11 07:59:29 字数 1475 浏览 0 评论 0原文

我有以下 serverless.yml 文件内容,

custom:
  alarms:
    - functionErrors
    - functionThrottles
  myStage: ${opt:stage, self:provider.stage}
  daas-aa-maturity-model-env-stage: !Join
    - '-'
    - - ${self:service}
      - ${self:custom.myStage}
      - ${self:provider.region}
  scripts:
    hooks:
      'deploy:finalize': serverless invoke -f copyGlueScripts
provider:
  name: aws
  lambdaHashingVersion: "20201221"
  managedPolicyArns:
    - 'arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole'
    - ${self:provider.environment.GLUE_JOB_POLICY_ARN}
    ...

当我部署时,

serverless deploy --aws-profile "${PROFILE_NAME}" --stage nonprod && \
sleep 10 && \
sls s3deploy --profile "${PROFILE_NAME}" --stage nonprod

它会生成一个文件 .serverless/serverless-state.json ,该文件具有以下值

    "provider": {
      ...
      "stage": "dev",

,在使用“dev”调用 lambda 时会出现错误以它的名义。在我的例子中,“阶段”不应该是“非产品”吗?

如果我在 serverless.yml 中执行类似操作

provider:
  stage: ${opt:stage}

,则会出现以下错误:

  ServerlessError: Cannot resolve serverless.yml: Variables resolution errored with:
    - Cannot resolve variable at "provider.stage": Value not found at "opt" source

解决方法是在提供程序中专门指定 nonprod:

provider:
  stage: nonprod
  name: aws

但我们经常将服务部署到不同的环境,例如 dev、nonprod 或 prod。我们希望这是自动化的。

有人可以帮忙吗?谢谢!

I have the following serverless.yml file content

custom:
  alarms:
    - functionErrors
    - functionThrottles
  myStage: ${opt:stage, self:provider.stage}
  daas-aa-maturity-model-env-stage: !Join
    - '-'
    - - ${self:service}
      - ${self:custom.myStage}
      - ${self:provider.region}
  scripts:
    hooks:
      'deploy:finalize': serverless invoke -f copyGlueScripts
provider:
  name: aws
  lambdaHashingVersion: "20201221"
  managedPolicyArns:
    - 'arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole'
    - ${self:provider.environment.GLUE_JOB_POLICY_ARN}
    ...

when I deploy using

serverless deploy --aws-profile "${PROFILE_NAME}" --stage nonprod && \
sleep 10 && \
sls s3deploy --profile "${PROFILE_NAME}" --stage nonprod

it generates a file .serverless/serverless-state.json which has the following values

    "provider": {
      ...
      "stage": "dev",

which runs into error when invoking a lambda with a "dev" in its name. Isn't that the "stage" supposed to be "nonprod" in my case?

If I do something like

provider:
  stage: ${opt:stage}

in serverless.yml, it gets the following error:

  ServerlessError: Cannot resolve serverless.yml: Variables resolution errored with:
    - Cannot resolve variable at "provider.stage": Value not found at "opt" source

The workaround is to specify nonprod specifically in provider:

provider:
  stage: nonprod
  name: aws

But we frequently deploy the service to different env such as dev, nonprod or prod. We would like this to be automated.

Can anyone help? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2025-01-18 07:59:29

您可以执行类似

service: your service

custom:
  alarms:
    nonprod: alarm1
    dev: alarm2

provider:
    name: aws
    runtime: nodejs8.10
    stage: ${opt:stage, 'dev'}

functions: 
    something:
    handler: handler.scheduledUpdater
    
    someoperation: ${self:provider.stage}

sls deploy --stage nonprod 的操作,否则它将默认为 dev
${opt:stage, 'dev'} 获取从命令行 --stage 选项传递的值。在这种情况下,产品。如果没有传递任何选项,则将 dev 作为默认值

You can do something like this

service: your service

custom:
  alarms:
    nonprod: alarm1
    dev: alarm2

provider:
    name: aws
    runtime: nodejs8.10
    stage: ${opt:stage, 'dev'}

functions: 
    something:
    handler: handler.scheduledUpdater
    
    someoperation: ${self:provider.stage}

sls deploy --stage nonprod otherwise it will default to dev
${opt:stage, 'dev'} takes the value passed from command line --stage option. In this case prod. If no option is passed dev is taken as default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文