This blog provides some basic examples of templated infrastructure for Microsoft Azure and how to deploy said infrastructure into Azure using their CLI/API authentication processes.
Terraform and Microsoft Azure
This basic compute demo will consist of the orchestration of blank Basic_A1 Azure instances into Microsoft Azure.
Pre-requisites:
- It is assumed that this demo is being run from a UNIX based machine and that basic existing experience with UNIX based systems has been acquired
- You have a Microsoft Azure account – Sign up here
- You have Azure API authentication credentials for your account stored locally – Download them here
- This repository cloned locally (install Git here)
- You have Terraform installed (install it here)
Instructions:
- From the command-line, change directory into:
{location_of_this_repo}/azure/environments/{environment_you_wish_to_build}/compute
- In this directory, open ‘terraform.tfvars‘ with your editor of choice and enter a value for variable ‘ssh_username‘, ‘ssh_password‘, ‘storage_name‘ and save the file
- A file with all of the populated variables will look something similar to the code below:
# Demo Node # demo_node_image = “Ubuntu Server 14.04 LTS” demo_node_instance_size = “Basic_A1” demo_node_instance_count = “1” demo_node_instance_location = “North Europe” demo_node_ssh_username = “example_username” demo_node_ssh_password = “example_password” demo_node_storage_name = “example_storage_name”
- The storage_name variable is the name of the storage service to attach to the instance, it is blob storage service in Azure that serves as the backend to the instance. If you don’t already have a storage service to use, a storage service can be built using this repository by making a module declaration within an environment for the “storage_service” module at “./modules/compute/storage/storage_service” and pass the appropriate variables in the “terraform.tfvars” file of the specified environment. It’s important to note that each storage service requires a unique name.
- From the command-line, run the command
export AZURE_PUBLISH_SETTINGS=$(cat {path_to_the_publish_settings_downloaded_in_prereqs})`
- This command is exporting the credentials that will be used to authenticate with the Azure API as environment variables, ready to be used by Terraform
- Then run the command
terraform get
- This command will pull all of the modules declared within the current environment into the current environment’s directory from the modules directory (they can be seen in the.terraform directory of the current directory)
- Then run the command
terraform plan -var-file=./terraform.tfvars
- This command will demonstrate what Terraform is about to deploy into Azure, also demonstrating any existing resources that may be affected (if there are any) by the upcoming terraform apply. The variable file to be used has been explicitly stated pointed to using the path, however it is not required if the command is ran from within the same directory as the variable file.
- Then run the command
terraform apply -state=./terraform.tfstate -var-file=./terraform.tfvars
- This command will build all of the templated infrastructure that was shown in the terraform plan above. Again, the explicit paths to the variable and state files are not required if the command is ran from the same directory, this is to provide an example of how the command could be ran from a different directory should it be required.
- To destroy the infrastructure you have built, run command
terraform destroy -state=./terraform.tfstate -var-file=./terraform.tfvars
- Be careful, running this command will destroy all of the resources you have built into Azure!