Tag Archives: Azure

Azure VM run commands

Azure VM run commands is a mechanism to run a PowerShell script inside a VM which is hosted on Azure. I need to perform some steps inside a Azure VM as a part of CI CD but I can’t open a connection to the VM for security reasons. In this case I find the Azure VM run commands is very useful. I use this to run a PowerShell script from my CI CD agent to the Azure VM.

There are three ways with which you can run a script inside the VM from outside without connecting to the VM. First is from Azure Portal, second is with Azure CLI and third is with Azure PowerShell. In my case I used Azure PowerShell.

You need to create a PowerShell script (.ps1) file which you need to run inside the VM. Then you need to login to Azure with Connect-AzAccount and use Invoke-AzVMRunCommand to run the command inside the VM.

I have created a PowerShell script file with name HelloUser.ps1.

param(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string]
    $UserName
)

Write-Host "Hello $UserName"

Then I have logged in to Azure.

Connect-AzAccount

After login I can use Invoke-AzVMRunCommand to run HelloUser.ps1 script inside the VM.

Invoke-AzVMRunCommand `
    -ResourceGroupName "vm-resource-group" `
    -Name "target-vm-name" `
    -CommandId "RunPowerShellScript" `
    -ScriptPath "HelloUser.ps1" `
    -Parameter @{"UserName" = "Jon"}

Read more about Azure VM run commands here.

Create Azure web app with Azure Cli

I am using Azure cloud shell to create a web app. I am opening https://portal.azure.com and click on the cloud shell button to open cloud shell window inside web browser. I am using bash and Azure Cli.

Create a new resource group.

az group create --name webapprg-1602 --location northeurope

Create a new app service plan.

az appservice plan create --name appserviceplan-1602 \
    --resource-group webapprg-1602

Create a new web app.

az webapp create --name webapp-1602 --resource-group webapprg-1602 \
    --plan appserviceplan-1602

Clean up

az group delete --name webapprg-1602

Choose between Azure CLI and Azure PowerShell

There is a misconception that Azure PowerShell works only on Windows and Azure CLI works everywhere. This is not true. PowerShell (not Windows PowerShell) is based on .NET Core which is cross platform. We can run PowerShell on Windows as well as on Linux. Both Azure PowerShell and Azure CLI runs on Windows and Linux.

Which option you will choose depends on your personal preference and from what background you are coming. If you are already working on PowerShell then Azure PowerShell should be your choice but if you are more like a person who works with for example Bash, then Azure CLI will be a good option for you. Also sometimes people find Azure CLI commands are more easy to type and remember.

This is a personal preference.

Containerized .NET app

Create a console app

I have created a new .NET 5 console app.

ConsoleApp1.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Program.cs

using System;

Console.WriteLine("Hello World!");

I have published the app in a folder.

In the publish folder I found the below files.

To test the published app, I have opened a terminal and navigate to the published folder and execute below command.

dotnet .\ConsoleApp1.dll

This should print ‘Hello World!’ in the terminal.

Create container image with Docker

I have WSL 2 installed in my Windows 10 machine and I have Ubuntu installed. I have installed Docker desktop for WSL. I have created a new folder ‘ConsoleApp1’ in the Ubuntu home folder and put the published artefacts inside that.

I have opened Widows Terminal and connect Ubuntu Bash with it. Navigate to the ‘ConsoleApp1’ folder. Type code . to open VS Code.

Add a new file with name ‘Dockerfile’ in the folder.

In VS Code write the below code in the Dockerfile.

FROM mcr.microsoft.com/dotnet/runtime:5.0
COPY . ./app
WORKDIR /app
ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]

In the terminal run below command to create a Docker image.

docker build -t 45862391/consoleapp1:v1 .

Check the image successfully created or not.

docker images

To push the image to Docker Hub, run the below command in terminal.

docker push 45862391/consoleapp1:v1

Create a container with Azure Container Instances

Open Azure Portal and create a new Container Instance resource.

After the successful creation of the resource I can check the logs for the output of my console app.

Now my .NET 5 console app is deployed to Azure Container Instance from Docker Hub image.

Summary

  1. Create a .NET 5 console app.
  2. Publish it in a folder.
  3. Create a Dockerfile.
  4. Create Docker image and push to Docker Hub.
  5. Create a Azure container instance based on Docker Hub image.