I am trying to integrate Terraform go-SDK with my go-code where I already have an terraform template file and I need to create the infrastructure by importing that file to the go-SDK. I am not able to find any related documents. I tried with the godoc for terraform. With a little understanding, I tried to implement a simple terraform template. My terraform template file is,
provider "aws" {
access_key = "xxxx"
secret_key = "xxxx"
region = "xxxx"
}
resource "aws_instance" "example" {
ami = "ami-xxxx"
instance_type = "t2.micro"
# The name of our SSH keypair.
key_name = "xxxx"
# Security groupID
vpc_security_group_ids = ["sg-xxxx"]
#Subnet ID
subnet_id = "subnet-xxxx"
}
I tried via commandLine and I am able to bring up an instance with this file. I tried the same via my go-code. My understanding is that I need to create a Context in order to apply it. For creating a Context, I need a module.Tree. So I tried creating a module.Tree with config.Config. I imported the above terraform template file to my go-code to create a config and a tree. My go-code looks like,
package main
import (
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"fmt"
)
func main() {
con, err := config.LoadFile(path to .tf file)
fmt.Println("Config: ", con)
fmt.Println("Error: ", err)
fmt.Println("Validate: ", con.Validate())
tree := module.NewTree("testTree", con)
fmt.Println("Tree: ", tree.String())
}
When I execute my code, I am not getting any errors for loading the config from file, or validating the same. But I am getting the output as "tree not loaded",
Output:
Config: &{ <nil> <nil> [] [0xc4201cd0c0] [0xc4201f03c0] [] [] [] []}
Error: <nil>
Validate: <nil>
Tree: testTree not loaded
Can someone help me out to implement this? Thanks in advance.