Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make webpack aware of stage/region #86

Closed
Spaider opened this issue Jan 13, 2017 · 9 comments
Closed

Make webpack aware of stage/region #86

Spaider opened this issue Jan 13, 2017 · 9 comments

Comments

@Spaider
Copy link

Spaider commented Jan 13, 2017

I need a fresh look at my problem. I am using serverless 1.x and want to deploy to different regions with different stages (surprise, huh? :) ) Every stage/region has its own set of settings, DB connection options, to be precise.

Everything was more or less good before I decided to introduce webpack plugin: region/stage-specific options were stored in ${region}.${stage}.env files and I load them with dot-env.

It's not possible anymore when I started to use webpack plugin. Well, at least solution is avoiding me. The only way to achieve similar functionality is to define all the variables in serverless.yml file and resolve them indirectly via ${opt:stage} and ${opt:region} variables. I do not like this for two reasons:

  • all credentials are defined in one file and has to be committed to repo
  • very verbose syntax, which essentially repeats itself for every stage/region compilation

I am thinking, if stage/region information will be available during webpack'ing phase then it will be possible to dynamically load necessary settings with help of dotenv-webpack plugin.

May be I am missing something or even trying to approach the problem from the wrong site.

Any ideas will be much appreciated.

Thanks.

@hassankhan
Copy link
Contributor

I've posted my workaround for this issue here.

@HyperBrain
Copy link
Member

There have been vast improvements made in Serverless in the last few versions, especially for the variable system. E.g. it is now possible to have variables stored externally (S3) because the variable system has been been switched to asynchronous retrieval. Even CF output variables can be referenced and imported into variables now.

With this in mind, does the issue mentioned here still persist?

@hassankhan
Copy link
Contributor

hassankhan commented Jul 7, 2017

It still does unfortunately, although I'm not sure whether serverless-webpack or serverless-offline should be handling inserting env vars.

@HyperBrain
Copy link
Member

We even use serverless-export-env in combination with webpack/offline. This makes sure that for local runs the environment is set correctly.

@hassankhan
Copy link
Contributor

hassankhan commented Jul 7, 2017

@HyperBrain How do you load the generated .env into your handlers? Via a bootstrap script or using something like webpack-dotenv?

@HyperBrain
Copy link
Member

The export-env plugin sets the environment variables defined for your lambdas correctly when you use serverless-offline. That's the only way we use to get them set. Maybe @arabold can provide some details for his plugin.

@arabold
Copy link

arabold commented Jul 7, 2017

@Spaider I fail to see how an external .env file that's loaded with dotenv relates to the serverless-webpack plugin. I assume that what happens is that webpack bundles the .env file directly in the package depending on which stage is being built. So if you build stage "dev" for "us-east-1" it automatically bundles the us-east-1.dev.env file and thus prevents you to use the same build package for other regions and stages? If that's the case then this can typically be solved via either the webpack.config.

The serverless-export-env plugin doesn't really solve much here IMO. It's purpose is to expose environment variables defined in the serverless.yml and referencing CloudFormation resources to your local environment, e.g. during running local tests (think npm test) or when using the serverless-offline plugin. But that's not the problem here.

@Spaider
Copy link
Author

Spaider commented Jul 20, 2017

Sorry guys, it was so long ago, I had been drawn to other projects already. Not sure if I will have time to check this soon.

@HyperBrain
Copy link
Member

@Spaider No problem. It was serverless-webpack that fell asleep like Sleeping Beauty for quite a long time 😃 so it's not your fault. But we kissed it alive again.
I will close the issue then. Feel free to reopen it if you have time to check it in the future.

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

No branches or pull requests

4 participants