Exercise 2.1: Create Terraform Files
1
Create Terraform Directory
cd $env:USERPROFILE\Desktop
mkdir terraform-demo
cd terraform-demo
2
Create main.tf
notepad main.tf
Add Terraform configuration:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "main" {
name = "rg-terraform-demo"
location = "East US"
tags = {
Environment = "Development"
ManagedBy = "Terraform"
Owner = "DevOps Team"
}
}
Save and close
3
Initialize Terraform
terraform init
Initializing the backend...
Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "~> 3.0"...
- Installing hashicorp/azurerm v3.85.0...
- Installed hashicorp/azurerm v3.85.0
Terraform has been successfully initialized!
4
Login to Azure
az login
You have logged in. Now let us find all the subscriptions to which you have access...
5
Plan Infrastructure Changes
terraform plan
Terraform will perform the following actions:
# azurerm_resource_group.main will be created
+ resource "azurerm_resource_group" "main" {
+ id = (known after apply)
+ location = "eastus"
+ name = "rg-terraform-demo"
+ tags = {
+ "Environment" = "Development"
+ "ManagedBy" = "Terraform"
+ "Owner" = "DevOps Team"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
6
Apply Changes
terraform apply
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
azurerm_resource_group.main: Creating...
azurerm_resource_group.main: Creation complete after 3s
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
7
Verify in Azure Portal
- Open Azure Portal
- Go to Resource Groups
- See
rg-terraform-demo created!
- Check tags are applied