Coder Social home page Coder Social logo

serverless-stage-env-vars's Introduction

Serverless Stage Environment Variables Plugin

serverless npm package Build Status Coverage Status Downloads

A Serverless plugin to easily use stage-specific environment variables.

Installation

npm install --save-dev serverless-stage-env-vars

Add the plugin to serverless.yml:

plugins:
  - serverless-stage-env-vars

Note: Node 10.x or higher runtime required.

Usage

Inside your Serverless config, include this plugin and define a custom.stageEnvVars object and specify the default environment and a path the environment configuration file. This configuration is required.

plugins:
  - serverless-stage-env-vars
  ...

custom:
  stageEnvVars:
    defaultEnv: dev # The name of the stage config to use if no matching stage is found
    environmentFile: path/relative/to/cwd/config.js

Next, define your environment config file (e.g. path/relative/to/cwd/config.js):

// This config file is designed to contain configuration that is needed by
// APIs and websites. This config is global in nature.
const BASE_DOMAIN_NAME = 'foo.digio.com.au';

const devConfig = {
  AWS_ACCOUNT_ID: 'devAccountId',
  WEB_DOMAIN: `plugin.${BASE_DOMAIN_NAME}`,
  CERTIFICATE_COMMON_NAME: `*.${BASE_DOMAIN_NAME}`,
  TRACING_ENABLED: false,
};

// Export a configuration object which has top-level properties which match
// the Serverless stage names you use.
// If a matching stage name is not found, the Serverless custom.stageEnvVars.defaultEnv config
// will be used (which is 'dev' in this example)/
module.exports = {
  dev: { ...devConfig },
  prod: {
    ...devConfig,
    AWS_ACCOUNT_ID: 'prodAccountId',
    TRACING_ENABLED: true,
  },
};

Finally, you can refer to stage environment variables in your Servlerless config file using the ${stageEnv:VARIABLE_NAME} syntax:

provider:
  environment:
    XRAY_ENABLED: '${stageEnv:TRACING_ENABLED}'
    AWS_ACCOUNT_ID: '${stageEnv:AWS_ACCOUNT_ID}'
    ...
  tracing:
    apiGateway: '${stageEnv:TRACING_ENABLED}',
    lambda: '${stageEnv:TRACING_ENABLED}',

The plugin will replace those variables with the stage-specific values from your environment-config file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.