Linux virtual machine¶
Level: Intermediate
Overview and purpose¶
Create a Trusted Launch Ubuntu VM and private NIC with SSH-key authentication and a system-assigned identity.
Azure services and dependencies¶
Read main.bicep for the exact resource types, supported API versions, decorators, and outputs. No secret values are required or emitted by this module.
Prerequisites¶
Install Azure CLI and Bicep, authenticate to the intended subscription, and
confirm az account show before executing cloud commands.
Provide a subnet, supported VM quota, and an SSH public key. Configure an NSG plus a private management path such as VPN or Bastion.
Use the repository root as the working directory. Commands below use Bash
(Azure Cloud Shell works); PowerShell variable syntax differs.
Set RG to a dedicated existing sandbox group (except for the subscription-scoped
resource-group example), and LOCATION to an approved Azure region.
Set shell variables NAME, SUBNET_ID, SSH_PUBLIC_KEY to your own approved values.
For array/object inputs, put valid JSON in the corresponding quoted variable.
Never use example IDs as real principal/resource IDs.
Important parameters¶
| Parameter | Type | Requirement |
|---|---|---|
name |
string |
Required |
location |
string |
Optional; see source default |
subnetId |
string |
Required |
sshPublicKey |
string |
Required |
administrator |
string |
Optional; see source default |
vmSize |
string |
Optional; see source default |
Descriptions and defaults live beside each declaration in the canonical source. Names must meet the individual Azure service's naming rules.
Bicep source and usage¶
View the complete source.
Reference this module from another Bicep file with a relative module path and
a params object. The resources/modules lesson shows composition and dependencies.
Compile and validate locally¶
az bicep build --file 2_compute-containers/virtual-machine/main.bicep --stdout
Compilation and linting need no Azure login and create no resources. A successful compile is not evidence that a deployment or application has been tested.
Deployment instructions¶
These are opt-in cloud operations, never routine CI steps. Review policy, permissions, region, quotas, dependencies, and price before proceeding.
az deployment group validate --resource-group "$RG" --template-file 2_compute-containers/virtual-machine/main.bicep --parameters name="$NAME" subnetId="$SUBNET_ID" sshPublicKey="$SSH_PUBLIC_KEY"
az deployment group what-if --resource-group "$RG" --template-file 2_compute-containers/virtual-machine/main.bicep --parameters name="$NAME" subnetId="$SUBNET_ID" sshPublicKey="$SSH_PUBLIC_KEY"
az deployment group create --resource-group "$RG" --template-file 2_compute-containers/virtual-machine/main.bicep --parameters name="$NAME" subnetId="$SUBNET_ID" sshPublicKey="$SSH_PUBLIC_KEY"
For a reusable deployment, put your own values into an untracked
*.local.bicepparam file with a using declaration instead of embedding
environment identifiers into the module.
Expected results and validation¶
The deployment returns the outputs declared at the end of main.bicep.
Inspect the deployment's provisioning state and resource IDs in Azure Portal
or Azure CLI. For a resource-group deployment:
az resource list --resource-group "$RG" --output table
For subscription resource-group creation, verify az group show --name "$NAME".
Then perform the service-specific checks below. No live Azure deployment is
claimed by this repository's offline CI.
Service-specific checks and limitations¶
No public IP, Bastion, NAT, or application is deployed. Private-only VMs need explicit outbound access for package installation. The image uses latest; pin an image version for production reproducibility.
Cleanup¶
Only if this is a dedicated disposable group, inspect its contents and delete it:
az resource list --resource-group "$RG" --output table
az group delete --name "$RG"
For the resource-group module use the created NAME as RG.
Do not delete shared prerequisites or groups containing unrelated work.
Deletion can be delayed by locks, soft-delete, protection settings, or backup
retention. Verify completion in Azure; stopping compute is not full cleanup.
Cost and security considerations¶
Running compute and disks are billable. Deallocation stops compute billing but not disk costs; deleting the dedicated group removes lab resources.
Use least privilege and approved regions. Consult the current Azure pricing calculator and Microsoft Bicep resource reference.
Complete Bicep source¶
@description('Virtual machine name.')
param name string
@description('Azure region.')
param location string = resourceGroup().location
@description('Existing subnet resource ID. No public IP is created.')
param subnetId string
@description('SSH public key, never a private key.')
param sshPublicKey string
@description('Linux administrator username.')
param administrator string = 'azureuser'
@description('VM size available in your region and quota.')
param vmSize string = 'Standard_B2s'
resource nic 'Microsoft.Network/networkInterfaces@2024-05-01' = {
name: '${name}-nic'
location: location
properties: {
ipConfigurations: [{
name: 'primary'
properties: { privateIPAllocationMethod: 'Dynamic', subnet: { id: subnetId } }
}]
}
}
resource vm 'Microsoft.Compute/virtualMachines@2024-07-01' = {
name: name
location: location
identity: { type: 'SystemAssigned' }
properties: {
hardwareProfile: { vmSize: vmSize }
osProfile: {
computerName: name
adminUsername: administrator
linuxConfiguration: {
disablePasswordAuthentication: true
ssh: { publicKeys: [{ path: '/home/${administrator}/.ssh/authorized_keys', keyData: sshPublicKey }] }
}
}
storageProfile: {
imageReference: {
publisher: 'Canonical'
offer: 'ubuntu-24_04-lts'
sku: 'server'
version: 'latest'
}
osDisk: { createOption: 'FromImage', managedDisk: { storageAccountType: 'Standard_LRS' }, deleteOption: 'Delete' }
}
networkProfile: { networkInterfaces: [{ id: nic.id, properties: { deleteOption: 'Delete' } }] }
securityProfile: { securityType: 'TrustedLaunch', uefiSettings: { secureBootEnabled: true, vTpmEnabled: true } }
}
}
output id string = vm.id