How to use Environment Variables
Last updated: June 29, 2024
Environment variables are a key feature in Spacelift that enable you to configure and manage your infrastructure deployments effectively. They can be used to pass configuration settings, credentials, and other information.
For the full detailed guide, visit the Environment Variables Documentation.
Terraform Provider for Environment Variables
Spacelift's Terraform provider allows you to manage environment variables programmatically, offering a powerful way to automate and version control your configuration settings.
To use environment variables with the Terraform provider:
Define Environment Variables in Terraform:
Use the
spacelift_stack_environment_variableresource to manage environment variables.Example:
resource "spacelift_stack_environment_variable" "example" { stack_id = spacelift_stack.example.id key = "EXAMPLE_KEY" value = "example_value" }
Update and Manage Variables:
You can update and manage these variables through Terraform, ensuring changes are tracked and reversible.
This approach integrates seamlessly with your existing Terraform workflows, providing a consistent method for managing infrastructure and its configurations.
For more detailed instructions on using the Terraform provider, refer to the Terraform Provider Documentation.
Steps to Set Up Environment Variables in the UI
Adding an Environment Variable:
Navigate to your stack's settings.
Under the Environment tab, click Add environment variable.
Enter the key and value for the environment variable. You can also mark it as secret if needed.
Editing an Environment Variable:
Find the environment variable you wish to edit.
Click the three-dot menu on the right-hand side of the variable row and select Edit.
Update the key, value, or description as necessary.
More Information on Environment Variables
Environment Variable Interpolation:
Environment variables can refer to other environment variables using simple interpolation.
For example, if you have an environment variable
FOOwith a value ofbar, you can define another variableBAZas${FOO}-baz, which will result inbar-bazas the value ofBAZ.
Computed Values:
Some environment variables are marked as
<computed>, meaning their value is only computed at runtime.These can include dynamically generated credentials or Spacelift-specific metadata.
You can override a computed value by setting an environment variable with the same name.
Precedence Rules:
If the same environment variable is defined in multiple places, Spacelift applies a set of precedence rules:
Directly set stack variables take the highest precedence.
Variables from attached contexts come next.
Computed values have the lowest precedence.
Per-Stage Environment Variables:
Spacelift supports defining environment variables specific to different stages of the workflow (e.g., initializing, planning, applying).
Use prefixes like
ro_(read-only) andwo_(write-only) to control which stages the variables are available in.
Special Considerations
Secret Environment Variables: These are write-only and not visible in the UI or API. They are only available to runs and tasks.
Mounted Files: Sometimes, you may need to use files instead of environment variables. These files can also be marked as secret and have similar visibility controls.
Spacelift Environment: Certain environment variables provide metadata about the context of the job being executed, such as
TF_VAR_spacelift_commit_shaorTF_VAR_spacelift_run_id.
Additional Information
Context Integration: Environment variables can also be set in contexts, which can then be attached to multiple stacks.
Visibility: Plain variables are visible in the UI and API, while secret ones are not.
File Size Limit: Mounted files are limited to 2 MB in size.
For more detailed instructions and examples, refer to the full Environment Variables Documentation.