我正在尝试将 Terraform go-SDK与我的go-code集成,我已经有了一个terraform模板文件,我需要通过将该文件导入go-SDK来创建基础架构。我无法找到任何相关文件。我尝试使用 godoc 作为terraform。稍微理解一下,我试图实现一个简单的terraform模板。我的terraform模板文件是,
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"
}
我尝试通过commandLine,我可以用这个文件调出一个实例。 我通过我的代码尝试了同样的方法。我的理解是,我需要创建上下文才能应用。要创建上下文,,我需要 module.Tree。所以我尝试使用 config.Config创建 module.Tree 。< / strong>我将上述 terraform 模板文件导入到我的go-code中,以创建 config 和树。我的go-code看起来像,
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())
}
当我执行我的代码时,我没有从文件加载配置或验证相同的错误。但我得到输出为“树未加载”,
Output:
Config: &{ <nil> <nil> [] [0xc4201cd0c0] [0xc4201f03c0] [] [] [] []}
Error: <nil>
Validate: <nil>
Tree: testTree not loaded
有人可以帮我实现吗?提前谢谢。