Terraform - Azure
Steps to Learn and Apply Terraform to Cloud Resources Using an Azure Example
Section titled “Steps to Learn and Apply Terraform to Cloud Resources Using an Azure Example”Summary: Learn about the cloud provider and the resource you want to create. Create a resource manually in the cloud provider. Observe or export the results of the resource(s) created. Read the relevant Terraform provider in the Terraform registry and cloud provider’s documentation on the resource. Use the registry and cloud provider documentation/examples to create a Terraform configuration to recreate the resource.
Applying to Steps to Creating an Azure Shared Dashboard
Section titled “Applying to Steps to Creating an Azure Shared Dashboard”- Learn Fundamentals of Terraform on Azure to learn concepts of Infrastructure as Code (IaC) and how to use Terraform’s declarative syntax to manage Azure resources. Create an example Terraform workflow. Go through the Azure with Terraform documentation.
- Follow Create a dashboard in the Azure portal to manual build a dashboard and its tiles in the Azure portal. A task for use with Terraform later is learning to export the dashboard to JSON.
- Using the Terraform registry: azurermportaldashboard specifications and the Microsoft Azure documentation, start to create the Terraform configuration. Export the JSON of the manually created Azure dashboard. Place the JSON in the Terraform HCL files.
- THe Microsoft.Portal dashboards reference has Bicep, ARM template & Terraform examples. It describes each configuration of the cloud resource that might be seen in the registry and used in the HCL files.
- Create the Terraform project, for example with files
main.tf
- main configurationvariables.tf
- input variables, for example locations, resource groupsproviders.tf
- provider for Terraformoutputs.tf
- output variables
Azure Terraform Example to Create a Resource Group
Section titled “Azure Terraform Example to Create a Resource Group”az login# set to ADDS DEV Azure subscription azrdevod0156wrkaz account set --subscription "my-sub-id"
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/my-subscription-id"Creating 'Contributor' role assignment under scope '/subscriptions/my-subscription-id'
# Need this information for Terraform set upCreating 'Contributor' role assignment under scope '/subscriptions/...'The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli{ "appId": "xxxxxx-xxx-xxxx-xxxx-xxxxxxxxxx", "displayName": "azure-cli-2022-xxxx", "password": "xxxxxx~xxxxxx~xxxxx", "tenant": "xxxxx-xxxx-xxxxx-xxxx-xxxxx"}
# Create a Terraform configuration file in the current directory# hconfig.tf see below
# Initilize the terraform projectterraform init
# Format and validate the configurationterraform fmtterraform validate
# Plan changes and output to a planterraform plan -out=tplan
# Apply the planterraform apply tplan
# Inspect stateterraform show
# See state file and list resourcesterraform state list
# We strongly recommend using the required_providers block to set the# Azure Provider source and version being used# Configure the Azure providerterraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~> 3.0.2" } }
required_version = ">= 1.1.0"}
# Configure the Provider for Microsoft Azureprovider "azurerm" { features {}
subscription_id = "my-subscription-id"}
resource "azurerm_resource_group" "rg" { name = "myterraformtestrg" location = "canadacentral"}
>