Datta
Datta

Reputation: 149

json file in terraform

I have a JSON file with the following and trying to list the shapes ex: "t3-nano, t3-micro, t3-small, t2.medium, t3-2xlarge, r6g-medium".

json file = info.json 


{
"t3-nano" : {
    "service_name" : "t3",
    "existing" : 100
},
"t3-micro" : {
    "service_name" : "t3",
    "existing" : 1
},
"t3-small" : {
    "service_name" : "t3",
    "existing" : 2
},    
"t2.medium" : {
    "service_name" : "t2",
    "existing" : 0
},
"t3-2xlarge" : {
    "service_name" : "t3-2",
    "existing" : 5
},     
"r6g-medium" : {
    "service_name" : "r6g.medium",
    "existing" : 10
}

}

I tried the following

    locals { 
service_name  = flatten([for i in local.info : i[*].service_name])
shapes = flatten([for i in local.info : i[*].index]) 
    }

and it got failed

Error: Unsupported attribute
This object does not have an attribute named "index".

I was expecting to print shapes = [t3-nano, t3-micro, t3-small, t2.medium, t3-2xlarge, r6g-medium]. Can someone help if there is a way to just list the shapes?

Upvotes: 0

Views: 280

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28774

The flatten function and for expression are both unnecessary here. The function keys already has the functionality and return value that you want to achieve:

shapes = keys(local.info)

and that will assign the requested value.

Upvotes: 1

Related Questions