Hi BoltOps Community,
I don’t have any background on Ruby/ERB, and would like some pointers/help in doing perhaps something basic with ERB templating…
I am trying to do some ERB templating with terraspace, and would like to read data from a JSON file. E.g. content of vpc_config.json
{
"network": {
"vpcs": {
"main": {
"name": "main",
"vpc_cidr": "10.98.20.0/22",
"subnets": {
"public": {
"az1": {
"subnet_type": "external",
"subnet_name": "public",
"subnet_az" : "1",
"subnet_cidr": "10.98.20.0/26"
},
"az2": {
"subnet_type": "external",
"subnet_name": "public",
"subnet_az" : "2",
"subnet_cidr": "10.98.20.64/26"
}
},
"private": {
"az1": {
"subnet_type": "internal",
"subnet_name": "private",
"subnet_az" : "1",
"subnet_cidr": "10.98.21.0/26"
},
"az2": {
"subnet_type": "internal",
"subnet_name": "private",
"subnet_az" : "2",
"subnet_cidr": "10.98.21.64/26"
}
},
"database": {
"az1": {
"subnet_type": "internal",
"subnet_name": "database",
"subnet_az" : "1",
"subnet_cidr": "10.98.22.0/26"
},
"az2": {
"subnet_type": "internal",
"subnet_name": "database",
"subnet_az" : "2",
"subnet_cidr": "10.98.22.64/26"
}
}
}
}
}
}
}
If I want to read the vpc_config.json and try to use ERB templating to loop through the subnets (rather than using terraform’s for_each
loop), would it correct to assume that I will need to create a customer helper file to do this?
I have tried creating a custom helper file by doing terraspace new helper custom --type project
(which config/helpers/custom_helper.rb
for me).
And in the custom_helper.rb
, when I tried to do
require 'json'
file = File.read('path_to_vpc_config.json')
data_hash = JSON.parse(file)
module Terraspace::Project::CustomHelpers
def app_name
"#{@mod.name}-#{Terraspace.env}"
end
end
running terraspace build against the stack gives an error saying that vpc_config.json is the wrong constant name (or something along the lines of that). Any pointers on how I would be able read the JSON file so that I can reference it in my .tf files?
Many thanks,
James