Skip to content

Instantly share code, notes, and snippets.

@thomd
Last active August 2, 2021 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomd/ccc3f14915bf136b6f4a538ca913e16d to your computer and use it in GitHub Desktop.
Save thomd/ccc3f14915bf136b6f4a538ca913e16d to your computer and use it in GitHub Desktop.
Create a VMSS and other resource in your Azure account (from Udacity nd081) #azure
#!/bin/bash
read -r -p "Enter the Resource Group name: " resourceGroup
[ -z "$resourceGroup" ] && echo "enter a resource group" && exit 1
# Variables
location="eastus"
osType="UbuntuLTS"
vmssName="${resourceGroup}-vmss"
adminName="${resourceGroup}admin"
storageAccount="${resourceGroup}storage"
bePoolName="${vmssName}-bepool"
lbName="${vmssName}-lb"
lbRule="${lbName}-network-rule"
nsgName="${vmssName}-nsg"
vnetName="${vmssName}-vnet"
subnetName="${vnetName}-subnet"
probeName="tcpProbe"
vmSize="Standard_B1ls"
storageType="Standard_LRS"
# Create resource group
echo "Creating resource group $resourceGroup..."
az group create \
--name "$resourceGroup" \
--location "$location" \
--verbose
echo "Resource group created: $resourceGroup"
# Create Storage account
echo "Creating storage account $storageAccount"
az storage account create \
--name "$storageAccount" \
--resource-group "$resourceGroup" \
--location "$location" \
--sku Standard_LRS
echo "Storage account created: $storageAccount"
# Create Network Security Group
echo "Creating network security group $nsgName"
az network nsg create \
--resource-group "$resourceGroup" \
--name "$nsgName" \
--verbose
echo "Network security group created: $nsgName"
# Create VM Scale Set
echo "Creating VM scale set $vmssName"
az vmss create \
--resource-group "$resourceGroup" \
--name "$vmssName" \
--image "$osType" \
--vm-sku "$vmSize" \
--nsg "$nsgName" \
--subnet "$subnetName" \
--vnet-name "$vnetName" \
--backend-pool-name "$bePoolName" \
--storage-sku "$storageType" \
--load-balancer "$lbName" \
--custom-data cloud-init.txt \
--upgrade-policy-mode automatic \
--admin-username "$adminName" \
--generate-ssh-keys \
--verbose
echo "VM scale set created: $vmssName"
# Associate NSG with VMSS subnet
echo "Associating NSG: $nsgName with subnet: $subnetName"
az network vnet subnet update \
--resource-group "$resourceGroup" \
--name "$subnetName" \
--vnet-name "$vnetName" \
--network-security-group "$nsgName" \
--verbose
echo "NSG: $nsgName associated with subnet: $subnetName"
# Create Health Probe
echo "Creating health probe $probeName"
az network lb probe create \
--resource-group "$resourceGroup" \
--lb-name "$lbName" \
--name "$probeName" \
--protocol tcp \
--port 80 \
--interval 5 \
--threshold 2 \
--verbose
echo "Health probe created: $probeName"
# Create Network Load Balancer Rule
echo "Creating network load balancer rule $lbRule"
az network lb rule create \
--resource-group "$resourceGroup" \
--name "$lbRule" \
--lb-name "$lbName" \
--probe-name "$probeName" \
--backend-pool-name "$bePoolName" \
--backend-port 80 \
--frontend-ip-name loadBalancerFrontEnd \
--frontend-port 80 \
--protocol tcp \
--verbose
echo "Network load balancer rule created: $lbRule"
# Add port 80 to inbound rule NSG
echo "Adding port 80 to NSG $nsgName"
az network nsg rule create \
--resource-group "$resourceGroup" \
--nsg-name "$nsgName" \
--name Port_80 \
--destination-port-ranges 80 \
--direction Inbound \
--priority 100 \
--verbose
echo "Port 80 added to NSG: $nsgName"
# Add port 22 to inbound rule NSG
echo "Adding port 22 to NSG $nsgName"
az network nsg rule create \
--resource-group "$resourceGroup" \
--nsg-name "$nsgName" \
--name Port_22 \
--destination-port-ranges 22 \
--direction Inbound \
--priority 110 \
--verbose
echo "Port 22 added to NSG: $nsgName"
echo "VMSS script completed!"
echo
az vmss list-instance-connection-info \
--resource-group "$resourceGroup" \
--name "$vmssName" \
echo "Adminuser: $adminName"
echo
echo "login with: ssh -i ~/.ssh/id_rsa <admin>@<ip> -p <port>"
#cloud-config
package_upgrade: true
packages:
- nginx
- nodejs
- npm
write_files:
- owner: www-data:www-data
path: /etc/nginx/sites-available/default
content: |
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
- owner: azureuser:azureuser
path: /home/azureuser/myapp/index.js
content: |
var express = require('express')
var app = express()
var os = require('os');
app.get('/', function (req, res) {
res.send('<h1>Welcome to Azure VM Scale Set</h1> <h2>Hello World from host ' + os.hostname() + '! </h2>')
})
app.listen(3000, function () {
console.log('Hello world app listening on port 3000!')
})
runcmd:
- service nginx restart
- cd "/home/azureuser/myapp"
- npm init
- npm install express -y
- nodejs index.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment