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
Instead of using an .env file, which is a simple properties file if you're following dotenv package, you can do the following:
.yml file, for example .secrets.yml. you can store the secrets per stage:prod:
  MY_SECRET: foo
dev:
  MY_SECRET: bar
Then in serverless.yml:
custom:
  secrets: ${file(.secrets.yml):${self:provider.stage}}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With