Home > Backend Development > Golang > How to pass `ApplyConfig` to `tf.Apply()` in `hashicorp/terraform-exec`?

How to pass `ApplyConfig` to `tf.Apply()` in `hashicorp/terraform-exec`?

王林
Release: 2024-02-09 10:50:29
forward
454 people have browsed it

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

php editor Youzi will explain to you how to pass `ApplyConfig` to `tf.Apply()` in `hashicorp/terraform-exec`. When using `hashicorp/terraform-exec` for Terraform deployments, you can configure the behavior of `tf.Apply()` by creating an `ApplyConfig` object. This object is then passed to the `tf.Apply()` method for the appropriate deployment operation. This way, you can flexibly control the deployment process and implement customized configuration needs. In actual applications, you can set the properties of the `ApplyConfig` object according to specific business needs to achieve the best deployment effect.

Question content

I am trying to add target to terraform using golang sdk in hashicorp/terraform-exec apply

in the command Ideally the equivalent command for the cli is terraform apply --auto-approve --target 'module.example'

However, when I pass the targets in applyoptions{} to the apply() function, I get the following error.

Can someone point out what I'm doing here?

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)
    }
}
Copy after login

Error display, invalid compound literal type tfexec.applyoptioncompiler

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

Solution

I think the following should work:

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)
    }
Copy after login

The above is the detailed content of How to pass `ApplyConfig` to `tf.Apply()` in `hashicorp/terraform-exec`?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template