Data Sources Flashcards

1
Q

What are terraform data sources

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Example #1 - Data Source

Reading info of DO (digital ocean) account

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Example #2 - Reading a file

A

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”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is ${path.module}

A

Returns the current file system path where your code is located.

Ex:
data “local_file” “foo” {
filename = “${path.module}/demo.txt”
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Example #3 - Fetch EC2 instance detailsd

A

Fetch details about EC2 instance in yoru AWS region

Ex:

data “aws_instances” “example” {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly