Terraform and creating a base image in Azure Resource Manager

This blog post was originally stretched out into a few sections. It never came into fruition. Leaving this here as a reference for anyone else trying similar things.

  • Creating and configuring Azure to work with Terraform.
  • Terraforming a Azure resource group using Azure Resource Manager (ARM).
  • Configuring a base image.
  • Using Terraform and Ansible to deploy an infrastructure.

This second section deals with using Terraform and configuring a base image, for future use.

the first terraform apply

The first run of Terraform will be to create the vm_image Resource Group.

Clone the repository if you haven’t already (or create your own):

git clone git@github.com:bonovoxly/terraforms.git
  • Step into the ./terraforms/ARM/vm_image directory:
cd terraforms/ARM/vm_image
  • Source the azure_credentials.sh file (loads Azure variables as environment variables from ~/.azure/credentials).
source ./azure_credentials.sh
  • Run Terraform plan and apply it:
% terraform plan
% terraform apply

It may take a few minutes to create the resource group and all associated objects. Take a look at the Terraform files here.

configure the base VM image

With the vm_image resource group created, the next step is to configure the instance. Get the IP address from the Azure portal.

  • SSH to the instance.
  • Run the following:
% sudo waagent -force -deprovision+user
% exit
  • Configure Azure CLI:
% azure config mode arm
% azure login
  • Shutdown the VM:
% azure vm deallocate -g vmimage -n vmtarget
info:    Executing command vm deallocate
+ Looking up the VM "vmtarget"
+ Deallocating the virtual machine "vmtarget"
info:    vm deallocate command OK
  • Generalize the VM:
% azure vm generalize -g vmimage -n vmtarget
info:    Executing command vm generalize
+ Looking up the VM "vmtarget"
+ Generalizing the virtual machine "vmtarget"
info:    vm generalize command OK