The script deploys VMs. By updating one script variable, you can deploy as many VMs as needed. However, you must still connect to the VM with SSH and install the app manually. Well, today, we are going to fix it!
In this task, you will learn how to use VM extension to automate the deployment of your app to the VM. The resulting script will allow you to deploy a VM and install the todo web app to it without any manual actions, only by running the Powershell script.
Before completing any task in the module, make sure that you followed all the steps described in the Environment Setup topic, in particular:
-
Ensure you have an Azure account and subscription.
-
Create a resource group called
mate-resourcesin the Azure subscription. -
In the
mate-resourcesresource group, create a storage account (any name) and atask-artifactscontainer. -
Install PowerShell 7 on your computer. All tasks in this module use Powershell 7. To run it in the terminal, execute the following command:
pwsh -
Install Azure module for PowerShell 7:
Install-Module -Name Az -Repository PSGallery -Force
If you are a Windows user, before running this command, please also run the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
- Log in to your Azure account using PowerShell:
Connect-AzAccount -TenantId <your Microsoft Entra ID tenant id>
In this task, you will need to write and run a Powershell script, which deploys virtual machines and uses a custom script VM extension to deploy a web app:
-
Write your script code to the file
task.ps1in this repository:- In the script, you should assume that you are already logged in to Azure and using the correct subscription (don't use commands 'Connect-AzAccount' and 'Set-AzContext', if needed — just run them on your computer before running the script).
The script already has code that deploys a VM. Update the code so it will deploy a web app from this repo using a custom script VM extension.
-
To deploy an extension, use Set-AzVMExtention commanded.
-
Extention should run a script
install-app.sh, which should be loaded from your fork of this repo. In your form, the script will be available by the URL:https://raw.githubusercontent.com/<your-github-username>/azure_task_12_deploy_app_with_vm_extention/main/install-app.sh -
Make sure to review and update the script
install-app.sh—it should clone your fork of this repo to the VM. Take note that asinstall-app.shwill be downloaded by your VM from GitHub, you need to commit and push changes to it before running the Powershell code that deploys the extension.
-
When the script is ready, run it to deploy resources to your subscription. Ensure that the script is working without errors and that the application is available on port 8080 after running the script. To verify that the web application is running, open in a web browser the following URL:
http://<your-public-ip-DNS-name>:8080. -
Run artifacts generation script
scripts/generate-artifacts.ps1. -
Test yourself using the script
scripts/validate-artifacts.ps1. -
Make sure that changes to both
task.ps1andresult.jsonare committed to the repo, and submit the solution for review. -
When the solution is validated, delete the resources you deployed with the Powershell script — you won't need them for the next tasks.
Tasks in this module are relying on 2 PowerShell scripts:
scripts/generate-artifacts.ps1generates the task “artifacts” and uploads them to cloud storage. An “artifact” is evidence of a task you completed. Each task will have its own script to gather the required artifacts. The script also adds a link to the generated artifact in theartifacts.jsonfile in this repository — make sure to commit changes to this file after you run the script.scripts/validate-artifacts.ps1validates the artifacts generated by the first script. It loads information about the task artifacts from theartifacts.jsonfile.
Here is how to complete tasks in this module:
-
Clone task repository
-
Make sure you completed the steps described in the Prerequisites section
-
Complete the task described in the Requirements section
-
Run
scripts/generate-artifacts.ps1to generate task artifacts. The script will update the fileartifacts.jsonin this repo. -
Run
scripts/validate-artifacts.ps1to test yourself. If tests are failing — follow the recommendation from the test script error message to fix or re-deploy your infrastructure. When you are ready to test yourself again — re-generate the artifacts (step 4) and re-run tests again. -
When all tests will pass — commit your changes and submit the solution for review.
Pro tip: If you are stuck with any of the implementation steps, run scripts/generate-artifacts.ps1 and scripts/validate-artifacts.ps1. The validation script might give you a hint on what to do.