O'Reilly logo

Getting Started with Terraform - Second Edition by Kirill Shirinkin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Providing data from anywhere with external_data

As of Terraform 0.8, there is also an external_data resource. It allows us to call any other program and use the data returned by it, as long as it implements a specific protocol. The main requirement for this program is that it returns a valid JSON as a result of execution. Create the following tiny Ruby script in the root template directory:

require 'json' 
data = { 
  owner: "Packt" 
} 
puts data.to_json 
You need to have Ruby installed for this to work.

Now configure external data resource as follows:

data "external" "example" { 
  program = ["ruby", "${path.module}/custom_data_source.rb"] 
} 

Finally, use it inside the module to extend its name attribute:

 module "mighty_trousers" { source = "./modules/application" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required