Setting Up Terraform

Posted on
terraform azure

Intro

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Learn more

Basically, with Terraform you can define what you’d like like this:

server {
    os = windows
    version = Windows_10
    memory = 16
    CPU = i7
}

This allows you to manage your Infrastructure as code since now you can commit infra changes to a repository and have a CI/CD pipeline to build/test any infra changes, with approval flows.

All cloud providers like Azure, AWS, GCP have terraform modules; meaning you can deploy cloud resources in these respective clouds with pre-built modules.

Install

Download the executable if you’re on Windows, or use Brew for mac.

On Windows, be sure to add your executable location to the path environment variable.

After download, you should be able to see this:

> terraform --version
Terraform v0.12.24

Your version of Terraform is out of date! The latest version
is 0.13.2. You can update by downloading from https://www.terraform.io/downloads.html

Azure Setup

To set up Terraform for Azure, you need to create an Application in Azure AD:

  • Log into Azure
  • Go to Azure Active Directory -> App Registrations
  • New Registration
    • Give it a name, then click register
  • After that, you will get an Application (cliend) ID. Save this.
  • Go to the new app registration -> Certificates and Secrets -> New Client Secret. Save this.

Add details to your environment variables

Windows:

  • This PC -> Properties -> Advanced System Settings -> Environment Variables
  • Add a varibale called CLIENT_SECRET: Your Azure Client Secret

In your terrafrom file (usually main.tf), set up the Azure provider, and optionally a remote backend:

terraform {
  backend "azurerm" {
    resource_group_name  = "your_rg_name"
    storage_account_name = "storage_acct_name"
    container_name       = "blob_container_name"
    key                  = "file_name.tfstate"
  }
}

#Azure provider setup
provider "azurerm" {

  version = ">=2.0.0"
  features {}
}

More details on how to set up Azure provider here.

AWS Setup

Follow this doc.