How to Create Network Security Group in Azure: Step-by-Step Guide
To create a
Network Security Group (NSG) in Azure, use the az network nsg create command with parameters for resource group and NSG name. This command sets up a security boundary to control inbound and outbound traffic to your Azure resources.Syntax
The basic syntax to create a Network Security Group in Azure using Azure CLI is:
az network nsg create: Command to create the NSG.--resource-group: Specifies the resource group where the NSG will be created.--name: The name you want to assign to the NSG.--location: (Optional) The Azure region for the NSG. Defaults to the resource group's location if not specified.
bash
az network nsg create --resource-group MyResourceGroup --name MyNSG --location eastus
Example
This example creates a Network Security Group named MyNSG in the resource group MyResourceGroup located in eastus. It shows how to run the command and the expected output confirming creation.
bash
az network nsg create --resource-group MyResourceGroup --name MyNSG --location eastus
Output
{
"etag": "W/\"00000000-0000-0000-0000-000000000000\"",
"id": "/subscriptions/{subscription-id}/resourceGroups/MyResourceGroup/providers/Microsoft.Network/networkSecurityGroups/MyNSG",
"location": "eastus",
"name": "MyNSG",
"provisioningState": "Succeeded",
"resourceGroup": "MyResourceGroup",
"securityRules": [],
"type": "Microsoft.Network/networkSecurityGroups"
}
Common Pitfalls
Common mistakes when creating an NSG include:
- Not specifying the correct
--resource-group, which causes the command to fail. - Using an NSG name that already exists in the resource group, leading to conflicts.
- Forgetting to add security rules after creating the NSG, which means no traffic filtering is applied.
- Not specifying
--locationwhen the resource group spans multiple regions, causing unexpected placement.
bash
Wrong: az network nsg create --name MyNSG Right: az network nsg create --resource-group MyResourceGroup --name MyNSG --location eastus
Quick Reference
Here is a quick summary of key parameters for az network nsg create:
| Parameter | Description |
|---|---|
| --resource-group | Name of the Azure resource group |
| --name | Name of the Network Security Group |
| --location | Azure region for the NSG (optional) |
| --tags | Optional tags to organize resources |
| --debug | Show debug information during command execution |
Key Takeaways
Use the az CLI command 'az network nsg create' with resource group and name to create an NSG.
Always specify the correct resource group to avoid errors.
Add security rules after creating the NSG to control traffic.
Specify location if your resource group spans multiple regions.
Check for name conflicts before creating a new NSG.