Json support for api driven runs

Hi Team,

I was successfully able to do api-driven runs with main.tf file compressed to main.tar.gz but our project has a challenge to run with main.tf.json instead. Is there a support or way I can compress main.tf.json to main.tar.gz and run it through api.

Hi @rajagopalanrg,

Are you asking about Terraform Cloud API-driven Runs? It sounds like you’re talking about step 2, “create the file for upload”, and you were previously able to create an archive from a directory containing main.tf but you found a problem when you tried the same procedure with a directory containing main.tf.json.

If so, could you please share the sequence of steps you followed in both cases and what result you saw from doing so?

Hi @apparentlymart,

Exactly, what you said is right. I tried to create a json template for terraform and zipped it using golang. Then I pushed it to terraform cloud api using go-tfe. After I pushed, it started the run and said the syntax for tf is wrong.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.

Error: Argument or block definition required

  on main.tf line 1:
   1: {

An argument or block definition is required here.`An argument or block definition is required here.

I am putting my code right here,
{

    "module": {

        "azureVM": {

            "admin_password": "Password@2020",

            "admin_username": "winuser",

            "location": "westeurope",

            "os_data_disk_size_in_gb": 0,

            "resource_group_name": "Testing",

            "source": "",

            "storage_data_disks": [

                {

                    "CreateOption": "Empty",

                    "Lun": 0,

                    "Name": "datadisk1",

                    "DataDiskSizeInGB": 0

                }

            ],

            "subnet_name": "subnet001",

            "version": "1.0.4",

            "vm_name": "euwmvm01",

            "vm_size": "Standard_DS1_v2",

            "vm_sku": "2016-Datacenter",

            "vnet_name": "vnet001"

        }

    },

    "provider": {

        "azurerm": {

            "client_id": "${var.client_id}",

            "client_secret": "${var.client_secret}",

            "features": {},

            "subscription_id": "${var.subscription_id}",

            "tenant_id": "${var.tenant_id}",

            "version": "=2.4.0"

        }

    },

    "variable": {

        "client_id": {

            "type": "string"

        },

        "client_secret": {

            "type": "string"

        },

        "subscription_id": {

            "type": "string"

        },

        "tenant_id": {

            "type": "string"

        }

    }

}

The go lang code to convert into tar.gz is below,
package main

import (

    "bufio"

    "compress/gzip"

    "fmt"

    "io/ioutil"

    "os"

    "strings"

)

func main() {

    // Open file on disk.

    name := "main.tf.json"

    f, _ := os.Open("C:\\Users\\xxxx\\go\\src\\terracloud\\ws-jBf7St7V2Sa7fLRZ\\" + name)

    // Create a Reader and use ReadAll to get all the bytes from the file.

    reader := bufio.NewReader(f)

    content, _ := ioutil.ReadAll(reader)

    // Replace txt extension with gz extension.

    name = strings.Replace(name, ".tf.json", ".tar.gz", -1)

    // Open file for writing.

    f, _ = os.Create("C:\\Users\\LK757HL\\go\\src\\terracloud\\ws-jBf7St7V2Sa7fLRZ\\" + name)

    // Write compressed data.

    w := gzip.NewWriter(f)

    w.Write(content)

    w.Close()

    // Done.

    fmt.Println("DONE")

}

Could you please help me out here.