${env:MYVAR} not working in serverless.yml
See original GitHub issueWhen I run in offline mode (serverless-offline
) I’m unable to access the loaded env
variables in my serverless.yml. For example in the config below, the ${env:STAGE}
always rolls over to the default.
# serverless.yml
service: serverless-framework-starter
plugins:
- serverless-dotenv-plugin
- serverless-webpack
- serverless-offline #serverless-offline needs to be last in the list
custom:
webpack:
webpackConfig: 'webpack.config.js'
includeModules: true
packager: 'npm'
serverless-offline:
port: 4000
dotenv:
path: "./.env.${opt:NODE_ENV, 'development'}"
provider:
name: aws
runtime: nodejs8.10
stage: ${env:STAGE, 'dev'}
region: ${env:REGION, 'us-east-1'}
versionFunctions: false
iamRoleStatements:
- Effect: "Allow"
Action:
- "s3:*"
Resource: "*"
functions:
test-func:
handler: src/test-func/handler.default
name: ${self:provider.stage}-${self:service}-upload
memorySize: 128
events:
- http:
path: hello
method: get
cors: true
The test handler has no trouble accessing SOMEVAL
and it uses the proper value if I change the NODE_ENV
at runtime.
// handler.js
const hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: `Go Serverless v1.0! Your function executed successfully! ENV: ${process.env.SOMEVAL}`,
input: event,
}),
};
callback(null, response);
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};
export default hello;
It seems like a timing issue. It seems like the serverless.yml
is processed before the serverless-dotenv-plugin
is able to load the environment variables into scope. Any idea how I can prove this or whether this is the right concept?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:11
Top Results From Across the Web
Tests do not load .env or serverless.yml variables #51 - GitHub
So, it seems the tests are expecting this variable under process.env.SAMPLE_ENV_VAR . To get it to work correctly, I had to change the...
Read more >Variables - Serverless Framework
To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless.yml configuration file. It is valid to use the empty string in place...
Read more >Facing problem to set service name from env variable in ...
We are deploying to AWS Lambda and using container image. Serverless Framework version: 2.32.0 We could set the service name dynamically from ...
Read more >Serverless Environment Variables - SST.Dev
Serverless Framework builds on these ideas to make it easier to define and work with environment variables in our serverless.yml by generalizing the...
Read more >Lambda was unable to configure your environment variables
Problem. Lambda functions come with a set of default environment variables. These environment variable names are reserved, and users will not be able...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Have the same issue, and it’s pretty annoying =(
Oh, yeah, if it helps:
.env.development
.env.production