In this studio we will be using PowerShell to script a complete deployment of the Coding Events API to a Linux VM.
This PowerShell script will provision and configure all of the Azure resources we will need for the deployment. The script will combine all the steps from the Azure CLI chapter into one easily runnable deploy script.
You will be writing the PowerShell script, however the provisioned VM will require three Bash configuration scripts. The VM Bash configuration scripts will be provided for you, but it would be in your best interest to read over them to make sense of what they are doing.
This will be one of the most challenging studios you have worked on. However, at the end of this studio, you will gain invaluable practical experience writing real scripts and utilizing the power of PowerShell. To that end, consider a methodical approach to completing it:
While the first two steps should now be familiar to you, the final two points are the most critical. Before you start using any of the commands, make sure to write smaller scripts to test out how to:
These are all techniques you have worked with in previous walkthroughs and studios that you can review if necessary.
Fork and clone the powershell-az-cli-scripting-deployment repository.
Update the provisionResources.ps1
script so that it accomplishes the following:
systemAssignedIdentity
systemAssignedIdentity
)configure-vm.sh
, configure-ssl.sh
, deliver-deploy.sh
)We will provide you with steps 6 and 9, which have a high likelihood being written incorrectly and causing an error.
Tip
The Bash script provision-resources.sh
you saw earlier is very similar to this script, due to the cross-platform nature of the Azure CLI. However, the shell-specific syntax will be the bulk of your work in writing a PowerShell variant.
After completing the script, you will need to update the KeyVaultName
in your appsettings.json
using the name that you set in your provisionResources.ps1
script. In step 6 of the script it was automatically set to <name>-lc0820-ps-kv
.
The deliver-deploy.sh
script will require you to set your GitHub username and the branch of your Coding Events API that includes the updated appsettings.json
. These are the variables at the top of the script:
github_username=
(your username)solution_branch=
(3-aadb2c
)Finally, run the script and confirm the deployment by navigating to the public IP address that it prints out!
After you have finished and executed your deploy script you will be able to access your running application using HTTPS at the public IP address of your VM.
Share this link along with your powershell-az-cli-scripting-deployment
repo link with your TA so they can review your work.
Recall that Windows will not let you just run a PowerShell script, you must first set the ExecutionPolicy
before you can run any custom PowerShell scripts.
In the cloned repository you will find a folder called exampleResources
. This folder contains three JSON files with the output of each Azure CLI command.
You can use these example resources to practice working with these outputs as PowerShell objects. They can be loaded from the file and converted to PSCustomObjects
(exactly what the commands will output) using Get-Content
and ConvertFrom-Json
.
For example, you can examine the resource group name with:
(Get-Content ./resourceGroup.json | ConvertFrom-Json).name
Recall that your Key Vault will need the VM’s systemAssignedIdentity
to properly set an access policy from the Azure CLI. See if you can access this property with PowerShell and virtualMachine.json
. Then consider and practice accessing the other properties you will need using:
virtualMachine.json
resourceGroup.json
keyVault.json
Here is a general example of how to load and access a property. Be mindful of the syntax needed to access nested properties, or those that exist within an array field:
> $virtualMachine = Get-Content virtualMachine.json | ConvertFrom-Json
> $virtualMachine.someProperty
Fun Fact
These files were created using a simple PowerShell pipeline. For example, the virtualMachine.json
file was created like this:
> az vm create -n .... | Set-Content virtualMachine.json
As we saw in the Azure CLI walkthrough, you will want to explore and plan out your commands before turning them into a script. As a reminder, you can get help for any AZ CLI command, or sub-command with -h
or the longhand --help
:
> az vm create -h
Similar to how the example .json
files were created, you can capture the output in a variable:
> $someVariable = az vm create -n .....
> $someVariable.someProperty
If you think you’ve messed something up throughout this deployment, you can easily destroy the entire resource group using the AZ CLI:
> $rgName = "<your-rg-name>"
> az group delete -n "$rgName"
This command takes a couple of minutes to run because it first has to delete each of the resources inside of the resource group. However, this handy command allows you to cleanup easily, or start over if you’ve made a mistake!