Terraform by Example: Data Sources
Data sources allow you to query existing infrastructure information. This sample code demonstrates fetching the latest Ubuntu AMI ID dynamically instead of hardcoding it.
Code
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}Explanation
Data sources provide a read-only view into existing infrastructure, allowing your Terraform configuration to use information defined outside of its own state. They are essential for fetching dynamic data, such as the latest Amazon Machine Image (AMI) IDs, VPC details, or security group identifiers.
In this example, the aws_ami data source queries the AWS API. Key features include:
- Dynamic Lookups: Finds the most recent image automatically
- Filtering: Narrows results by name, virtualization type, or other attributes
- Ownership: Restricts searches to trusted publishers (e.g., Canonical)
Once a data source is defined, its attributes can be referenced elsewhere in your configuration using the standard data.type.name.attribute syntax. This allows you to seamlessly integrate external data into your managed resources, ensuring that your infrastructure is always built using the most current and correct information available.
Code Breakdown
data "aws_ami" initiates a read-only query for an AMI.most_recent = true ensures we get the latest version of the image.filter block restricts the search query to specific criteria.owners limits the search to trusted account IDs (Canonical).data.aws_ami.ubuntu.id retrieves the specific ID from the query result.
