Does a packer image builder creates or uses a network interface and public ip if yes why and when?

88 views Asked by At

I tried to create an image using a sample packer build which was violated by policy network interface cannot have public ip

The error is as below .

" Build 'azure-arm' errored after 1 minute 33 seconds: deployments.DeploymentsClient#Validate: Failure sending request: StatusCode=0 -- Original Error: Code="InvalidTemplateDeployment" Message="The template deployment failed because of policy violation. Please see details for more information." Details=[{"additionalInfo":[{"info":{"evaluationDetails":{"evaluatedExpressions":[{"expression":"type","expressionKind":"Field","expressionValue":"Microsoft.Network/networkInterfaces","operator":"Equals","path":"type","res ult":"True"

please find the packer template as below

{
    "builders": [
      {
        "name": "image",
        "type": "azure-arm",

        "os_disk_size_gb": "256",
        "vm_size": "Standard_F8s_v2",
        "managed_image_storage_account_type": "Standard_LRS",
        
        "client_id": "xxxxxxxxxxxxxxxxxxx",
        "client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
        "tenant_id": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
        "subscription_id": "xxxxxxxxxxxxxxxxxxxxxxxx",
        "object_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

        "managed_image_name": "mypackerimage",
        "managed_image_resource_group_name": "resource_group_name",
        "build_resource_group_name": "rg",

        "virtual_network_name": "VizDev-AzureDevOps-vnet",
        "virtual_network_resource_group_name": "VizDev-AzureDevOps",
        "virtual_network_subnet_name": "default",

        "os_type": "Windows",
        "image_publisher": "MicrosoftWindowsServer",
        "image_offer": "WindowsServer",
        "image_sku": "2022-Datacenter",
        "communicator": "winrm",
        "winrm_use_ssl": "true",
        "winrm_insecure": "true",
        "winrm_username": "packer"

      }
    ],
    "provisioners": [
      {
        "type": "shell",
        "script": "./provisioning-script.sh"
      },
      {
        "type": "powershell",
        "inline": [
          "Write-Output 'Hello, Packer!'"
        ]
      }
    ]
  }
1

There are 1 answers

10
Marcin Słowikowski On

Does a packer image builder creates or uses a network interface and public ip if yes why and when?

It depends on what you have in your configuration file. Yes, but it can be customized. Please check docs:

virtual_network_name (string) - Use a pre-existing virtual network for the VM. This option enables private communication with the VM, no public IP address is used or provisioned (unless you set private_virtual_network_with_public_ip).

You have to specify virtual_network_resource_group_name, virtual_network_name and virtual_network_subnet_name to create VM without public IP.

Example:

packer {
  required_plugins {
    azure = {
      source  = "github.com/hashicorp/azure"
      version = "~> 2"
    }
  }
}

source "azure-arm" "linux_build_agent" {
  use_azure_cli_auth = var.use_azure_cli_auth
  subscription_id = var.subscription_id

  build_resource_group_name         = var.build_resource_group_name
  managed_image_resource_group_name = var.managed_image_resource_group_name
  managed_image_name                = var.managed_image_name


  os_type         = var.os_type
  image_publisher = var.source.image_publisher
  image_offer     = var.source.image_offer
  image_sku       = var.source.image_sku

  vm_size = var.vm_size

  azure_tags = {
    "Deployment Type" = "Packer"
  }

  ssh_username = var.user

  virtual_network_resource_group_name = var.networking.virtual_network_resource_group_name
  virtual_network_name                = var.networking.virtual_network_name
  virtual_network_subnet_name         = var.networking.virtual_network_subnet_name

  shared_gallery_image_version_exclude_from_latest = var.shared_gallery_image_version_exclude_from_latest
  shared_image_gallery_destination {
    subscription         = var.destination.gallery_subscription
    resource_group       = var.destination.gallery_resource_group
    gallery_name         = var.destination.gallery_name
    image_name           = var.destination.gallery_image_name
    image_version        = local.image_version
    replication_regions  = var.destination.gallery_replication_regions
    storage_account_type = var.destination.gallery_storage_account_type
  }
}

build {
  name = "linux"
  sources = [
    "source.azure-arm.linux"
  ]
}