Using data sources for retrieving data from not managed resources

2 min read | by Jordi Prats

Using data sources on terraform allows us to make use of information not managed by Terraform, or defined by another separate Terraform codebase. We can use it for any resource os even an entire terraform remote state

Let's dive in

First we will need to "select" the data we want to retrieve by creating the data resource specifying the needed variable to be able to identify the resource. For example, for retrieving an aws_route53_zone we will need to specify it's name and whether is a private zone like this:

data "aws_route53_zone" "private_r53_zone" {
  name         = var.domain_name
  private_zone = true
}

Once we have it defined, we can use it just as we would if it were defined as a resource but the data. prefix. For example:

resource "aws_route53_record" "cname_spinnaker" {
  zone_id = data.aws_route53_zone.private_r53_zone.zone_id
  name    = "spinnaker.${var.domain_name}"
  type    = "CNAME"

  ttl     = "3600"
  records = [module.spinnaker.hostname]
}

Posted on 23/03/2021

Categories