Terraform Cost Optimization for AWS - Reduce Your Cloud Bill
Practical Terraform patterns to reduce AWS costs: right-sizing, spot instances, scheduling, and reserved capacity. Step-by-step guide with code examples and ...
Guides
Complete guide to Terraform data sources: querying existing infrastructure, filtering, and common patterns. Step-by-step guide with code examples and best pr...
Data sources let you fetch information about existing infrastructure that Terraform doesn't manage. They're read-only — they query but never create or modify resources.
# Look up the latest Ubuntu AMI
data "aws_ami" "ubuntu" {
most_recent = true
owners = ["099720109477"] # Canonical
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
}data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
region = data.aws_region.current.name
}data "aws_vpc" "main" {
filter {
name = "tag:Name"
values = ["production-vpc"]
}
}data "aws_availability_zones" "available" {
state = "available"
}data "aws_ssm_parameter" "db_host" {
name = "/prod/database/host"
}data "terraform_remote_state" "networking" {
backend = "s3"
config = {
bucket = "terraform-state"
key = "networking/terraform.tfstate"
region = "us-east-1"
}
}
# Use: data.terraform_remote_state.networking.outputs.vpc_id| Aspect | Data Source | Resource |
|---|---|---|
| Purpose | Read existing | Create/manage |
| Prefix | data. | Direct reference |
| State | Refreshed every plan | Tracked in state |
| Lifecycle | Read-only | Full CRUD |
terraform_remote_state for shared outputsmost_recent = true for AMI lookups to avoid stale IDsRelated: How to install Terraform on macOS — our most popular installation guide.
Practical Terraform patterns to reduce AWS costs: right-sizing, spot instances, scheduling, and reserved capacity. Step-by-step guide with code examples and ...
How to achieve zero-downtime deployments with Terraform using blue-green, rolling updates, and create_before_destroy. Step-by-step guide with code examples a...
Complete guide to testing Terraform configurations with terraform test, Terratest, and validation rules. Step-by-step guide with code examples and best pract...
Best practices for managing secrets, passwords, and sensitive data in Terraform configurations. Step-by-step guide with code examples and best practices for ...