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 |
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
[!INCLUDE open-source-devops-prereqs-azure-subscription.md]
[!INCLUDE configure-terraform.md]
-
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.
- To confirm that you have access to the Packer executable, run the following command:
-
Run az group create to create a resource group to hold the Packer image.
az group create -n myPackerImages -l eastus
-
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
).
- Make note of the output values (
-
Create a Packer template file named
ubuntu.json
and paste the following code:[!code-terraformtarcher-move-sample-code-to-github]
-
Build the Packer image.
packer build ubuntu.json
-
Create a directory in which to test and run the sample Terraform code.
-
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. -
Insert the following code into the main Terraform configuration file.
[!code-terraformtarcher-move-sample-code-to-github]
-
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. -
Insert the following code into the variables file.
[!code-terraformtarcher-move-sample-code-to-github]
[!INCLUDE variables-terraform-file-key-points.md]
-
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]
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-create-plan.md]
[!INCLUDE terraform-apply-plan.md]
[!INCLUDE terraform-destroy-plan.md]
Troubleshoot common problems when using Terraform on Azure
[!div class="nextstepaction"] Learn more about using Terraform in Azure