In this post we will see how to create root and other code with help of bash script:
vi script.sh
Pest the code in that file:
#!/bin/bash
# Create directory structure
mkdir -p tfmodule-count/modules/rg
cd tfmodule-count || exit
echo "📁 Creating Terraform files..."
# -------------------------
# main.tf (uses count)
# -------------------------
cat <<EOF > main.tf
provider "azurerm" {
features {}
}
variable "rg_names" {
type = list(string)
default = ["rg-dev", "rg-test", "rg-prod"]
}
variable "location" {
default = "eastus"
}
module "rg" {
source = "./modules/rg"
count = length(var.rg_names)
rg_name = var.rg_names[count.index]
rg_location = var.location
}
output "rg_names_output" {
value = [for m in module.rg : m.rg_output.name]
}
EOF
# -------------------------
# modules/rg/rg.tf
# -------------------------
cat <<EOF > modules/rg/rg.tf
resource "azurerm_resource_group" "rg" {
name = var.rg_name
location = var.rg_location
tags = {
environment = "Dev"
}
}
EOF
# -------------------------
# modules/rg/variable.tf
# -------------------------
cat <<EOF > modules/rg/variable.tf
variable "rg_name" {
type = string
}
variable "rg_location" {
type = string
}
EOF
# -------------------------
# modules/rg/output.tf
# -------------------------
cat <<EOF > modules/rg/output.tf
output "rg_output" {
value = azurerm_resource_group.rg
}
EOF
echo "✅ Terraform module with count created successfully!"
echo ""
echo "👉 Next steps:"
echo "cd tfmodule-count"
echo "terraform init"
echo "terraform plan"
echo "terraform apply"
▶️ How to Run
chmod +x setup.sh
./setup.sh📁 What You Get
- Root module with
count- Reusable module (
modules/rg)- 3 Azure Resource Groups created:
- rg-dev
- rg-test
- rg-prod
🎯 After Setup
Run:
terraform init
terraform plan
terraform apply
Contact EduArn.com for Retail and corporate training: www.eduarn.com
No comments:
Post a Comment