npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-fuck-you-4kb

v0.0.12

Published

Bring environements variables to lambda and yml

Downloads

13

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.