DEV Community

Cover image for how to create azure resources via azure cli
Louis Oodo
Louis Oodo

Posted on

how to create azure resources via azure cli

This guide provides a comprehensive walkthrough for setting up an Azure environment from your Mac. Whether you are prepping for a certification or building a personal lab, these steps will take you from a blank terminal to a running Linux server.


1. Install Azure CLI & Login

On macOS, the most efficient way to manage the Azure CLI is through Homebrew.

Step-by-Step Installation:

  1. Open Terminal: Press Cmd + Space, type "Terminal," and hit Enter.
  2. Update Homebrew: Ensure your package manager is current.
brew update

Enter fullscreen mode Exit fullscreen mode
  1. Install Azure CLI:
brew install azure-cli

Enter fullscreen mode Exit fullscreen mode
  1. Login to Azure: Run the following command. It will automatically open your default web browser (Safari/Chrome).
az login

Enter fullscreen mode Exit fullscreen mode
  • Pro Tip: If your browser doesn't open or you are on a remote server, use az login --use-device-code.
  1. Set your Subscription: If you have multiple accounts, list them and select the correct one:
az account list --output table
az account set --subscription "Your-Subscription-Name"

Enter fullscreen mode Exit fullscreen mode

2. Create a Resource Group

A Resource Group (RG) is a logical container. Deleting the RG later will automatically clean up all resources inside it, preventing "ghost" charges on your bill.

Run this command:

az group create --name MyLabGroup --location eastus

Enter fullscreen mode Exit fullscreen mode
  • --name: The identifier for your group.
  • --location: Choose a region close to you (e.g., eastus, westeurope, southeastasia).

3. Build a Virtual Network (VNet) & Subnet

Before you can drop a VM into Azure, you need a network. We will create a VNet and a Subnet simultaneously.

Run this command:

az network vnet create \
  --resource-group MyLabGroup \
  --name MyVNet \
  --address-prefix 10.0.0.0/16 \
  --subnet-name MySubnet \
  --subnet-prefix 10.0.1.0/24

Enter fullscreen mode Exit fullscreen mode
  • 10.0.0.0/16: This is your private "building."
  • 10.0.1.0/24: This is a specific "room" (subnet) within that building where your VM will live.

4. Provision a Linux Virtual Machine

Now, let's launch an Ubuntu 22.04 VM. This command handles the compute, creates a Public IP, and secures it with SSH keys.

az vm create \
  --resource-group MyLabGroup \
  --name MyLinuxVM \
  --image Ubuntu2204 \
  --vnet-name MyVNet \
  --subnet MySubnet \
  --admin-username azureuser \
  --generate-ssh-keys \
  --public-ip-sku Standard

Enter fullscreen mode Exit fullscreen mode

What's happening here?

  • --generate-ssh-keys: This is the "Magic Mac" flag. It looks in your ~/.ssh folder for a key; if it doesn't find one, it creates one for you automatically.
  • --public-ip-sku Standard: Assigns a static-capable IP so you can reach the machine from your laptop.

5. Verify & Connect

Once the command finishes, you will see a JSON output. Look for the publicIpAddress field.

To connect from your Mac:

ssh azureuser@<YOUR_PUBLIC_IP>

Enter fullscreen mode Exit fullscreen mode

If this is your first time connecting, type yes when asked about the authenticity of the host.


Summary of Commands

Action Command Snippet
Install brew install azure-cli
Login az login
Create Group az group create -n <name> -l <location>
Create Network az network vnet create ...
Create VM az vm create ...

Would you like me to provide a script to automatically shut down or delete these resources so you don't get charged when you're finished with your lab?

Top comments (0)