Skip to content

Latest commit

 

History

History
111 lines (66 loc) · 4.41 KB

create-vm-scaleset-network-disks-using-packer-hcl.md

File metadata and controls

111 lines (66 loc) · 4.41 KB
title description ms.topic ms.date ms.custom
Create an Azure virtual machine scale set from a Packer custom image by using Terraform
Learn how to use Terraform to configure and version an Azure virtual machine scale set from a custom image generated by Packer
how-to
07/28/2021
devx-track-terraform

Create an Azure virtual machine scale set from a Packer custom image by using Terraform

Azure virtual machine scale sets allow you to configure identical VMs. The number of VM instances can adjust based on demand or a schedule. For more information, see Automatically scale a virtual machine scale set in the Azure portal.

In this article, you learn how to:

[!div class="checklist"]

  • Set up your Terraform deployment
  • Use variables and outputs for Terraform deployment
  • Create and deploy a network infrastructure
  • Create a custom virtual machine image by using Packer
  • Create and deploy a virtual machine scale set by using the custom image
  • Create and deploy a jumpbox

1. Configure your environment

[!INCLUDE open-source-devops-prereqs-azure-subscription.md]

[!INCLUDE configure-terraform.md]

2. Create a Packer image

  1. Install Packer.

    Key points:

    • To confirm that you have access to the Packer executable, run the following command: packer -v.
    • Depending on your environment, you might need to set your path and reopen the command-line.
  2. Run az group create to create a resource group to hold the Packer image.

    az group create -n myPackerImages -l eastus
    
  3. Run az ad sp create-for-rbac to enable Packer to authenticate to Azure using a service principal.

    az ad sp create-for-rbac --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
    

    Key points:

    • Make note of the output values (appId, client_secret, tenant_id).
  4. Create a Packer template file named ubuntu.json and paste the following code:

    [!code-terraformtarcher-move-sample-code-to-github]

  5. Build the Packer image.

    packer build ubuntu.json

3. Implement the Terraform code

  1. Create a directory in which to test and run the sample Terraform code.

  2. Create your main Terraform configuration file. By convention, the name of this file is main.tf. However, you can specify any valid name for your environment.

  3. Insert the following code into the main Terraform configuration file.

    [!code-terraformtarcher-move-sample-code-to-github]

  4. Create a variables file that will contain the values for Terraform. By convention, the name of this file is variables.tf. However, you can specify any valid name for your environment.

  5. Insert the following code into the variables file.

    [!code-terraformtarcher-move-sample-code-to-github]

    [!INCLUDE variables-terraform-file-key-points.md]

  6. Create a file to specify what values to output when the Terraform plan is applied. By convention, the name of this file is output.tf. However, you can specify any valid name for your environment.

    [!code-terraformtarcher-move-sample-code-to-github]

4. Initialize Terraform

[!INCLUDE terraform-init.md]

5. Create a Terraform execution plan

[!INCLUDE terraform-create-plan.md]

6. Apply a Terraform execution plan

[!INCLUDE terraform-apply-plan.md]

7. Verify the results

8. Clean up resources

[!INCLUDE terraform-destroy-plan.md]

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps

[!div class="nextstepaction"] Learn more about using Terraform in Azure