syst0m
syst0m

Reputation: 391

Fetched vs computed values in Terraform data sources

According to Terraform docs, data sources can be used to fetch or compute values by using different provider implementations.

Ref. - https://www.terraform.io/docs/configuration/data-sources.html

What exactly is the computed vs. fetched distinction, preferrably in a concrete example?

Upvotes: 2

Views: 1391

Answers (1)

kenlukas
kenlukas

Reputation: 3973

The distinction is slight:

At a high level, fetched data exists before you run your template and computed data is created during runtime.

Fetched data would be similar to the example in the reference. Here Terraform is fetching information about an AMI which can then be used to build out an EC2 instance:

data "aws_ami" "web" {
  filter {
    name   = "state"
    values = ["available"]
  }

  filter {
    name   = "tag:Component"
    values = ["web"]
  }

  most_recent = true
}

Computed data could generally be defined as data that doesn't exist before Terraform starts but will be generated (computed) as your template proceeds. For example if you were to create an EIP to use with an EC2 instance you would run:

resource "aws_eip" "ip" {
  instance = "${aws_instance.example.id}"
}

From that, several bits of information will be computed as the EIP is created:

+ aws_eip.ip
    allocation_id:     "<computed>"
    association_id:    "<computed>"
    domain:            "<computed>"
    instance:          "${aws_instance.example.id}"
    network_interface: "<computed>"
    private_ip:        "<computed>"
    public_ip:         "<computed>"

You could then use those computed values in other resources. e.g. passing aws_eip.ip.public_ip to a security group for example.

Upvotes: 4

Related Questions