1. Recap: Terraform Basics
- Commands:
-
terraform apply: Creates/updates infrastructure. -
terraform destroy: Removes all managed resources. -
terraform fmt: Auto-format code for readability.
-
- State File:
-
terraform.tfstatetracks resource metadata. - Backup files (
terraform.tfstate.backup) allow recovery if state is corrupted.
-
2. Introduction to Terraform Variables
Purpose: Avoid hardcoding values, improve reusability, and manage configurations across environments.
Variable Declaration (variables.tf)
variable "bq_dataset_name" { description = "My BigQuery dataset name" type = string default = "demo_dataset" } variable "gcs_bucket_name" { description = "My GCS bucket name" type = string default = "terraform-demo-bucket" } variable "location" { description = "Project location (region/multi-region)" type = string default = "US" } variable "credentials" { description = "Path to service account JSON file" type = string default = "./keys/my-creds.json" } Key Notes:
- Use
descriptionfor clarity. -
defaultprovides a fallback value (optional but recommended for testing). - Types:
string,number,bool,list,map, etc.
3. Using Variables in Resources
Example: Modify main.tf to reference variables.
provider "google" { project = var.project region = var.location credentials = file(var.credentials) # Read file content } resource "google_storage_bucket" "demo-bucket" { name = var.gcs_bucket_name location = var.location force_destroy = true } resource "google_bigquery_dataset" "demo-dataset" { dataset_id = var.bq_dataset_name location = var.location delete_contents_on_destroy = true } Key Functions:
-
file(var.credentials): Reads the JSON key file for authentication.
4. Workflow with Variables
-
Initialize and Plan:
terraform init # Install providers terraform plan # Preview changes -
Apply Configuration:
terraform apply # Deploy resources -
Destroy Resources:
terraform destroy # Clean up
5. Handling Credentials Securely
- Best Practices:
- Never hardcode credentials in
main.tf. - Use
variables.tfto reference external files (e.g.,keys/my-creds.json). - Avoid committing credentials to version control (add
.jsonto.gitignore).
- Never hardcode credentials in
Troubleshooting Authentication:
- Error: No credentials loaded.
- Ensure
credentialsvariable points to the correct JSON file path. - Use
export GOOGLE_APPLICATION_CREDENTIALS=./keys/my-creds.jsonas a fallback.
- Ensure
6. Advanced Tips
- Variable Files (
.tfvars):- Create
dev.tfvarsorprod.tfvarsfor environment-specific values. - Apply with
terraform apply -var-file="dev.tfvars".
- Create
- Dynamic Values:
- Use
terraform.tfvarsfor local overrides (automatically loaded).
- Use
-
Validation:
variable "location" { validation { condition = contains(["US", "EU"], var.location) error_message = "Allowed values: US, EU." } }
7. Key Takeaways
- Reusability: Variables centralize configuration, making code adaptable.
- Security: Keep credentials external and never expose them.
- Scalability: Use
.tfvarsand modules for complex projects.
Next Steps: Explore Terraform modules, remote state storage (e.g., GCS), and environment-specific workflows.
Top comments (0)