serverless-fuck-you-4kb
v0.0.12
Published
Bring environements variables to lambda and yml
Downloads
36
Maintainers
Readme
Serverless Fuck You 4KB
Allow you to inject every environements variables as you want.
Note :
I will make an example and fix my grammar soon
Note 2 :
DO NOT USE serverless-export-env
with this library. It will replace your .env
and you don't want that!
Command
At anytime, you can check your configuration with sls print --stage dev
or beta or prod.
Installation
On your Serverless Framework project
npm install --save serverless-fuck-you-4kb
Setup
Step 1 : Creating your own config folder
Create a folder like ./config
at the root of your SLS project.
Then, create a javascript file, something like env.js
.
Now we have a folder and a js file at : ./config/env.js
Step 2 : Create an .env
file
At the root of your project, create an .env
file.
You'll write all of your variables here!
Step 3 : Migrate or create your environement variables
Note that you can't have other variable references inside, like ${self:....blablabla}
.
But, you can compute them later with serverless-fuck-you-4kb
.
For example, you have a s3_bucket_name
variable inside of your provider.environement
part of your serverless.yml
that is "computed" like this s3_bucket_name: bucket-${self:provider.environment.application}
.
Another variable is computed with a string to create your variable. Just keep the bucket
part and write into your .env
file : s3_bucket_name=bucket
.
Step 4 : Writing your own importer
Back to ./config/env.js
, write this code :
module.exports = require("serverless-fuck-you-4kb")({
log: true,
/*
This function is use twice!
- during serverless printing : before your serverless.yml is computed
- during the injection : at the top of your function
*/
map: (key, value, environment) => {
if (key == "application") {
return value;
}
if (key == "region") {
return value;
}
if (key == "stage") {
return value;
}
// We only want other vars to be affected with "application" var
return value + "-" + environment.application;
}
});
Your .env
file look like this :
s3_bucket_name=bucket
Your serverless.yml
look like this :
service: serverless-application
provider:
name: aws
runtime: nodejs6.10
stage: ${opt:stage, 'prod'}
region: us-east-1
memorySize: 1536
timeout: 300
environment:
region: ${self:provider.region}
stage: ${self:provider.stage}
application: ${self:service}-${self:provider.region}-${self:provider.stage}
# ... other properties like functions and resoures...
Step 5 : Inject environement variables inside of your functions
Inside of your function file, at the top of it, add :
const config = require("../config/env");
config.inject(); // this will inject your mapping
console.log(process.env.s3_bucket_name); // bucket-serverless-application-us-east-1-dev
Step 6 : Overcome
You're done.
How can you get those variables to yml ?
Like this :
BucketName: ${file(./config/env.js):s3_bucket_name}
You're welcome.