Tinkering with Terraform and Azure to simplify cloud infrastructure setup

Stackify Developer Tips, Tricks & Resources Leave a Comment

I’ve been working with Terraform this week to help setup an Elasticsearch cluster on Azure. Terraform provides an agnostic (somewhat) means of defining infrastructure components locally and for cloud deployments (Azure, AWS, Google, etc. [https://www.terraform.io/docs/providers/index.html]).

Its pretty simple to get going; download and update your path: https://www.terraform.io/intro/getting-started/install.html.

Create a directory and drop in a sample.tf file wth a a virtual network component, the only prerequisite is a resource group.

 

provider "azurerm" {

subscription_id = "..."

client_id = "..."

client_secret = "..."

tenant_id = "..."

}

resource "azurerm_virtual_network" "sample" {

name = "sample-virtual-network"

resource_group_name = "sample-resource-group"

address_space = [

   "10.0.0.0/16"]

location = "centralus"

}

 

Run the command terraform plan to see what will be created:

 

image-1


 

Run command terraform apply:

 

image-2


 

Logging into the Azure portal you can see the created resource:

 

image-3


 

Terraform keeps track of state, calling terraform apply again will detect that the resource exists and make no adjustments.

Now this is where the agnostic part somewhat falls apart. Since all the cloud providers don’t have a one-to-one equivalency, you end up making vendor specific configurations. I imagine that converting from an Azure Terraform config to AWS is still easier than converting an Azure ARM template to AWS Cloudformation.

I’ve had to make a few adjustments to get the Elasticsearch cluster setup just the way I wanted it. One hurdle was how azure permissions are validated by terraform, requiring more privileges than I felt necessary [https://github.com/hashicorp/terraform/issues/8824]. I may followup up with a pull request if I can code a patch in Go and not embarrass myself.

Another roadblock was the lack of a loadbalancer resource. Luckily a branch exists out there so you can manually patch and build if this a requirement for your project. [https://github.com/hashicorp/terraform/pull/6429]. Hopefully this can get merged into the main project soon.

 

darin-1

 

 

Darin Howard

Java Engineer / Stackify