Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call .env file from YAML?

I want to hide my secret credential from my yaml, i need to use .env, so how to call .env file from my yaml, so that every I call this YAML, YAML will automatic call .env file. Please help me. thx

like image 947
Johannes Sitorus Avatar asked Nov 02 '25 01:11

Johannes Sitorus


1 Answers

Instead of using an .env file, which is a simple properties file if you're following dotenv package, you can do the following:

  1. create additional .yml file, for example .secrets.yml. you can store the secrets per stage:
prod:
  MY_SECRET: foo
dev:
  MY_SECRET: bar
  1. store your secrets/configurations there

Then in serverless.yml:

  1. load this file into an object:
custom:
  secrets: ${file(.secrets.yml):${self:provider.stage}}
  1. load object fields as environment variables:
provider:
  environment:
    MY_SECRET: ${self:custom.secrets.MY_SECRET}

How to test locally

In your tests you can load the secrets file this way:

const yaml = require('js-yaml');
const fs = require('fs');
const _ = require('lodash');

module.exports.loadSecrets = function (env = 'dev', path = './.secrets.yml') {
    const secrets = yaml.load(fs.readFileSync(path));
    _.forEach(secrets[env], (value, key) => {
        process.env[key] = value;
    });
}

Reference: http://www.goingserverless.com/blog/using-environment-variables-with-the-serverless-framework

like image 85
Benny Bauer Avatar answered Nov 03 '25 23:11

Benny Bauer