Terraform
Jarvis Mishler

Jarvis Mishler

HATech Academy – Terraform 101

Introduction

Here is a link to our presentation:
https://hatech.io/download/hatech-academy-intro-to-terraform

For a great introduction to Terraform in Microsoft Azure, refer to the following tutorial:
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm

Read on for a more detailed introduction of the basic Terraform concepts.

The following labs reference the Terraform-Azure-101 directory you can find in the following repo:
https://github.com/hatech/academy-training

Configuring the Terraform Provider

Directions can be found here:
https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-install-configure

1. Run the following in Cloud Shell and record SubscriptionID and TenantID

az account show --query "{subscriptionId:id, tenantId:tenantId}"

2. Run the following command in Cloud Shell and record the ApplicationID and Password

az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}" 

3. Copy provider.template to provider.tf and replace the following values with yours.

	subscription_id = [SUBSCRIPTION_ID]
	client_id = [APPLICATION_ID] 
	client_secret = [CLIENT_SECRET] 
	tenant_id = [TENANT_ID]

Creating a Terraform Resource

1. Create and open a new file

nano main.tf

2. Create a terraform resource for a Resource Group

resource "azurerm_resource_group" "demo-group" {
name = "demo-resource-group"
location = "eastus"
}

3. Create a terraform resource for a Storage Account

resource "azurerm_storage_account" "demo-account" {
name = "hatechdemostorage"
resource_group_name = "demo-resource-group"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
depends_on = ["azurerm_resource_group.demo-group"]
}

Creating a Terraform Resource using Variables

1. Create variables file

nano variables.tf

2. Create a variable for Account Name

variable "account_name" {
description = "name for the storage account"
default = "hatechdemostorage"
}

3. Create a terraform resource using your variables

resource "azurerm_resource_group" "demo-group" {
name = "demo-resource-group"
location = "eastus"
}

resource "azurerm_storage_account" "demo-account" {
name = "${var.account_name}"
resource_group_name = "${azurerm_resource_group.demo-group.name}"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
depends_on = ["azurerm_resource_group.demo-group"]
}

Creating a Terraform Resource using Modules

1. Create two module directories

mkdir tf_resource_group
mkdir tf_storage_account

2. Create storage_account.tf wrapper file

module "storage_account_1" {
source = "./tf_storage_account"
resource_group = "${module.resource_group.group_name}"
account_name = "hatechstorageaccount01"
}
module "storage_account_2" {
source = "./tf_storage_account"
resource_group = "${module.resource_group.group_name}"
account_name = "hatechstorageaccount02"
}

3. Create resource_group.tf wrapper file

module "resource_group" {
source = "./tf_resource_group"
}

4. Create main.tf in “tf_resource_group”

resource "azurerm_resource_group" "demo-group" {
name = "demo-resource-group"
location = "eastus"
}
output "group_name" {
value = "${azurerm_resource_group.demo-group.name}"
}

5. Create variables.tf in  “tf_storage_account”

variable "account_name" {}
variable "resource_group" {}

6. Create main.tf in  “tf_storage_account”

resource "azurerm_storage_account" "demo-account" {
name = "${var.account_name}"
resource_group_name = "${var.resource_group}"
location = "eastus"
account_tier = "Standard"
account_replication_type = "LRS"
}

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

You must be logged in to post a comment.