Skip to content

AzureCode03-InfraSetup - To setup RG, Azure Key Vault, Vnet, Subnet, NIC, NSG, Public IP, VM, SQL Server, SQL Database, by dynamically providing user-prefix for the resources at run time.

Notifications You must be signed in to change notification settings

ShrutiCloudDevOpsNinja/AzureCode03-InfraSetup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AzureCode03-InfraSetup

AzureCode03-InfraSetup - To setup RG, Azure Key Vault, Vnet, Subnet, NIC, NSG, Public IP, VM, SQL Server, SQL Database, by dynamically providing user-prefix for the resources.

Summary

Following resources are created on the Azure Colud environment by executing the code.

  • Resource Group (for Key Vault)
    • Key Vault
      • SQL Username
      • SQL Password (User Input)
      • VM Username
      • VM Password (User Input)
  • Resource Group (User Input for suffix)
    • Virtual Network (VNet)
    • Subnet
    • NSG
    • Public IP
    • NIC
    • NIC-NSG association
    • Virtual Machine
    • SQL Server
    • SQL Database

Pre-Requisite

⚠️ Your user needs to have "Key Vault Administrator" rights βœ… in the subscription.

Step 01 - Update backend configuration in the below mentioned file.

βœ… Update the values to be used to backend configuration.

⚠️ This is a pre-requisite to execute the code.

❌ If the below resources are not present in the Azure Cloud, the terraform code will ❌ FAIL.

πŸ“ .\INFRA01\backend-config-infra01.hcl

---------------------------------------------------------------------------------------------------
# βœ… Update the values to be used to backend configuration.
# ⚠️ This is pre-requisite to execute the code.
# ❌ If the below resources are not present in the Azure Cloud, the terraform code will ❌ FAIL.

resource_group_name  = <ENTER backend - Resource Group>          # Example: "rg-backend"
storage_account_name = <ENTER backend - Storage Account>         # Example: "storageaccount"
container_name       = <ENTER backend - Storage Container Name>  # Example: "storagecontainer"
key                  = <ENTER backend - State File Name>         # Example: "a.terraform.tfstate"

Step 02 - Update azure subscription id in the below mentioned file.

βœ… Update this with your own subscription ID.

πŸ“ infra01.auto.tfvars

---------------------------------------------------------------------------------------------------
# βœ… Update this variable with your own subscription ID.
main_provider_subscription_id = "00000000-0000-0000-000000000000"

Step 03 - Update variables in the below mentioned file.

All variables like Resource Group Location, VNet Address, Subnet Address needs to be updated.

πŸ“ infra01.auto.tfvars

---------------------------------------------------------------------------------------------------
# βœ… Update this variable with your own Azure region.
root_resource_location        = "France Central"

# βœ… Update this variable with your own VNet address space.
root_vnet_address_space       = ["46.87.0.0/24"]

# βœ… Update this variable with your own subnet address prefixes.
root_subnet_address_prefixes  = ["46.87.0.0/28"]

# βœ… Update this with your own Username for VM
root_virtual_machine_username = "VMUser1"  # πŸ“Œ This will be the username to access your VM.

# βœ… Update this with your own Username for SQL Server
root_sql_server_username      = "SQLUser1" # πŸ“Œ This will be the username to access your SQL Server.

Step 04 - Following command needs to be executed to initialize the backend.

πŸ’‘ Run with -backend-config parameter, else the initialize will ask for user input.

terraform init -backend-config='backend-config-infra01.hcl'
OR
terraform init -backend-config='backend-config-infra01.hcl' -upgrade

Step 05 - Following commands can be used to proceed with infrastructure creation.

terraform validate
terraform plan

Step 06 - ❌ DO Not run terraform apply with auto-approve as user input is needed for user prefix.

βœ… Only run terrafom apply, as user input is need for dynamically creating resource names.

terraform apply

# ❌ Do Not run terraform apply -auto-approve

Execution

User Input 1 - On running 'terraform apply', user is asked to provide SQL Server Password.

User is allowed to enter 8-12 alphanumeric characters and allowed special characters: @ !

PS D:\TerraformCode\AzureCode01-InfraSetup\INFRA01> terraform apply

Acquiring state lock. This may take a few moments...

var.root_key_vault_secret_sql_password_value

  ###########################################################################################
   Enter password for : SQL-Username
   Enter 8 to 12 character alphanumeric Secret.

   βœ… Example Valid Inputs:
   >> Eagle98!   βœ… (valid, 8-12 characters)
   >> Lion@12345 βœ… (valid, 8-12 characters)
   >> βœ… Allowed special characters: @ !

   ❌ Invalid Inputs:
   >> abc123        ❌ (less than 8 characters)
   >> abcijklmngh98 ❌ (too long, more than 12 characters)

  Enter a value: SQL@12345  # βœ… User enters 8-12 character alphanumeric value

User Input 2 - After providing 1st input, user is asked to provide VM Password.

User is allowed to enter 8-12 alphanumeric characters and allowed special characters: @ !

