vimmi
vimmi

Reputation: 407

aws serverless.yml file "A valid option to satisfy the declaration 'opt:stage' could not be found" error

Getting below warning when trying to run serverless.

Serverless Warning --------------------------------------------

A valid option to satisfy the declaration 'opt:stage' could not be found. Below is my serverless.yml file

# 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/**

Upvotes: 11

Views: 13311

Answers (2)

Rishikesh Darandale
Rishikesh Darandale

Reputation: 3310

I will suggest you to do below implementation

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'

Basically, if stage and region is not specified using command line, then use defaults. Otherwise the command line one will be used.

Upvotes: 0

vgaltes
vgaltes

Reputation: 1218

In the description of the testFunc you're using ${opt:stage}. If you use that directly you need to pass the --stage flag when you run the deploy command.

What you should do there is to use the ${self:provider.stage}, because there you will have the stage calculated.

Upvotes: 12

Related Questions