【问题标题】:aws serverless.yml file "A valid option to satisfy the declaration 'opt:stage' could not be found" erroraws serverless.yml 文件“找不到满足声明‘opt:stage’的有效选项”错误
【发布时间】:2019-12-18 15:25:58
【问题描述】:

尝试运行无服务器时收到以下警告。

无服务器警告 --------------------------------------------

找不到满足声明“opt:stage”的有效选项。 下面是我的 serverless.yml 文件

# Serverless Config
service: api-service

# Provider
    provider:
      name: aws
      runtime: nodejs8.10
      region: ${opt:region, 'ap-east-1'}
      stage: ${opt:stage, 'dev'}
      # Enviroment Varibles
      environment:
        STAGE: ${self:custom.myStage}
        MONGO_DB_URI: ${file(./serverless.env.yml):${opt:stage}.MONGO_DB_URI}
        LAMBDA_ONLINE: ${file(./serverless.env.yml):${opt:stage}.LAMBDA_ONLINE}


    # Constants Varibles
    custom:
        # environments Variables used for convert string in upper case format
        environments:
        myStage: ${opt:stage, self:provider.stage}
        stages:
          - dev
          - qa
          - staging
          - production
        region:
          dev: 'ap-east-1'
          stage: 'ap-east-1'
          production: 'ap-east-1'

    # Function
    functions:
      testFunc:
        handler: index.handler
        description: ${opt:stage} API's
        events:
          - http:
              method: any
              path: /{proxy+}
              cors:
                origin: '*'

    #package
    package:
      exclude:
        - .env
        - node_modules/aws-sdk/**
        - node_modules/**

【问题讨论】:

    标签: jenkins aws-lambda serverless


    【解决方案1】:

    在 testFunc 的描述中,您使用的是 ${opt:stage}。如果直接使用它,则需要在运行 deploy 命令时传递 --stage 标志。

    你应该做的是使用${self:provider.stage},因为你将在那里计算阶段。

    【讨论】:

      【解决方案2】:

      我会建议你做下面的实现

      provider:
        name: aws
        runtime: nodejs8.10
        region: ${opt:region, self:custom.environments.region.${self:custom.environments.myStage}}
        stage: ${opt:stage, self:custom.environments.myStage}
        # Enviroment Varibles
        environment:
          STAGE: ${self:custom.myStage}
          MONGO_DB_URI: ${file(./serverless.env.yml):${self:provider.stage}.MONGO_DB_URI}
          LAMBDA_ONLINE: ${file(./serverless.env.yml):${self:provider.stage}.LAMBDA_ONLINE}
      
      
      # Constants Varibles
      custom:
        # environments Variables used for convert string in upper case format
        environments:
          # set the default stage if not specified
          myStage: dev
          stages:
            - dev
            - qa
            - staging
            - production
          region:
            dev: 'ap-east-1'
            stage: 'ap-east-1'
            production: 'ap-east-1'
      

      基本上,如果stage 和region 没有使用命令行指定,则使用默认值。否则将使用命令行一。

      【讨论】:

      • self:custom.environments 是一个 .env 文件?
      猜你喜欢
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-14
      相关资源
      最近更新 更多