Skip to content

Cannot find a way to configure environment variables dynamically based on stage #79

Open

Description

@colesiegel

I'd like to set up my environment variables based on the current stage.

I initially tried to do this by referencing ${opt:stage} directly in the serverless.ts file, but I found that the value is not evaluated until the file is finished it's execution. As a result, I couldn't use the value of ${opt:stage} to determine my environment variables.

I have also tried to compute the environment variables in a separate serverless.config.js file with a resolver function and import them following the steps here (https://www.serverless.com/framework/docs/providers/aws/guide/variables#exporting-a-function) but the problem is that I cannot set the aws lambda function environment to a variable reference like ${self:custom.config.env} as it expects an environment map of type AwsLambdaEnvironment rather than a string.

Suppose I have a simple requirement - on the local stage there should exist an environment variable called LOCAL_SNS_ENDPOINT set to localhost:1234 and in all other stages this environment variable should not be set. Is there a way to do that with serverless.ts? Due to the previously mentioned issues, I can't seem to dynamically include or exclude environment variables based on the stage.

Activity

  1. changed the title [-]Cannot find a way to set environment variables dynamically based on stage[/-] [+]Cannot find a way to configure environment variables dynamically based on stage[/+] on May 17, 2023
  2. colesiegel commented on May 17, 2023

    @colesiegel
    Author

    @fredericbarthelet any help much appreciated thank you

  3. nfritz2016 commented on Jun 1, 2023

    @nfritz2016

    I am also experiencing this issue and would like a solution

  4. colesiegel commented on Aug 15, 2023

    @colesiegel
    Author

    Completely useless feature, worse than a waste of time unless this is addresed

  5. urg commented on Sep 22, 2023

    @urg

    I believe the params section can be used to achieve something close (empty env var vs not set).

    {
    provider: {
      environment: {
         LOCAL_SNS_ENDPOINT: "${param:local_sns_endpoint}"
      }
    },
    params: {
      local: {
        local_sns_endpoint: 'localhost:1234'
      }
    }
    
  6. colesiegel commented on Sep 25, 2023

    @colesiegel
    Author

    @urg I don't see how that accomplishes what I asked for.. i.e. setting or leaving environment variable unset based on the stage. Where is the conditional logic in your example? What does putting the value in params accomplish?

  7. urg commented on Sep 26, 2023

    @urg

    @colesiegel - see https://www.serverless.com/framework/docs/guides/parameters - the param corresponding to the stage is automatically set.

    Admittedly for this example the LOCAL_SNS_ENDPOINT would be empty string instead of not set for any environment other then local, however for many use cases this would be acceptable.

    potentially something like the following might work to avoid setting at all (untested)

      environment: {
         (${param:local_sns_endpoint} && ...{LOCAL_SNS_ENDPOINT: "${param:local_sns_endpoint}"})
      }
    
  8. arcaela commented on Nov 27, 2023

    @arcaela

    @colesiegel You can use dotenv before load configuration, for example:

    // serverless.ts
    import dotenv from 'dotenv';
    dotenv.config()
    
    
    module.exports = {
     key: process.env.MY_KEY==='myValue' ? 12 : 34
    }
    

    sls deploy -c serverless.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions