Skip to content

Allow to run functions with env variables correctly populated#27

Open
marcosnils wants to merge 1 commit into
Nopik:masterfrom
marcosnils:master
Open

Allow to run functions with env variables correctly populated#27
marcosnils wants to merge 1 commit into
Nopik:masterfrom
marcosnils:master

Conversation

@marcosnils

@marcosnils marcosnils commented Apr 20, 2016

Copy link
Copy Markdown

My lambda functions use process.env to read variables configured from the environment. They currently crash because functions are not populating this variables correctly.

This PR calls the Runtime.run function which properly initializes env variables for the specific function.

note: if multiple functions use the same env variables and functions are executed concurrently, unknown errors may happen as process.env will be modified nondeterministically

Comment thread index.js
return functions.forEach(function(fun) {
if( -1 !== fun.getRuntime().getName().indexOf('nodejs') ) {
// Override s-function.json defined environment!
Object.getPrototypeOf( fun.getRuntime() ).getEnvVars = ()=> BbPromise.resolve( {} );

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be made optional, but not removed, because it's really useful to pass local config (different form AWS) via env and not have it overriden by SLS Function.run, e.g. https://github.com/serverless/serverless/blob/master/lib/RuntimeNode.js#L48

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tszajna0 I see what you mean but shouldn't the serve plugin work the same was as serverless core does?. In serverless core you can't override the variables defined in your s-function.json file. You need to properly configure your stage variables to make them work.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcosnils let me put it opposite way: I wish I could easily specify my own env when locally running functions in SLS, unfortunately I have to properly configure stage variables in a different way than I'd deploy to AWS.

IMO it's useful to mimic sls behavior as close as possible and it's also useful to be able to alter it slightly. Then having a switch between these two would make everyone happy, I suppose. If you agree, then the question is shall I do it or would you?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tszajna0 I'll do it. No worries :)

@tszajna0

tszajna0 commented Apr 21, 2016

Copy link
Copy Markdown
Collaborator

@marcosnils populating env from s-function.json can be made optional, but it'll be painful to make it mandatory; see my comments inline.

note: you're right on different env for each function; AFAIR it wasn't possible with older sls. IMO it's not a piece of cake to have this supported in serverless-serve under current design, anyway you may try if you'd like to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants