Getting an Environment Variable in Terraform Configuration
TLDR
To use environment variables in Terraform, you can pass them as input variables or use the TF_VAR_ prefix. Alternatively, use the env function to access environment variables directly in your configuration.
Environment variables are a convenient way to pass dynamic values to Terraform configurations. This guide will show you how to retrieve and use environment variables effectively in Terraform.
Why Use Environment Variables?
- Dynamic Configurations: Pass values that change between environments (e.g., dev, staging, prod).
- Security: Store sensitive data like API keys or credentials securely.
- Automation: Simplify CI/CD pipelines by using environment variables for configuration.
Using Input Variables with TF_VAR_ Prefix
Terraform automatically maps environment variables with the TF_VAR_ prefix to input variables.
Example: Using TF_VAR_
Define an input variable in your configuration:
variable "region" {
description = "The AWS region to deploy resources in."
type = string
}
Set the environment variable:
export TF_VAR_region=us-east-1
Run Terraform commands, and the region variable will automatically use the value from the environment variable.
Using the env Function
The env function allows you to access environment variables directly in your configuration.
Example: Using env
variable "api_key" {
default = "${env("API_KEY")}" # Replace "API_KEY" with your environment variable name
}
output "api_key" {
value = var.api_key
}
Set the environment variable:
export API_KEY=your-api-key
Run Terraform commands, and the api_key variable will use the value from the environment variable.
Best Practices
- Use Secure Storage: Store sensitive environment variables in a secure location, such as AWS Secrets Manager or HashiCorp Vault.
- Validate Inputs: Use
validationblocks to enforce constraints on input variables. - Document Variables: Clearly document which environment variables are required and their purpose.
By using environment variables in Terraform, you can create dynamic and secure configurations that adapt to different environments and workflows.
We earn commissions when you shop through the links below.
DigitalOcean
Cloud infrastructure for developers
Simple, reliable cloud computing designed for developers
DevDojo
Developer community & tools
Join a community of developers sharing knowledge and tools
SMTPfast
Developer-first email API
Send transactional and marketing email through a clean REST API. Detailed logs, webhooks, and embeddable signup forms in one dashboard.
QuizAPI
Developer-first quiz platform
Build, generate, and embed quizzes with a powerful REST API. AI-powered question generation and live multiplayer.
Want to support DevOps Daily and reach thousands of developers?
Become a SponsorFound an issue?
Related Posts
Also worth your time on this topic
Conditional Attributes in Terraform
Set Terraform resource attributes conditionally with the ternary operator. Covers syntax, dynamic blocks, and switching values per environment cleanly.
Terraform State Management
What is Terraform state, why is it important, and how do you manage state in a team environment?
mid
Terraform Repository Structure Checklist
Best practices for organizing and structuring your Terraform projects for maintainability and scalability.
30-45 minutes