How to provision and configure a Linux VM using Azure CLI
Tags: azuredevops, bashscripting, linux, virtualmachine
Published: 2023-05-20 06:38:38

Whether you're a seasoned DevOps pro or just getting started on your cloud journey, exploring the wonders of the cloud using a command line interface is fulfilling. It can not be overemphasized that one of the frequent expectations of a Cloud/DevOps engineer is to provision resources in the cloud efficiently.

In this piece, we'll explore how we can leverage the Azure CLI to provision resources in the cloud, with a specific focus on creating and configuring a Linux virtual machine (VM).

**Rudiments /Requirements:
**
Before getting started, ensure that you have the following prerequisites in place:

1. An Azure subscription
2. Azure CLI installed on your local machine
3. Basic knowledge of the Linux operating system
4. Basic knowledge of Bash scripting

#### Device your environment variables
I'm using a Mac for this, so I'll define mine in my ~/.bashrc file located in my home direcroty.

$RESOURCE_GROUP_NAME
$LOCATION
$VM_NAME
$VM_IMAGE
$ADMIN_USERNAME

![Env vars for Vm provisioning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qndh9k89erdpdzpeh0h.png)

We need to run the below command to persist the env vars in our shell

`source ~/.bashrc
`
#### Login to Azure CLI
open your terminal or command prompt and enter command below:
`az login
`
or
`az login --use-device-code
`
#### Now, proceed to provision the Azure VM using CLI

`az vm create \
  --resource-group $RESOURCE_GROUP_NAME \
  --name $VM_NAME \
  --image $VM_IMAGE \
  --admin-username $ADMIN_USERNAME \
  --generate-ssh-keys \
  --public-ip-sku Standard`


![Output of VM provisioning](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i9q6bbd8pjr8dheuyd13.png)

#### Install Nginx server on newly provisioned vm:

```
az vm run-command invoke \
   --resource-group $RESOURCE_GROUP_NAME \
   --name $VM_NAME \
   --command-id RunShellScript \
   --scripts "sudo apt-get update && sudo apt-get install -y nginx"
```

![Nginx server installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1awu1hi07ryicl1h4l6.png)

#### Start the Nginx web server

```
az vm run-command invoke \
   --resource-group $RESOURCE_GROUP_NAME \
   --name $VM_NAME \
   --command-id RunShellScript \
   --scripts "sudo systemctl start nginx”
```

#### Expose HTTP port for web traffic:

```
az vm open-port --port 80 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
```

![Opening web port 80](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvdxot9x0fm0pjflbhnz.png)

#### Expose HTTPS port for encrypted web traffic:

```
az vm open-port --port 443 --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME
```

At this point, I had a blocker whilst trying to run the command to allow HTTPS traffic, I was able to overcome it using the method below.

## Blocker:

_> (SecurityRuleConflict) Security rule open-port-80 conflicts with rule open-port-443. Rules cannot have the same Priority and Direction. To learn more, see aka.ms/nsgrules.
> Code: SecurityRuleConflict
> Message: Security rule open-port-80 conflicts with rule open-port-443. Rules cannot have the same Priority and Direction. To learn more, see aka.ms/nsgrules._

### Solution

```
az vm open-port --resource-group $RESOURCE_GROUP_NAME --name $VM_NAME --port 443 --priority 940
```

![Network ports confirmed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s8l36bjs347uc11qfty.png)

![Site running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z19qtpukw7rac8fsw5h1.png)

#### Summary
we explored how to provision and configure a Linux VM using Azure CLI. We covered the steps from logging in to Azure CLI, creating a resource group, provisioning the VM, connecting to it via SSH, and performing Linux-specific configurations. With Azure CLI's powerful commands, managing and deploying Linux VMs on Azure becomes straightforward.

_The End!!_