Is there a way to access configuration variables (environment, etc.) from the source code?
I read about context.stageVariables
but it’s empty for me when I run serverless invoke local -f create --path mocks/create-feedback.json
Is there a way to access configuration variables (environment, etc.) from the source code?
I read about context.stageVariables
but it’s empty for me when I run serverless invoke local -f create --path mocks/create-feedback.json
You can set the env vars on the provider
or function
level
# Service metadata
service:
name: basic-example
displayName: Basic Example
version: 0.0.1
description: "Demonstrate Hello World"
# Service provider
provider:
name: aws
runtime: nodejs6.10
environment:
My_key: ${opt:whateverVariable}
# Functions in service
functions:
hello:
handler: handler.hello
environment:
My_key_in_this_func_only: ${opt:whateverVariableTwo}
Then in your code you can access the variables on process.env
like:
console.log(process.env.My_key)
I used the ${opt:whateverVariable}
variable syntax to reference CLI flags but you can use any variable. The variable types are listed here: https://serverless.com/framework/docs/providers/aws/guide/variables/
Nice! I know I read it somewhere like 4 days ago, but couldn’t find where.
By the way, as the docs states, environment variable should be using uppercase
process.env.NODE_ENV