Environment Setup
Guide to Automating Resource Group Creation in Azure
This comprehensive guide provides detailed instructions on automating the creation of resource groups within Microsoft Azure. By following these steps, you can ensure a secure and scalable environment setup, which is crucial for efficient cloud resource management.
Tags
- Environment
- Setup
- Resource Group
- Automation
Steps
- Open Azure Portal or Azure CLI
* Start by accessing the Azure Portal or using the Azure Command-Line Interface (CLI). This is the initial step in setting up your environment, allowing you to manage Azure resources effectively.
- Create Resource Groups
* Utilize the Azure CLI to create multiple resource groups. This can be done by executing the following commands, which specify the name and location for each group:
az group create --name DevOpsExpert-Dev1 --location eastus
az group create --name DevOpsExpert-Dev2 --location eastus
az group create --name DevOpsExpert-Dev3 --location eastus
az group create --name DevOpsExpert-Test --location eastus
az group create --name DevOpsExpert-Prod --location eastus
* These commands create resource groups for different environments such as development, testing, and production, all located in the East US region.
- Verify Resource Group Creation
* After executing the commands, verify the successful creation of resource groups by navigating to the "Resource Groups" section in the Azure Portal. This step ensures that all specified groups are correctly set up and ready for use.
- Ensure Isolation for Security and Scalability
* It is crucial to maintain isolation between resource groups to enhance security and scalability. This practice prevents unauthorized access and ensures that resources can scale independently according to demand.
Placeholders
Master Script
The following Bash script automates the setup of environments by creating resource groups, deploying infrastructure, and configuring various services:
bash
#!/bin/bash
Variables for the project (supporting dev, test, and prod environments)
ENVIRONMENTS=("dev1" "dev2" "dev3" "test" "prod")
RESOURCE\_GROUP\_PREFIX="DevOpsExpert-"
LOCATION="eastus"
SQL\_ADMIN="sqladmin"
SQL\_PASSWORD="P@ssw0rd123"
DOMAIN="devopsexpert.ai"
FRONT\_DOOR\_PROFILE="FrontDoorProfile"
Authenticate with Azure
az login
Loop through each environment (dev1, dev2, dev3, test, prod)
for ENV in "${ENVIRONMENTS\[@\]}"; do
RESOURCE\_GROUP="${RESOURCE\_GROUP\_PREFIX}${ENV}"
APP\_NAME="${ENV}.${DOMAIN}"
echo "Setting up environment for $APP\_NAME..."
# 1. Create Resource Group for each environment
echo "Creating resource group $RESOURCE\_GROUP..."
az group create --name $RESOURCE\_GROUP --location $LOCATION
# 2. Deploy Infrastructure using ARM templates (IIS servers, SQL)
echo "Deploying infrastructure for $APP\_NAME..."
az deployment group create --resource-group $RESOURCE\_GROUP --template-file infrastructure-template.json --parameters adminUsername=$SQL\_ADMIN adminPassword=$SQL\_PASSWORD appName=$APP\_NAME
# 3. CI/CD Setup (GitHub Actions)
echo "Setting up GitHub Actions for CI/CD for $APP\_NAME..."
cat <<EOT >> .github/workflows/deploy.yml
name: Build and Deploy $APP\_NAME
on:
push:
branches:
- main
jobs:
build-deploy-$ENV:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build and Deploy to Azure
run: |
az webapp up --resource-group $RESOURCE_GROUP --name $APP_NAME --plan Free
EOT
# 4. Configure SSL Certificates with Azure Front Door for each environment
echo "Configuring SSL certificates for $APP\_NAME using Azure Front Door..."
az afd endpoint create --resource-group $RESOURCE\_GROUP --profile-name $FRONT\_DOOR\_PROFILE --endpoint-name $ENV --host-name $APP\_NAME
# Create a free Azure Managed Certificate for the environment
az afd custom-domain create --resource-group $RESOURCE\_GROUP --profile-name $FRONT\_DOOR\_PROFILE --custom-domain-name $ENV --host-name $APP\_NAME
# Enable HTTPS with automatic certificate management
az afd custom-domain https enable --resource-group $RESOURCE\_GROUP --profile-name $FRONT\_DOOR\_PROFILE --custom-domain-name $ENV --certificate-type ManagedCertificate
# 5. Configure Monitoring with New Relic and Azure Monitor
echo "Setting up New Relic and Azure Monitor for $APP\_NAME..."
msiexec /i http://download.newrelic.com/infrastructure\_agent.msi /qn
# Azure Monitor Alerts for CPU usage
az monitor metrics alert create --name "HighCPUAlert-$ENV" --resource-group $RESOURCE\_GROUP --scopes "/subscriptions/YOUR\_SUB\_ID/resourceGroups/$RESOURCE\_GROUP/providers/Microsoft.Compute/virtualMachines/IIS-Server" --condition "avg Percentage CPU > 80"
# 6. Backup and Disaster Recovery for each environment
echo "Setting up Backup and Disaster Recovery for $APP\_NAME..."
az backup protection enable-for-vm --resource-group $RESOURCE\_GROUP --vault-name BackupVault --vm IIS-Server
az site-recovery protection enable --source-vault-name RecoveryVault --target-resource-group $RESOURCE\_GROUP --source-virtual-machine IIS-Server --target-region westus
# 7. Set up Auto-scaling and Teardown
echo "Setting up auto-scaling for $APP\_NAME..."
az monitor autoscale create --resource-group $RESOURCE\_GROUP --name "AutoScale-$ENV" --min-count 1 --max-count 5 --count 2
echo "Setting up automated resource teardown for $APP\_NAME after idle time..."
az group delete --name $RESOURCE\_GROUP --yes --no-wait
done
ARM Template
The following ARM (Azure Resource Manager) template is used to deploy infrastructure components such as virtual networks, load balancers, and virtual machines:
json
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": \[
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2021-02-01",
"name": "[concat(parameters('appName'), '-pip')]",
"location": "[resourceGroup().location]",
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-06-01",
"name": "[concat(parameters('appName'), '-vnet')]",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": {
"addressPrefixes": ["10.0.0.0/16"]
},
"subnets": [
{
"name": "default",
"properties": {
"addressPrefix": "10.0.1.0/24"
}
}
]
}
},
{
"type": "Microsoft.Network/loadBalancers",
"apiVersion": "2021-02-01",
"name": "[concat(parameters('appName'), '-lb')]",
"location": "[resourceGroup().location]",
"properties": {
"frontendIPConfigurations": [
{
"name": "LoadBalancerFrontend",
"properties": {
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(parameters('appName'), '-pip'))]"
}
}
}
],
"backendAddressPools": [
{
"name": "LoadBalancerBackend"
}
],
"loadBalancingRules": [
{
"name": "HTTPRule",
"properties": {
"frontendIPConfiguration": {
"id": "[concat(resourceId('Microsoft.Network/loadBalancers', concat(parameters('appName'), '-lb')), '/frontendIPConfigurations/LoadBalancerFrontend')]"
},
"backendAddressPool": {
"id": "[concat(resourceId('Microsoft.Network/loadBalancers', concat(parameters('appName'), '-lb')), '/backendAddressPools/LoadBalancerBackend')]"
},
"protocol": "Tcp",
"frontendPort": 80,
"backendPort": 80,
"enableFloatingIP": false,
"idleTimeoutInMinutes": 4,
"loadDistribution": "Default",
"probe": {
"id": "[concat(resourceId('Microsoft.Network/loadBalancers', concat(parameters('appName'), '-lb')), '/probes/HealthProbe')]"
}
}
}
],
"probes": [
{
"name": "HealthProbe",
"properties": {
"protocol": "Http",
"port": 80,
"requestPath": "/"
}
}
]
}
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2021-07-01",
"name": "[concat(parameters('appName'), '-vm')]",
"location": "[resourceGroup().location]",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"osProfile": {
"computerName": "[concat(parameters('appName'), '-vm')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2019-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', concat(parameters('appName'), '-nic'))]"
}
]
}
}
},
{
"type": "Microsoft.Sql/servers",
"apiVersion": "2021-02-01",
"name": "[concat(parameters('appName'), '-sqlserver')]",
"location": "[resourceGroup().location]",
"properties": {
"administratorLogin": "[parameters('adminUsername')]",
"administratorLoginPassword": "[parameters('adminPassword')]",
"version": "12.0"
}
},
{
"type": "Microsoft.Sql/servers/databases",
"apiVersion": "2021-02-01",
"name": "[concat(parameters('appName'), '-sqldb')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Sql/servers', concat(parameters('appName'), '-sqlserver'))]"
],
"properties": {
"collation": "SQL_Latin1_General_CP1_CI_AS",
"maxSizeBytes": "2147483648",
"createMode": "Default",
"requestedServiceObjectiveName": "S1"
}
}
\],
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "Admin username for virtual machines and SQL Server"
}
},
"adminPassword": {
"type": "securestring",
"metadata": {
"description": "Admin password for virtual machines and SQL Server"
}
},
"appName": {
"type": "string",
"metadata": {
"description": "Name of the application (used for naming resources)"
}
}
}
}
This guide and the accompanying scripts and templates provide a robust framework for automating the setup of Azure environments, ensuring that your infrastructure is both secure and scalable.