首頁  >  文章  >  後端開發  >  如何將 `ApplyConfig` 傳遞給 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

如何將 `ApplyConfig` 傳遞給 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

王林
王林轉載
2024-02-09 10:50:29411瀏覽

如何将 `ApplyConfig` 传递给 `hashicorp / terraform-exec` 中的 `tf.Apply()`?

php小編柚子為您解答如何將 `ApplyConfig` 傳遞給 `hashicorp / terraform-exec` 中的 `tf.Apply()`。在使用 `hashicorp / terraform-exec` 進行 Terraform 部署時,可以透過建立 `ApplyConfig` 物件來配置 `tf.Apply()` 的行為。然後,將該物件傳遞給 `tf.Apply()` 方法,以便進行相應的部署操作。透過這種方式,您可以靈活地控制部署流程,並實現自訂的配置需求。在實際應用中,您可以根據特定的業務需求來設定 `ApplyConfig` 物件的屬性,以達到最佳的部署效果。

問題內容

我正在嘗試使用golang sdk 在hashicorp/terraform-exec 中使用golang sdk 將目標#添加到terraform apply 指令中

理想情況下,cli 的等效指令是 terraform apply --auto-approve --target 'module.example'

## 但是,當我將

applyoptions{} 中的 targets 傳遞給 apply() 函數時,出現以下錯誤。

有人能指出我在這裡做什麼嗎?

package main

import (
    "context"

    "github.com/hashicorp/terraform-exec/tfexec"
)

func main() {
    // create a new tfexec.executor instance
    tf, err := tfexec.newterraform("/path/to/terraform/binary")
    if err != nil {
        panic(err)
    }
    err = tf.init(context.background(), tfexec.upgrade(true))
    if err != nil {
        panic(err)
    }
    // define the targets you want to apply
    targets := []string{"module.example", "module.another_example"}

    // create an applyoption with the targets
    applyoption := tfexec.applyoption{
        targets: targets,
    }

    // apply the terraform configuration with the defined targets
    err = tf.apply(context.background(), applyoption)
    if err != nil {
        panic(err)
    }
}

錯誤顯示,

invalid 複合文字類型 tfexec.applyoptioncompiler

go run test.go # command-line-arguments ./test.go:23:17: invalid composite literal type tfexec.ApplyOption

解決方法

我認為以下應該有效:

targets := []tfexec.ApplyOption{
        tfexec.Target("module.example"),
        tfexec.Target("module.another_example"),
    }

    // Apply the Terraform configuration with the defined targets
    err = tf.Apply(context.Background(), targets...)
    if err != nil {
        panic(err)
    }

以上是如何將 `ApplyConfig` 傳遞給 `hashicorp / terraform-exec` 中的 `tf.Apply()`?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除