github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/website/source/docs/builders/azure.html.md (about)

     1  ---
     2  description: |
     3  
     4  layout: docs
     5  page_title: Azure Resource Manager
     6  ...
     7  
     8  # Azure Resource Manager Builder
     9  
    10  Type: `azure-arm`
    11  
    12  Packer supports building VHDs in [Azure Resource Manager](https://azure.microsoft.com/en-us/documentation/articles/resource-group-overview/). Azure provides new users a [$200 credit for the first 30 days](https://azure.microsoft.com/en-us/free/); after which you will incur costs for VMs built and stored using Packer.
    13  
    14  Unlike most Packer builders, the artifact produced by the ARM builder is a VHD (virtual hard disk), not a full virtual machine image. This means you will need to [perform some additional steps](https://github.com/Azure/packer-azure/issues/201) in order to launch a VM from your build artifact.
    15  
    16  Azure uses a combination of OAuth and Active Directory to authorize requests to the ARM API. Learn how to [authorize access to ARM](/docs/builders/azure-setup.html).
    17  
    18  The documentation below references command output from the [Azure CLI](https://azure.microsoft.com/en-us/documentation/articles/xplat-cli-install/).
    19  
    20  ## Configuration Reference
    21  
    22  The following configuration options are available for building Azure images. In addition to the options listed here, a
    23  [communicator](/docs/templates/communicator.html) can be configured for this
    24  builder.
    25  
    26  ### Required:
    27  
    28  -   `client_id` (string) The Active Directory service principal associated with your builder.
    29  
    30  -   `client_secret` (string) The password or secret for your service principal.
    31  
    32  -   `resource_group_name` (string) Resource group under which the final artifact will be stored.
    33  
    34  -   `storage_account` (string) Storage account under which the final artifact will be stored.
    35  
    36  -   `subscription_id` (string) Subscription under which the build will be performed. **The service principal specified in `client_id` must have full access to this subscription.**
    37  
    38  -   `capture_container_name` (string) Destination container name. Essentially the "folder" where your VHD will be organized in Azure.
    39  
    40  -   `capture_name_prefix` (string) VHD prefix. The final artifacts will be named `PREFIX-osDisk.UUID` and `PREFIX-vmTemplate.UUID`.
    41  
    42  -   `image_publisher` (string) PublisherName for your base image. See [documentation](https://azure.microsoft.com/en-us/documentation/articles/resource-groups-vm-searching/) for details.
    43  
    44      CLI example `azure vm image list-publishers -l westus`
    45  
    46  -   `image_offer` (string) Offer for your base image. See [documentation](https://azure.microsoft.com/en-us/documentation/articles/resource-groups-vm-searching/) for details.
    47  
    48      CLI example `azure vm image list-offers -l westus -p Canonical`
    49  
    50  -   `image_sku` (string) SKU for your base image. See [documentation](https://azure.microsoft.com/en-us/documentation/articles/resource-groups-vm-searching/) for details.
    51  
    52      CLI example `azure vm image list-skus -l westus -p Canonical -o UbuntuServer`
    53  
    54  -   `location` (string) Azure datacenter in which your VM will build.
    55  
    56      CLI example `azure location list`
    57  
    58  ### Optional:
    59  
    60  -   `azure_tags` (object of name/value strings) - the user can define up to 15 tags.  Tag names cannot exceed 512 
    61      characters, and tag values cannot exceed 256 characters.  Tags are applied to every resource deployed by a Packer
    62      build, i.e. Resource Group, VM, NIC, VNET, Public IP, KeyVault, etc.
    63  
    64  -   `cloud_environment_name` (string) One of `Public`, `China`, `Germany`, or
    65      `USGovernment`. Defaults to `Public`. Long forms such as
    66      `USGovernmentCloud` and `AzureUSGovernmentCloud` are also supported.
    67  
    68  -   `image_version` (string) Specify a specific version of an OS to boot from. Defaults to `latest`.  There may be a
    69       difference in versions available across regions due to image synchronization latency.  To ensure a consistent
    70       version across regions set this value to one that is available in all regions where you are deploying.
    71  
    72      CLI example `azure vm image list -l westus -p Canonical -o UbuntuServer -k 16.04.0-LTS`
    73  
    74  -   `image_url` (string) Specify a custom VHD to use.  If this value is set, do not set image_publisher, image_offer,
    75       image_sku, or image_version.
    76  
    77  -   `tenant_id` (string) The account identifier with which your `client_id` and `subscription_id` are associated. If not
    78       specified, `tenant_id` will be looked up using `subscription_id`.
    79  
    80  -   `object_id` (string) Specify an OAuth Object ID to protect WinRM certificates
    81      created at runtime.  This variable is required when creating images based on
    82      Windows; this variable is not used by non-Windows builds.  See `Windows`
    83      behavior for `os_type`, below.
    84  
    85  -   `os_type` (string) If either `Linux` or `Windows` is specified Packer will
    86      automatically configure authentication credentials for the provisioned machine. For
    87      `Linux` this configures an SSH authorized key. For `Windows` this
    88      configures a WinRM certificate.
    89      
    90  -   `os_disk_size_gb` (int32) Specify the size of the OS disk in GB (gigabytes).  Values of zero or less than zero are 
    91      ignored.
    92  
    93  -   `virtual_network_name` (string) Use a pre-existing virtual network for the VM.  This option enables private
    94      communication with the VM, no public IP address is **used** or **provisioned**.  This value should only be set if
    95      Packer is executed from a host on the same subnet / virtual network.
    96  
    97  -   `virtual_network_resource_group_name` (string) If virtual_network_name is set, this value **may** also be set.  If
    98      virtual_network_name is set, and this value is not set the builder attempts to determine the resource group
    99      containing the virtual network.  If the resource group cannot be found, or it cannot be disambiguated, this value
   100      should be set.
   101  
   102  -   `virtual_network_subnet_name` (string) If virtual_network_name is set, this value **may** also be set.  If
   103       virtual_network_name is set, and this value is not set the builder attempts to determine the subnet to use with
   104       the virtual network.  If the subnet cannot be found, or it cannot be disambiguated, this value should be set.
   105  
   106  -   `vm_size` (string) Size of the VM used for building. This can be changed
   107      when you deploy a VM from your VHD. See
   108      [pricing](https://azure.microsoft.com/en-us/pricing/details/virtual-machines/) information. Defaults to `Standard_A1`.
   109  
   110      CLI example `azure vm sizes -l westus`
   111  
   112  
   113  ## Basic Example
   114  
   115  Here is a basic example for Azure.
   116  
   117  ``` {.javascript}
   118  {
   119      "type": "azure-arm",
   120  
   121      "client_id": "fe354398-d7sf-4dc9-87fd-c432cd8a7e09",
   122      "client_secret": "keepitsecret&#*$",
   123      "resource_group_name": "packerdemo",
   124      "storage_account": "virtualmachines",
   125      "subscription_id": "44cae533-4247-4093-42cf-897ded6e7823",
   126      "tenant_id": "de39842a-caba-497e-a798-7896aea43218",
   127  
   128      "capture_container_name": "images",
   129      "capture_name_prefix": "packer",
   130  
   131      "os_type": "Linux",
   132      "image_publisher": "Canonical",
   133      "image_offer": "UbuntuServer",
   134      "image_sku": "14.04.4-LTS",
   135      
   136      "azure_tags": {
   137        "dept": "engineering"
   138      },
   139  
   140      "location": "West US",
   141      "vm_size": "Standard_A2"
   142  }
   143  ```
   144  
   145  ## Implementation
   146  
   147  \~> **Warning!** This is an advanced topic. You do not need to understand the implementation to use the Azure
   148  builder.
   149  
   150  The Azure builder uses ARM
   151  [templates](https://azure.microsoft.com/en-us/documentation/articles/resource-group-authoring-templates/) to deploy
   152  resources.  ARM templates make it easy to express the what without having to express the how.
   153  
   154  The Azure builder works under the assumption that it creates everything it needs to execute a build.  When the build has
   155  completed it simply deletes the resource group to cleanup any runtime resources.  Resource groups are named using the
   156  form `packer-Resource-Group-<random>`. The value `<random>` is a random value that is generated at every invocation of
   157  packer.  The `<random>` value is re-used as much as possible when naming resources, so users can better identify and
   158  group these transient resources when seen in their subscription.
   159  
   160   > The VHD is created on a user specified storage account, not a random one created at runtime.  When a virtual machine
   161   is captured the resulting VHD is stored on the same storage account as the source VHD.  The VHD created by Packer must
   162   persist after a build is complete, which is why the storage account is set by the user.
   163  
   164  The basic steps for a build are:
   165  
   166   1. Create a resource group.
   167   1. Validate and deploy a VM template.
   168   1. Execute provision - defined by the user; typically shell commands.
   169   1. Power off and capture the VM.
   170   1. Delete the resource group.
   171   1. Delete the temporary VM's OS disk.
   172  
   173  The templates used for a build are currently fixed in the code.  There is a template for Linux, Windows, and KeyVault.
   174  The templates are themselves templated with place holders for names, passwords, SSH keys, certificates, etc.
   175  
   176  ### What's Randomized?
   177  
   178  The Azure builder creates the following random values at runtime.
   179  
   180   * Administrator Password: a random 32-character value using the *password alphabet*.
   181   * Certificate: a 2,048-bit certificate used to secure WinRM communication.  The certificate is valid for 24-hours, which starts roughly at invocation time.
   182   * Certificate Password: a random 32-character value using the *password alphabet* used to protect the private key of the certificate.
   183   * Compute Name: a random 15-character name prefixed with pkrvm; the name of the VM.
   184   * Deployment Name: a random 15-character name prefixed with pkfdp; the name of the deployment.
   185   * KeyVault Name: a random 15-character name prefixed with pkrkv.
   186   * OS Disk Name: a random 15-character name prefixed with pkros.
   187   * Resource Group Name: a random 33-character name prefixed with packer-Resource-Group-.
   188   * SSH Key Pair: a 2,048-bit asymmetric key pair; can be overriden by the user.
   189  
   190  The default alphabet used for random values is **0123456789bcdfghjklmnpqrstvwxyz**.  The alphabet was reduced (no
   191  vowels) to prevent running afoul of Azure decency controls.
   192  
   193  The password alphabet used for random values is **0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ**.
   194  
   195  ### Windows
   196  
   197  The Windows implementation is very similar to the Linux build, with the exception that it deploys a template to
   198  configure KeyVault. Packer communicates with a Windows VM using the WinRM protocol.  Windows VMs on Azure default to
   199  using both password and certificate based authentication for WinRM.  The password is easily set via the VM ARM template,
   200  but the certificate requires an intermediary. The intermediary for Azure is KeyVault.  The certificate is uploaded to a
   201  new KeyVault provisioned in the same resource group as the VM.  When the Windows VM is deployed, it links to the
   202  certificate in KeyVault, and Azure will ensure the certificate is injected as part of deployment.
   203  
   204  The basic steps for a Windows build are:
   205  
   206    1. Create a resource group.
   207    1. Validate and deploy a KeyVault template.
   208    1. Validate and deploy a VM template.
   209    1. Execute provision - defined by the user; typically shell commands.
   210    1. Power off and capture the VM.
   211    1. Delete the resource group.
   212    1. Delete the temporary VM's OS disk.
   213  
   214  A Windows build requires two templates and two deployments.  Unfortunately, the KeyVault and VM cannot be deployed at
   215  the same time hence the need for two templates and deployments.  The time required to deploy a KeyVault template is
   216  minimal, so overall impact is small.
   217  
   218   > The KeyVault certificate is protected using the object_id of the SPN.  This is why Windows builds require object_id,
   219   and an SPN.  The KeyVault is deleted when the resource group is deleted.
   220  
   221  See the [examples/azure](https://github.com/mitchellh/packer/tree/master/examples/azure) folder in the packer project
   222  for more examples.