fbpx

Serverless allows specifying different stages to deploy projects to. Changing the stage will change the environment the function is running on, which is helpful when wishing to keep production code partitioned from the development environment.

The function’s stage is set to 'dev' by default. Update the stage when deploying the function, either from the command line using the serverless framework or by modifying the serverless.yml in the project.

When utilizing this feature, remember to include a config file that holds the environment IDs associated with your stages. An example config.json would look something like this:

{
    "dev": "env-abcd1234",
    "prod": "env-defg5678" 
}

Through Serverless Framework

To change the stage through the serverless framework, simply enter the command:

serverless deploy --stage #{Your Stage Name} 

Also, update the environment parameter to point to the config.json:

spotinst:
     environment: ${file(./config.json):${opt:stage, self:provider.stage, 'dev'}}

Note that while the example is using 'dev' as the default stage, you may change this parameter to a custom default stage.

Through the .yml File

To change the stage in the serverless.yml file, add the following into the provider tag  and then deploy the function as usual

provider:
  name: spotinst
  stage: #{Your Stage Name}  
  spotinst:
    environment: #{Your Environment ID}

Be sure to also modify the environment ID when changing the stage if not working with a config file.