Using PowerShell to Create a Static Website on Azure Storage

Kristian Ranstrom
June 22, 2022
3 min read

I actually use Azure Storage to host static websites all the time.  It costs on average 20 cents per month to host static websites that way.  It's an unbelievable no-brainer to host that way.  Setting up the environment in Azure takes maybe 20 minutes and there is a definite recipe that you must follow to make it happen.  The overall steps are:

  1. Create a new or use an existing Resource Group
  2. Create a new Storage Account
  3. Enable a Static Website withing the Storage Account
  4. Set your index and error document paths
  5. Create an Azure CDN
  6. Create a CDN Endpoint with Origin pointing to your Static Website url.
  7. Add a Custom Domain (with DNS cdnverify CNAME record)
  8. Enable HTTPS
  9. Create a rule to handle HTTPS enforcement

USING POWERSHELL

That's a lot of steps.  Let's simplify this by using a PowerShell script instead.

Let's start out by opening up a PowerShell window and login into Azure.


# connect to azure accounts to login
Connect-AzAccount

Now let's create a script to handle the creation of the environment:

# Define the variables for the environment
$ResourceGroupName="demo-resource-group"
$location="WestUs" 
$storagename="demostoragerst" # must be globally unique
$storagesku="Standard_GRS"
$containername="democontainer"
$cdnsku="Standard_Microsoft"
$customdomain="demo.domain.com"
$cdnprofilename="demo-domain-cdn"
$endpointname="demo-endpoint-name" # must be globally unique

# In order for the custom domain part of this script to work,  
# you must have a CNAME setup prior to running this
# The DNS entry will be in the form of:

# TYPE: CNAME
# HOST: subdomain.domain.com (your custom domain)
# VALUE: demo-endpoint-name.azureedge.net

# Note that it's the $endpointname with a .azureedge.net suffix


# Create a resource group.  
New-AzResourceGroup -Name $ResourceGroupName -Location $location  

# create a generic storage account
$storageAccount = New-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $storagename -Location $location -SkuName $storagesku

# set the current default storage account in powershell
Set-AzCurrentStorageAccount -ResourceGroupName $ResourceGroupName -Name $storagename

# get context of the storageaccount for next commands
$ctx = $storageAccount.Context

# optional - create a container.  (Permission is one of: Container,Blob,Off)
New-AzStorageContainer -Name $containername -Permission Container -Context $ctx

# optional - create a few containers at one time if you want
"container1 container2 container3".split() | New-AzStorageContainer -Permission Container

# enable static website 
Enable-AzStorageStaticWebsite -IndexDocument "index.html" -ErrorDocument404Path "index.html"

# disable static website 
# Disable-AzStorageStaticWebsite

# get access keys for a storage account in case you need them for other things
Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $storagename

# get the url of the static website
$weburl = (Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $storagename|select PrimaryEndpoints).PrimaryEndpoints.Web
$weburl = $weburl.replace("https://", "")
$weburl = $weburl.replace("/", "")

# create a CDN
New-AzCdnProfile -Name $cdnprofilename -ResourceGroupName $ResourceGroupName -Location $location -SkuName $cdnsku

# create an endpoint
$origin = @{
	Name = "websiteorigin"
	HostName = $webUrl
}
New-AzCdnEndpoint -EndpointName $endpointname -ProfileName $cdnprofilename -ResourceGroupName $ResourceGroupName -Location $location -Origin $origin -IsHttpsAllowed

# see endpoint details
Get-AzCdnEndpoint -Name $endpointname -ProfileName $cdnprofilename -ResourceGroupName $ResourceGroupName

# get domain url
(Get-AzCdnEndpoint -Name $endpointname -ProfileName $cdnprofilename -ResourceGroupName $ResourceGroupName|select HostName).HostName


# add a custom domain (cdnverify cname must be created prior to running this or else it'll error out)
#New-AzCdnCustomDomain -EndpointName $endpointname -HostName $customdomain -CustomDomainName $customdomain -ProfileName $cdnprofilename -ResourceGroupName $ResourceGroupName

# enable https
#Enable-AzCdnCustomDomainHttps -EndpointName $endpointname -CustomDomainName $customdomain -ProfileName $cdnprofilename -ResourceGroupName $ResourceGroupName

If you're new to PowerShell for Azure, you can get setup by following my Setting Up PowerShell for Azure post.