Understanding Terraform Data Blocks

By Łukasz Kallas
Picture of the author
Published on
terraform image

Data blocks in Terraform allow you to query and fetch information about existing resources in your infrastructure. They are essential for referencing resources that are managed outside of your current Terraform configuration.

What are Data Blocks?

Data blocks, also known as data sources, enable you to query external resources and use their attributes in your Terraform configuration. Unlike resource blocks that create and manage infrastructure, data blocks only read existing resources.

Common Use Cases

Here are some typical scenarios where data blocks prove invaluable:

  • Query resources across different AWS accounts
  • Access existing infrastructure not managed by Terraform
  • Build dynamic configurations based on existing resources
  • Fetch latest AMI IDs or availability zones

Practical Examples

Querying an Existing VPC
data "aws_vpc" "existing_vpc" {
  id = "vpc-1234567890"
}
Finding the Latest AMI
data "aws_ami" "amazon_linux_2" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
Fetching Available Zones
data "aws_availability_zones" "available" {
  state = "available"
}

Best Practices

  1. Always specify owners: When querying AMIs, always specify the owner to ensure you're getting images from trusted sources:

    • amazon for official Amazon AMIs
    • self for your own AMIs
    • Specific owner IDs for other trusted sources
  2. Use filters effectively: Combine multiple filters to get exactly what you need:

  3. Version Awareness: Keep in mind that data source attributes may change between provider versions.

Stay Tuned

Want to learn?
The best articles, links and news related to software development delivered once a week to your inbox.