Search code examples
packerpacker-builder

hashicorp packer with Azrure ARM: view generated ARM template


I am using packer with azure arm, I am wondering is there a way to see the generated ARM template that is getting deployed?

The reason I am asking is because I get this error, but I want to see the generated ARM template to debug it.

==> azure-arm: unexpected status 400 (400 Bad Request) with error: InvalidTemplateDeployment: The template deployment failed because of policy violation. Please see details for more information


Solution

  • I solved the problem by forking azure-arm plugin of packer, and doing this modification to it and building it back. After running go build you should get an exe file

    • copy it to C:\Users\<user>\AppData\Roaming\packer.d\plugins\github.com\hashicorp\azure and
    • update SHA256 checksum as well Get-FileHash -Path ".\packer-plugin-azure_v2.1.7_x5.0_windows_386.exe"

    This now outputs the generated arm parameter and template.

    diff --git a/builder/azure/arm/template_factory.go b/builder/azure/arm/template_factory.go
    index c06cc37..2e3798b 100644
    --- a/builder/azure/arm/template_factory.go
    +++ b/builder/azure/arm/template_factory.go
    @@ -8,6 +8,8 @@ import (
         "encoding/json"
         "errors"
         "fmt"
    +    "io/ioutil"
    +    "math/rand"
         "time"
     
         hashiVMSDK "github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/virtualmachines"
    @@ -380,6 +382,18 @@ func createDeploymentParameters(doc string, parameters *template.TemplateParamet
             return nil, err
         }
     
    +    suffix := rand.Int()
    +
    +    err = ioutil.WriteFile(fmt.Sprintf("deployment-%d.json", suffix), []byte(doc), 0644)
    +    if err != nil {
    +        fmt.Println("Error writing deployment file:", err)
    +    }
    +
    +    err = ioutil.WriteFile(fmt.Sprintf("parameter-%d.json", suffix), bs, 0644)
    +    if err != nil {
    +        fmt.Println("Error writing parameter file:", err)
    +    }
    +
         return &deployments.Deployment{
             Properties: deployments.DeploymentProperties{
                 Mode:       deployments.DeploymentModeIncremental,