-
Notifications
You must be signed in to change notification settings - Fork 416
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
All environment variables are missing #274
Comments
Hi @zamirdan , thanks for raising the issue. This is very strange. The plugin does not do anything with the environment variables and that should be handled completely by Serverless. Are the variables missing when you deploy the function, or only if you run the code locally? And which tool do you use to run them? Can you post the complete |
Thank you for the quick reply. yml file:
|
Can you try the same with one of the provided examples, preferrably https://github.com/serverless-heaven/serverless-webpack/tree/master/examples/serverless-offline or https://github.com/serverless-heaven/serverless-webpack/blob/master/examples/babel-dynamically-entries? |
@zamirdan Thanks for the feedback 👍 |
Opened a separate feature request for the improvement. Closing this issue now. |
Hey @HyperBrain - I am facing the same issue when trying to access
serverless.yml frameworkVersion: "=1.24.1"
package:
individually: true
provider:
name: aws
runtime: nodejs6.10
stage: dev
cfLogs: true
region: us-east-1
environment:
BLA: test
custom:
webpackIncludeModules: true
plugins:
- serverless-webpack
- serverless-offline-scheduler
- serverless-pseudo-parameters
- serverless-offline webpack.config.js 'use strict'
const nodeExternals = require('webpack-node-externals'),
webpack = require('webpack'),
path = require('path'),
UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
slsw = require('serverless-webpack'),
LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
module.exports = {
entry: slsw.lib.entries,
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js'
},
target: 'node',
externals: [
nodeExternals(),
],
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
path.resolve(__dirname)
],
exclude: [
path.resolve(__dirname, 'node_modules'),
],
query: {
presets: [
[
'env',
{
'targets': {
'node': '6.10',
'uglify': false
},
'useBuiltIns': true,
'modules': false,
'debug': false
}
]
],
plugins: [
'transform-runtime',
'transform-async-to-generator',
'transform-export-extensions',
'transform-es2015-modules-commonjs',
'lodash',
],
}
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
plugins: [
new LodashModuleReplacementPlugin({
'collections': true,
'shorthands': true
}),
new webpack.DefinePlugin({
'process.env': {NODE_ENV: JSON.stringify('production')}
}),
],
}; |
@swarajgiri Your configuration looks good. Do you have the problem with more than one project or only with this one?
You could do some checks:
Strange is, that serverless-webpack does not do anything with the configuration of your functions - only serverless composes them. If the function configuration in the CF template or in the AWS console show the variable, then something with the configuration (webpack config) is wrong. |
@HyperBrain - I just encountered this issue in a new project. Wasn't using env variables before. Will try out the checks and report back. |
Tried out the suggestions, found that the cf template contains the env variables and they are also available on aws lambda console. Its just that the function does not find them. function module.exports.handler = (event, context, cb) => {
const response = {
statusCode: 200,
body: JSON.stringify({
'data': process.env.BLA,
})
};
cb(null, response);
}; serverless.yml plugins:
- serverless-webpack
- serverless-offline and presets: [
'node6'
], Same error in both cases The env vars are present in cf template "MoveToPriorityQueueLambdaFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "content-processing-dev-moveToPriorityQueue",
"Handler": "moveToPriorityQueue.handler",
"MemorySize": 128,
"Role": {
"Fn::GetAtt": [
"IamRoleLambdaExecution",
"Arn"
]
},
"Runtime": "nodejs6.10",
"Timeout": 10,
"Description": "Move processing requests to priority queues",
"Environment": {
"Variables": {
"BLA": "test"
}
}
},
}, Node: v8.9.1 |
@HyperBrain - Any ideas on what might be causing this? |
@swarajgiri |
@HyperBrain - I have const nodeExternals = require('webpack-node-externals'),
webpack = require('webpack'),
path = require('path'),
UglifyJSPlugin = require('uglifyjs-webpack-plugin'),
slsw = require('serverless-webpack'),
LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
module.exports = {
entry: slsw.lib.entries,
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, '.webpack'),
filename: '[name].js'
},
target: 'node',
externals: [
nodeExternals(),
'aws-sdk',
], // exclude external modules,
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: [
path.resolve(__dirname)
],
exclude: [
path.resolve(__dirname, 'node_modules'),
],
query: {
presets: [
[
'env',
{
'targets': {
'node': '6.10',
'uglify': true
},
'useBuiltIns': true,
'modules': false,
'debug': false
}
]
],
plugins: [
'transform-runtime',
'transform-async-to-generator',
'transform-export-extensions',
'transform-es2015-modules-commonjs',
'lodash',
],
}
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
plugins: [
new LodashModuleReplacementPlugin({
'collections': true,
'shorthands': true
}),
new UglifyJSPlugin({
cache : true,
parallel : true,
}),
new webpack.DefinePlugin({
'process.env': {NODE_ENV: JSON.stringify('production')}
}),
],
}; |
I think it's your define here: new webpack.DefinePlugin({
'process.env': {NODE_ENV: JSON.stringify('production')}
}), You declare the complete new webpack.DefinePlugin({
'process.env.NODE_ENV': 'production'
}), Then only the very specific env var is replaced in the code |
Tried that. Would it help if i create a repo replicating the issue? |
But can you first remove the define plugin from your webpack config completely, just to check if it still isn't some configuration issue there? Then a repro repo would help. |
Tried that too. Still undefined. |
@HyperBrain I am running into this issue as well. I am applying
Run Perhaps there is another way of meeting this requirement that I am not seeing, but I am surprised that the serverless env variables aren't available to webpack.config.js. Any suggestions would be greatly appreciated. Edit: You can access the provider env variables in the webpack.config.js, like so:
Originally posted by @pizzarob in #222 (comment) |
This is a Bug Report
Description
All environment variables are missing.
I have built very simple project with only one function:
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: process.env.MY_VAR,
input: event,
}),
};
callback(null, response);
};
I have added to the yml file under provider this lines
environment:
MY_VAR: abc
when I run it without the plugin it works, but after I'm installing the plugin it doesn't.
Similar or dependent issue(s):
I'm using:
serverless-webpack 4.0.0
webpack 3.8.1
serverless 1.24
Windows 10
The text was updated successfully, but these errors were encountered: