gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.92k stars 966 forks source link

Help needed with terragrunt GO SDK #2380

Open md-saad-uddin-ansari opened 1 year ago

md-saad-uddin-ansari commented 1 year ago

Hi Team,

I am trying to interact with terragrunt programatically using golang sdk. (https://pkg.go.dev/github.com/gruntwork-io/terragrunt/cli#RunTerragrunt ) but am finding it difficult to understand how can i take this approach.

use case :

for eg, consider this command which we run using terragrunt cli : terragrunt run-all plan .

now i wanna to do exactly the same thing (and all other cmds) but via the terragrunts go pkg. I just need some help to get started, perhaps if someone can help me with a small example of how to achieve this, then i can take it from there .

This is really critical to my project, and any help is highly appreciated. thanks !

denis256 commented 1 year ago

Hi, examples from integration_test.go aren't helpful? https://github.com/gruntwork-io/terragrunt/blob/master/test/integration_test.go#L162

Also as an alternative, can be used terratest functions to launch terragrunt https://github.com/gruntwork-io/terratest/blob/master/modules/terraform/apply_test.go#L74

md-saad-uddin-ansari commented 1 year ago

Hi @denis256 , actually I am unable to figure out how to make it work, Hence i was looking for an working example. Is it possible for you to please help me with an working example , even a simple "terragrunt plan" example via gosdk would be enough for me to get started.

Would really appreciate any help here, thanks a ton in advance.

cryonex commented 3 months ago

I'm not a maintainer or anything, but I figured out how to make this work. Here's a basic example that captures and prints the output:

package main

import (
    "bytes"
    "context"
    "fmt"
    "os"

    tgCli "github.com/gruntwork-io/terragrunt/cli"
)

func main() {
    tgOutBuf := bytes.NewBuffer(make([]byte, 0))
    tgApp := tgCli.NewApp(tgOutBuf, os.Stderr)
    if err := tgApp.RunContext(context.TODO(), []string{
        "terragrunt",
        "plan",
        "--terragrunt-no-color",
        "--terragrunt-tfpath", "/usr/local/bin/terraform",
        "--terragrunt-working-dir", "/tmp/repo",
    }); err != nil {
        panic(err)
    }
    fmt.Println(tgOutBuf.String())
}