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.
- Resource Group (for Key Vault)
- Key Vault
- SQL Username
- SQL Password (User Input)
- VM Username
- VM Password (User Input)
- Key Vault
- Resource Group (User Input for suffix)
- Virtual Network (VNet)
- Subnet
- NSG
- Public IP
- NIC
- NIC-NSG association
- Virtual Machine
- SQL Server
- SQL Database
π .\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"π infra01.auto.tfvars
---------------------------------------------------------------------------------------------------
# β
Update this variable with your own subscription ID.
main_provider_subscription_id = "00000000-0000-0000-000000000000"π 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.terraform init -backend-config='backend-config-infra01.hcl'
OR
terraform init -backend-config='backend-config-infra01.hcl' -upgradeterraform validate
terraform planterraform apply
# β Do Not run terraform apply -auto-approveUser 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 valueUser 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 valueUser 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 valueApply 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 UsernameError: 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: 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