Hi,
I am using Terraform v0.14.2
I have these defined,
data "http" "instances" {
url = "https://api.lookup.com/v1/organizations/${var.project_id}/instances"
request_headers = {
Authorization = "Bearer ${data.client_config.current.access_token}"
Accept = "application/json"
}
}
JSON response from the above call will be like this,
{
"instances": [
{
"name": "tf-inst-dashing-midge",
"location": "europe-west2-c",
"host": "10.101.0.2"
},
{
"name": "tf-inst-stirred-pug",
"location": "us-east1-a",
"host": "10.101.0.3"
}
]
}
Now I would like to be able to create multiple resources based on the json output,
resource "google_compute_instance_template" "default" {
metadata = {
ENDPOINT = for_each(jsondecode(data.http.instances.instances.host))
}
}
resource "google_compute_instance_group_manager" "web" {
name = "web"
zone = for_each(jsondecode(data.http.instances.instances.location))
}
Is this possible through Terraform, if so can you please tell me how to achieve it?
Thanks,
Arun
Hi @arun-a-nayagam,
I’m afraid my relative unfamiliarity with these particular Google Cloud Platform object types is making it hard for me to follow exactly what your goal is here.
From some reading of the docs, I have the impression that google_compute_instance_group_manager
is an object representing a group of instances all created from the same template, but your JSON data structure describes individual instances and so I’m not sure how to understand the relationship between the objects in your JSON response and the resource types you want to declare.
Do you intend to have one google_compute_instance_group_manager
object that has target_size = 2
to match the length of the instances list? If so, how would the data in each of the objects be included into the configuration for that?
Probably the best way to describe what you want to achieve would be to pretend that Terraform has no for_each
feature and just write out individual resource
blocks that would get the result you want with hard-coded resource configurations, and then from there we can hopefully have a shared understanding of your goals and I (or someone else) can propose a way to translate that to a more dynamic approach using your API response.
Hi @apparentlymart, Thank you for the quick response.
Sorry, I did confuse you specifying google resources.
Ignoring GCP resources altogether, based on this JSON,
{
"instances": [
{
"name": "tf-inst-dashing-midge",
"location": "europe-west2-c",
"host": "10.101.0.2"
},
{
"name": "tf-inst-stirred-pug",
"location": "us-east1-a",
"host": "10.101.0.2"
}
]
}
I would like to create multiple compute instances (ec2 in AWS) for each location (zone in AWS) in the JSON with the respective metadata/tag filled in with the host value.
I hope that makes sense now.
Thanks,
Arun