-
-
Notifications
You must be signed in to change notification settings - Fork 183
Environment Variables
There are several easy ways to configure environment variables with Jets.
Environment variables can be set with optional .env files. If there is an .env file at the root of your project. Jets will apply the env key value pairs in the file to all of your Lambda functions. Example:
.env:
global_env_key1=global_env_value1
global_env_key2=global_env_value2Your app will have the environment variables global_env_key1 and global_env_key2 set in every single one of their lambda functions.
Jets detects and uses env files based on a JETS_ENV convention to make it easier to apply different .env files to environments.
- If
JETS_ENV=productionand there's an.env.productionfile then Jets will use that file instead of the.envfile. - If
JETS_ENV=stagingand there's an.env.stagingfile then Jets will use that file instead of the.envfile. - If
JETS_ENVis not set and there's an.env.developmentfile then Jets will use that file instead of the.envfile.
The .env file is the fallback when the JETS_ENV specific file do not exist.
Environment variables can be set in the config/application.rb. This sets environment variables for all functions in your application.
config/application.rb
Jets.application.configure do
config.environment(
global_app_key1: "global_app_value1",
global_app_key2: "global_app_value2"
)
endYour app will have the environment variables global_app_key1 and global_app_key2 set in every single one of their lambda functions. These settings can be overridden within each class as described in the next section.
Environment variables can be set in the app code with class_environment, environment or properties. Examples:
class HardJob < ApplicationJob
# class_environment sets environment variables to all functions in the class
class_environment(
class_wide_key: "class_wide_value"
)
rate "10 hours" # every 10 hours
def dig
{done: "digging"}
end
rate "8 hours" # every 8 hours
# environment sets environment variables to the specific function
environment(
function_specfic_key: "function_specific_value"
)
def drive
puts("event data: #{event.inspect}")
{done: "driving"}
end
# properties allow you to directly set the lambda function properties.
properties(environment: {variables: {
key_from_properties: "value_from_properties"
}})
cron "0 */12 * * ? *" # every 12 hours
def lift
{done: "lifting"}
end
endSo in the case above:
- all methods
dig,drive, andliftwill have the environment variableclass_wide_value. -
drivewill have both environment variablesclass_wide_keyandfunction_specific_key. -
liftwill have both environment variablesclass_wide_keyandkey_from_properties.
The precedence order from highest to lowest.
- Code Variables: function specific. What the
environmentmethod sets. - Code Variables: class level. What the
class_environmentmethod sets. - Global Application Variables. What the
config/application.rbsets. - Env Files Variables. What the
.envfile set.
The general recomendation is to use the env files for simplicity.
The JETS_ENV variable will always be set on all lambda functions. You do not need to worry about seting it. It defaults to 'development'. You can override it in the CLI. Example:
JETS_ENV=staging jets deploy