Image by Cloudypixel on Unsplash
In my last post. I demonstrated how to create a static website using the Azure Portal. However, many of those steps are much quicker using the Azure CLI. For today’s post, I’ll show you how to do just that.
First, login to your azure account at portal.azure.com and click on the cloudshell icon at the top of the page:
Alternatively, in another tab, you can browse to the full screen version at shell.azure.com. You can also use the CloudShell using the Windows Terminal - or you can use the AZ CLI from a local machine.
If you haven’t used it before, you’ll be prompted to create a storage account for your cloudshell - this stores any scripts you create/save here, command history, profile etc).
If you have access to multiple subscriptions, first change your subscription using the command:
az account set --subscription <subscription-id>
If you don’t know the subscription ID - enter the following command to list those available to you:
az account list --output table
You should see output like the following:
Now, just like in the last post - you’ll want a resource group for this, which you can achieve with the following command:
az group create --name <group_name> --location <location>
e.g. az group create --name MyTestRG --location "UK West"
How about storage account creation? Try the following command syntax:
az storage account create --name <account_name> --resource-group <resource_group_name> --sku "Standard_LRS" --location <location> --kind "StorageV2" --https-only true
e.g. az storage account create --name myteststor --resource-group MyTestRG --sku "Standard_LRS" --location "UK West"
Now that we have a storage account, we want to enable and configure Static Websites feature. Think you’ll need to do that in the portal? Think again - AZ CLI has you covered again:
az storage blob service-properties update --account-name <account_name> --static-website --404-document 404.html --index-document index.html
e.g. az storage blob service-properties update --account-name myteststor --static-website --404-document 404.html --index-document index.html
How about uploading your static HTML content? I mentioned in my advent calendar post about using the portal, Azure Storage Explorer or the REST API. In researching the AZ CLI, I discovered the az storage blob upload-batch
command.
If you’re running this from the CloudShell, you’ll need to have the files uploaded to the storage account that it uses, or have that mounted as a PS Drive in powershell.
You can upload files directly in within the CloudShell in the portal/browser:
For this example, I’ll upload files from a local folder using the AZ CLI - so you first need to enter az login
and follow the instructions to complete device login in the browser.
Then, use az account set --subscription-id <subscription-id>
if you have more than one subscription.
You need to escape the dollar sign - in cloudshell use \
, but locally I found a backtick to work just fine:
az storage blob upload-batch -s <source-path> -d `$web --account-name <storage-account-name>
e.g.
az storage blob upload-batch -s D:\Test -d `$web --account-name myteststor
You’ll see output similar to the following:
PS D:\AzureAdvent> az storage blob upload-batch -s D:\AzureAdvent\cli-test\ -d `$web --account-name myteststor
uploading D:\Test\advent.jpg
Finished[#############################################################] 100.0000%
uploading D:\Test\index.html
Finished[#############################################################] 100.0000%
[
{
"Blob": "https://myteststor.blob.core.windows.net/$web/advent.jpg",
"Last Modified": "2019-12-29T20:20:49+00:00",
"Type": "image/jpeg",
"eTag": "\"0x8D78C9C98A08B5B\""
},
{
"Blob": "https://myteststor.blob.core.windows.net/$web/index.html",
"Last Modified": "2019-12-29T20:20:50+00:00",
"Type": "text/html",
"eTag": "\"0x8D78C9C98B54FA0\""
}
]
So there you have it - you can spin up and populate a static website with as few as 4 AZ CLI commands, and (size dependent) in under a minute!
az group create --name MyTestRG --location "UK West"
az storage account create --name myteststor --resource-group MyTestRG --sku "Standard_LRS" --location "UK West"
az storage blob service-properties update --account-name myteststor --static-website --404-document 404.html --index-document index.html
az storage blob upload-batch -s D:\Test -d `$web --account-name myteststor
Give it a try!
As ever, thanks for reading and feel free to leave comments below.
If you like what I do and appreciate the time and effort and expense that goes into my content you can always