Terraform: Datasources
Data sources are a way to fetch and reference external information or data in your configuration. Unlike resources, which represent infrastructure elements that Terraform manages, data sources provide a way to import existing data into your Terraform configuration. Data sources allow you to query and use information from external systems, such as cloud providers, databases, or APIs, and use that data within your Terraform configuration.
In this article you will learn how to use them accordingly.
Structure
Data sources are declared using the data
block in your Terraform configuration.
data "aws_ami" "latest_amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
In this example, the aws_ami
data source is used to fetch the latest Amazon Linux AMI.
Attributes
Data sources have attributes that you can reference within your configuration. These attributes provide access to specific pieces of information retrieved from the external source.
resource "aws_instance" "example" {
ami = data.aws_ami.latest_amazon_linux.id
# other configurations...
}
Here, the ami
attribute of the aws_instance
resource references the AMI ID obtained from the aws_ami
data source.
Provider-Specific
Data sources are provider-specific, meaning they are designed to work with a particular cloud provider or external system. Each provider offers different data sources with their own set of attributes.
data "google_compute_network" "network" {
name = "my-network"
}
This example uses a Google Cloud Platform (GCP) data source to fetch information about a network.
Query and Filtering
Data sources often allow you to query and filter results based on specific criteria to retrieve the relevant data.
data "aws_subnet" "selected_subnet" {
count = 1 tags = {
Name = "my-subnet"
}
}
In this example, the aws_subnet
data source retrieves a subnet with a specific tag.
Dynamic Block Configuration
Data source configurations can include dynamic block definitions to dynamically generate multiple instances of a block based on data.
data "aws_instance" "example" {
for_each = toset(["instance-1", "instance-2"])
instance_id = each.value
}
This example dynamically queries information about multiple AWS instances based on a set of instance IDs.
Refreshing Data
Data sources are refreshed automatically during a terraform apply
or when explicitly refreshed using terraform refresh
. This ensures that the data is up-to-date.
Remote State Data Source
Terraform also allows you to use a remote state file as a data source. This can be useful for sharing data between different Terraform configurations.
data "terraform_remote_state" "network" {
backend = "s3"
config = {
bucket = "my-terraform-state-bucket"
key = "network.tfstate"
region = "us-west-2"
}
}
In this example, the terraform_remote_state
data source fetches information from a remote state file.