Data Sources Flashcards
What are terraform data sources
They allow Terraform to use/fetch info defined outside Terraform.
Ex: creating a data source block to fetch details of EC2 instances in region in AWS.
After fetching it’ll pass that info to the resource block and use that with its associated logic.
Example #1 - Data Source
Reading info of DO (digital ocean) account
Following data source code is used to get info on your digitalocean account
Ex: data “digitalocean_account” “example” {}
In the CLI, the status of reading/read complete is the action of “fetching” the information. It will not do anything with that info unless tied to a resource.
The info fetched is stored at the .tfstate file.
Example #2 - Reading a file
Following data source allows you to read contents of a file in your local filesystem
Ex:
data “local_file” “foo” {
filename = “${path.module}/demo.txt”
}
What is ${path.module}
Returns the current file system path where your code is located.
Ex:
data “local_file” “foo” {
filename = “${path.module}/demo.txt”
}
Example #3 - Fetch EC2 instance detailsd
Fetch details about EC2 instance in yoru AWS region
Ex:
data “aws_instances” “example” {}