Skip to content

Latest commit

 

History

History
140 lines (85 loc) · 5.34 KB

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

File metadata and controls

140 lines (85 loc) · 5.34 KB
title description ms.topic service ms.service 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
virtual-machine-scale-sets
azure-virtual-machine-scale-sets
10/26/2023
devx-track-terraform, linux-related-content

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

[!INCLUDE Terraform abstract]

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 --role Contributor --scopes /subscriptions/<subscription_id> --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
    

    Key points:

    • Make note of the output values (appId, client_secret, tenant_id).
  4. Run az account show to display the current Azure subscription.

    az account show --query "{ subscription_id: id }"
    
  5. Create a Packer template variables file named ubuntu.pkr.hcl and insert the following code. Update the highlighted lines with your service principal and Azure subscription information.

    :::code language="Terraform" source="../../terraform_samples/quickstart/201-vmss-packer-jumpbox/ubuntu.pkr.hcl" highlight="12,16,21,26":::

    Key points:

    • Set the client_id, client_secret, and tenant_id fields to the respective values from your service principal.
    • Set the subscription_id field to your Azure subscription ID.
  6. Build the Packer image.

    packer build ubuntu.json

3. Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named main.tf and insert the following code:

    :::code language="Terraform" source="../../terraform_samples/quickstart/201-vmss-packer-jumpbox/main.tf":::

  3. Create a file named variables.tf to contain the project variables and insert the following code:

    :::code language="Terraform" source="../../terraform_samples/quickstart/201-vmss-packer-jumpbox/variables.tf":::

  4. Create a file named output.tf to specify what values Terraform displays and insert the following code:

    :::code language="Terraform" source="../../terraform_samples/quickstart/201-vmss-packer-jumpbox/output.tf":::

4. Initialize Terraform

[!INCLUDE terraform-init.md]

5. Create a Terraform execution plan

[!INCLUDE terraform-plan.md]

6. Apply a Terraform execution plan

[!INCLUDE terraform-apply-plan.md]

7. Verify the results

  1. From the output of the terraform apply command, you see values for the following:

    • Virtual machine FQDN
    • Jumpbox FQDN
    • Jumpbox IP address
  2. Browse to the virtual machine URL to confirm a default page with the text Welcome to nginx!.

  3. Use SSH to connect to the jumpbox VM using the user name defined in the variables file and the password you specified when you ran terraform apply. For example: ssh azureuser@<ip_address>.

8. Clean up resources

Delete virtual machine scale set

[!INCLUDE terraform-plan-destroy.md]

Delete Packer image and resource group

Run az group delete to delete the resource group used to contain the Packer image. The Packer image is also deleted.

az group delete --name myPackerImages --yes

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps

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