var.root_key_vault_secret_vm_password_value

  ###########################################################################################
   Enter password for : VM-Username
   Enter 8 to 12 character alphanumeric Secret.

   βœ… Example Valid Inputs:
   >> Eagle98!   βœ… (valid, 8-12 characters)
   >> Lion@12345 βœ… (valid, 8-12 characters)
   >> βœ… Allowed special characters: @ !

   ❌ Invalid Inputs:
   >> abc123        ❌ (less than 8 characters)
   >> abcijklmngh98 ❌ (too long, more than 12 characters)

  Enter a value: VM@12345  # βœ… User enters 8-12 character alphanumeric value

User Input 3 - After providing 2nd input, user is asked to provide Resource Prefix to be used to resource names.

User is allowed to enter 8 alphanumeric characters.

var.user_prefix

  ###########################################################################################
   Enter an 8-character alphanumeric prefix for the resource names.

   βœ… Example Valid Inputs:
   >> eagle987 βœ… (valid)
   >> lion1234 βœ… (valid)

   ❌ Invalid Inputs:
   >> 123abcdz  ❌ (starts with a number)
   >> abc123    ❌ (less than 8 characters)
   >> abcdefgh9 ❌ (too long)

  Enter a value: fire0000  # βœ… User enters 8-12 character alphanumeric value

Output

πŸ“€ Output values displayed to user after successful execution of the code.

Apply complete! Resources: 15 added, 0 changed, 0 destroyed.

Outputs:

root_output_key_vault_name                = "Key Vault Name: KeyVault-fire0000-M1I4K"    # πŸ”‘ Key Vault Name
root_output_public_ip                     = "VM Public IP: 51.xxx.xxx.xx"                # 🌐 For information purpose only.
root_output_resource_group_name           = "Resource Group Name: rg-fire0000"           # πŸ“¦ Resource group created with 'rg-' as prefix and user input as suffix.
root_output_resource_group_name_key_vault = "Key Vault Resource Group Name: rg-fire0000-KeyVault-UgOtWb0o"
root_output_sql_server_username           = "SQL Server: Username: SQLUser1"             # πŸ” SQL Username
root_output_virtual_machine               = "Virtual Machine Name: fire0000-VM"          # πŸ“¦ Virtual Machine user input as prefix and '-VM' as suffix.
root_output_virtual_machine_username      = "SQL Server: Username: VMUser1"              # πŸ” VM Username

🧠 Troubleshooting errors

Error⚑ Provisioning is restricted in this region. Please choose a different region.

Error: creating Server (Subscription: "00000000-0000-0000-0000-000000000000"
β”‚ Resource Group Name: "rg-tommy123"
β”‚ Server Name: "tommy123-sqlserver"): polling after CreateOrUpdate: polling failed: the Azure API returned the following error:
β”‚
β”‚ Status: "ProvisioningDisabled"
β”‚ Code: ""
β”‚ Message: "Provisioning is restricted in this region. Please choose a different region. For exceptions to this rule please open a support request with Issue type of 'Service and subscription limits'. See https://docs.microsoft.com/en-us/azure/sql-database/quota-increase-request for more details."
β”‚ Activity Id: ""
β”‚ ---
β”‚ API Response:
β”‚ ----[start]----
β”‚ {"name":"00000000-0000-0000-ae00-00000000","status":"Failed","startTime":"2025-06-19T03:03:53.143Z","error":{"code":"ProvisioningDisabled","message":"Provisioning is restricted in this region. Please choose a different region. For exceptions to this rule please open a support request with Issue type of 'Service and subscription limits'. See https://docs.microsoft.com/en-us/azure/sql-database/quota-increase-request for more details."}}
β”‚ -----[end]-----
β”‚   with module.module_sql_sqerver.azurerm_mssql_server.child_sqlserver,
β”‚   on ..\modules\10-azurerm_mssql_server\main.tf line 1, in resource "azurerm_mssql_server" "child_sqlserver":
β”‚    1: resource "azurerm_mssql_server" "child_sqlserver" {

πŸ”§ In case you face above mentioned error, change the "root_resource_location" to "France Central" or any other available resource location.

πŸ“ infra01.auto.tfvars

---------------------------------------------------------------------------------------------------
# βœ… Update this variable with your own Azure region.
root_resource_location        = "France Central"

Error⚑ Error: External Program Execution Failed

β”‚ Error: External Program Execution Failed
β”‚
β”‚   with module.module_sql_sqerver.data.external.get_my_ip,
β”‚   on ..\modules\10-azurerm_mssql_server\data.tf line 16, in data "external" "get_my_ip":
β”‚   16:   program    = ["powershell", "-File", "${path.module}/../../INFRA01/scripts/get_ip.ps1"]
β”‚
β”‚ The data source received an unexpected error while attempting to execute the program.
β”‚
β”‚ Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
β”‚ Error Message: File D:\CodeSamples\TerraformCode\AzureCode10-InfrastructureSetup-UserInputForResourceName\INFRA01\scripts\get_ip.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
β”‚     + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
β”‚     + FullyQualifiedErrorId : UnauthorizedAccess
β”‚
β”‚ State: exit status 1

πŸ”§ In case you face above mentioned error, run the powershell command on your terraform terminal to provide access.

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

About

AzureCode03-InfraSetup - To setup RG, Azure Key Vault, Vnet, Subnet, NIC, NSG, Public IP, VM, SQL Server, SQL Database, by dynamically providing user-prefix for the resources at run time.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published