data external terraform Archives - For all your terraform needs https://terraformarchitect.com/tag/data-external-terraform/ Automation cannot be an afterthought (TM) Mon, 13 Feb 2023 04:33:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 Data Block External Data – Querying External Resources in Terraform https://terraformarchitect.com/terraform-basics/data-block-external-data/?utm_source=rss&utm_medium=rss&utm_campaign=data-block-external-data https://terraformarchitect.com/terraform-basics/data-block-external-data/#comments Wed, 14 Sep 2022 16:38:32 +0000 https://terraformarchitect.com/?p=258 Retrieving External data Use either the data block or the terraform_remote_state block to retrieve external data. However, there are scenarios where the data block does not exist in the provider or terraform_remote_state cannot be used, such as […]

The post Data Block External Data – Querying External Resources in Terraform appeared first on For all your terraform needs.

]]>
Retrieving External data

Use either the data block or the terraform_remote_state block to retrieve external data.

However, there are scenarios where the data block does not exist in the provider or terraform_remote_state cannot be used, such as when we need to process with an external API or need to use a local tool and process its output.

# Read the JSON payload from stdin
$jsonpayload = [Console]::In.ReadLine()

# Convert JSON to a string
$json = ConvertFrom-Json $jsonpayload
$environment = $json.environment

if($environment -eq "Production"){
$location="westeurope"
}else{
$location="westus"
}

# Write output to stdout
Write-Output "{ ""location"" : ""$location""}"

Retrieving External data

data "external" "getlocation" {
program = ["Powershell.exe", "./GetLocation.ps1"]
query = {
environment = "${var.environment_name}"
  }
}

Summary

External data sources are extremely useful in terraform – the data ‘external’ is defined for just this use case.





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 Data Block External Data – Querying External Resources in Terraform appeared first on For all your terraform needs.

]]>
https://terraformarchitect.com/terraform-basics/data-block-external-data/feed/ 1