
This assumes you have installed Terraform, as described at https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started.
Let's say you have a script that returns JSON. As a simple example, let's say you have a Perl script named sample.pl that returns JSON.
#!/usr/bin/perl
use strict;
use warnings;
print '{ "foo": "bar" }';
This Perl script will return the following JSON.
{
"foo": "bar"
}
Let's say you have the following files on your Terraform server.
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
In data.tf in the same directory as your main root module (main.tf), external data source can use the JSON.
data "external" "example" {
program = [ "perl", "sample.pl" ]
}
The terraform console command can be used to display the map being returned. Notice in this example that the "result" map contains the "foo" key and "bar" value.
~]# terraform console
> data.external.example
{
"id" = "-"
"program" = tolist([
"perl",
"sample.pl",
])
"query" = tomap(null) /* of string */
"result" = tomap({
"foo" = "bar"
})
"working_dir" = tostring(null)
}
And here is how you would use the external data source in resources.tf. In this example, data.external.example.result.foo will contain the value "bar".
resource "external" "example" {
name = "${data.external.example.result.foo}"
}
Did you find this article helpful?
If so, consider buying me a coffee over at