Terraform Data query Archives - For all your terraform needs https://terraformarchitect.com/tag/terraform-data-query/ Automation cannot be an afterthought (TM) Mon, 13 Feb 2023 04:35:10 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 Retrieving Resource IDs in Terraform – The Terraform Data Block https://terraformarchitect.com/terraform-basics/the-terraform-data-block/?utm_source=rss&utm_medium=rss&utm_campaign=the-terraform-data-block https://terraformarchitect.com/terraform-basics/the-terraform-data-block/#respond Mon, 13 Feb 2023 04:23:30 +0000 https://terraformarchitect.com/?p=327 Also read – Terraform external data querying The Terraform Data Block – Retrieving a Project Id for a GCP project data “google_project” “project” { } output "project_number" { value = […]

The post Retrieving Resource IDs in Terraform – The Terraform Data Block appeared first on For all your terraform needs.

]]>
Also read – Terraform external data querying

The Terraform Data Block – Retrieving a Project Id for a GCP project

data “google_project” “project” {
}

output "project_number" {
  value = data.google_project.project.number
}

Terraform Destroy and the Data Block

When executing the terraform destroy command on our Terraform configuration, Terraform does not perform a destroy action on the resource called by the data block. It is a read only block.

The data block is also called when executing the terraform plan command, so your external resource must be present before you execute the terraform plan and terraform apply commands.

Searching for Projects using the terraform data block

One can apply a filter to search for specific projects – e.g. projects about to be deleted

data "google_projects" "my-organization-projs" {
  filter = "parent.id:23232323 lifecycleState:DELETE_REQUESTED"
}

data "google_project" "deletion-candidate" {
  project_id = data.google_projects.my-organization-projs.projects[0].project_id
}

Summary

Instead of hard coding IDs of resources, the terraform data block allows dynamic retrieval of resource IDs. It also allows searching / filtering based a filter inside the data block.





Need an experienced Cloud Networking or a Cloud Data Protection Expert?  Anuj has successfully delivered over a dozen deployments on each of the public clouds (AWS/GCP/Azure) including several DevSecOps engagements. Set up a time with Anuj Varma.

The post Retrieving Resource IDs in Terraform – The Terraform Data Block appeared first on For all your terraform needs.

]]>
https://terraformarchitect.com/terraform-basics/the-terraform-data-block/feed/ 0