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 |
[!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
[!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 --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
).
- Make note of the output values (
-
Run az account show to display the current Azure subscription.
az account show --query "{ subscription_id: id }"
-
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
, andtenant_id
fields to the respective values from your service principal. - Set the
subscription_id
field to your Azure subscription ID.
- Set the
-
Build the Packer image.
packer build ubuntu.json
-
Create a directory in which to test the sample Terraform code and make it the current directory.
-
Create a file named
main.tf
and insert the following code::::code language="Terraform" source="../../terraform_samples/quickstart/201-vmss-packer-jumpbox/main.tf":::
-
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":::
-
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":::
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-plan.md]
[!INCLUDE terraform-apply-plan.md]
-
From the output of the
terraform apply
command, you see values for the following:- Virtual machine FQDN
- Jumpbox FQDN
- Jumpbox IP address
-
Browse to the virtual machine URL to confirm a default page with the text Welcome to nginx!.
-
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>
.
[!INCLUDE terraform-plan-destroy.md]
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 common problems when using Terraform on Azure
[!div class="nextstepaction"] Learn more about using Terraform in Azure