Parameterize configurations with variables, expose values with outputs, and understand what Terraform state is.
variable "region" {
description = "AWS region"
type = string
default = "us-east-1"
}
variable "environment" {
description = "Deployment environment"
type = string
# No default = required. Must be supplied.
}
variable "allowed_ips" {
description = "IPs allowed to access the server"
type = list(string)
default = ["0.0.0.0/0"]
}# In main.tf
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "main" {
bucket = "my-bucket-${var.environment}"
}# Values for required variables
environment = "staging"
allowed_ips = ["192.168.1.0/24", "10.0.0.0/8"]
# Never commit sensitive values — use env vars instead:
# TF_VAR_environment=staging terraform applyoutput "bucket_name" {
description = "Name of the created bucket"
value = aws_s3_bucket.main.bucket
}
output "bucket_arn" {
value = aws_s3_bucket.main.arn
}
# After apply:
# Outputs:
# bucket_name = "my-bucket-staging"
# bucket_arn = "arn:aws:s3:::my-bucket-staging"# terraform.tfstate is created after first apply
# It maps your config to real resources
# NEVER edit it manually
# NEVER commit it to git (may contain secrets)
# Add to .gitignore:
echo 'terraform.tfstate*' >> .gitignore
echo '.terraform/' >> .gitignore
# Remote state (team-friendly)
# terraform { backend "s3" { bucket = "my-tf-state" ... } }string, number, bool, list, map, object.terraform.tfvars supplies values. TF_VAR_name env vars work too — good for secrets.