Skip to content

Commit 7c8a346

Browse files
Merge pull request #263293 from asudbring/nat-bicep
Add bicep code to manage nat gateway article
2 parents 539aed4 + 5d39348 commit 7c8a346

File tree

1 file changed

+120
-4
lines changed

1 file changed

+120
-4
lines changed

articles/nat-gateway/manage-nat-gateway.md

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ This article explains how to manage the following aspects of NAT gateway:
2424

2525
## Prerequisites
2626

27+
# [**Azure portal**](#tab/manage-nat-portal)
28+
29+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
30+
31+
- An existing Azure Virtual Network with a subnet. For more information, see [Quickstart: Create a virtual network using the Azure portal](../virtual-network/quick-create-portal.md).
32+
33+
- The example virtual network that is used in this article is named *myVNet*.
34+
35+
- The example subnet is named *mySubnet*.
36+
37+
- The example NAT gateway is named *myNATgateway*.
38+
39+
# [**Azure PowerShell**](#tab/manage-nat-powershell)
40+
2741
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2842

2943
- An existing Azure Virtual Network with a subnet. For more information, see [Quickstart: Create a virtual network using the Azure portal](../virtual-network/quick-create-portal.md).
@@ -46,15 +60,41 @@ To use Azure PowerShell for this article, you need:
4660

4761
- Sign in to Azure PowerShell and select the subscription that you want to use. For more information, see [Sign in with Azure PowerShell](/powershell/azure/authenticate-azureps).
4862

63+
# [**Azure CLI**](#tab/manage-nat-cli)
64+
65+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
66+
67+
- An existing Azure Virtual Network with a subnet. For more information, see [Quickstart: Create a virtual network using the Azure portal](../virtual-network/quick-create-portal.md).
68+
69+
- The example virtual network that is used in this article is named *myVNet*.
70+
71+
- The example subnet is named *mySubnet*.
72+
73+
- The example NAT gateway is named *myNATgateway*.
74+
4975
To use Azure CLI for this article, you need:
5076

5177
- Azure CLI version 2.31.0 or later. Azure Cloud Shell uses the latest version.
5278

5379
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](~/articles/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
5480

81+
# [**Bicep**](#tab/manage-nat-bicep)
82+
83+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
84+
85+
- An existing Azure Virtual Network with a subnet. For more information, see [Quickstart: Create a virtual network using the Azure portal](../virtual-network/quick-create-portal.md).
86+
87+
- The example virtual network that is used in this article is named *myVNet*.
88+
89+
- The example subnet is named *mySubnet*.
90+
91+
- The example NAT gateway is named *myNATgateway*.
92+
93+
---
94+
5595
## Create a NAT gateway and associate it with an existing subnet
5696

57-
You can create a NAT gateway resource and add it to an existing subnet by using the Azure portal, Azure PowerShell, or the Azure CLI.
97+
You can create a NAT gateway resource and add it to an existing subnet by using the Azure portal, Azure PowerShell, Azure CLI, or Bicep.
5898

5999
# [**Azure portal**](#tab/manage-nat-portal)
60100

@@ -91,7 +131,7 @@ You can create a NAT gateway resource and add it to an existing subnet by using
91131

92132
1. Select **Create**.
93133

94-
# [**PowerShell**](#tab/manage-nat-powershell)
134+
# [**Azure PowerShell**](#tab/manage-nat-powershell)
95135

96136
### Public IP address
97137

@@ -282,6 +322,70 @@ az network vnet subnet update \
282322
--nat-gateway myNATgateway
283323
```
284324

325+
# [**Bicep**](#tab/manage-nat-bicep)
326+
327+
```bicep
328+
329+
@description('Name of the NAT gateway')
330+
param natgatewayname string = 'nat-gateway'
331+
332+
@description('Name of the NAT gateway public IP')
333+
param publicipname string = 'public-ip-nat'
334+
335+
@description('Name of resource group')
336+
param location string = resourceGroup().location
337+
338+
var existingVNetName = 'vnet-1'
339+
var existingSubnetName = 'subnet-1'
340+
341+
resource vnet 'Microsoft.Network/virtualNetworks@2023-05-01' existing = {
342+
name: existingVNetName
343+
}
344+
output vnetid string = vnet.id
345+
346+
resource publicip 'Microsoft.Network/publicIPAddresses@2023-06-01' = {
347+
name: publicipname
348+
location: location
349+
sku: {
350+
name: 'Standard'
351+
}
352+
properties: {
353+
publicIPAddressVersion: 'IPv4'
354+
publicIPAllocationMethod: 'Static'
355+
idleTimeoutInMinutes: 4
356+
}
357+
}
358+
359+
resource natgateway 'Microsoft.Network/natGateways@2023-06-01' = {
360+
name: natgatewayname
361+
location: location
362+
sku: {
363+
name: 'Standard'
364+
}
365+
properties: {
366+
idleTimeoutInMinutes: 4
367+
publicIpAddresses: [
368+
{
369+
id: publicip.id
370+
}
371+
]
372+
}
373+
}
374+
output natgatewayid string = natgateway.id
375+
376+
resource updatedsubnet01 'Microsoft.Network/virtualNetworks/subnets@2023-06-01' = {
377+
parent: vnet
378+
name: existingSubnetName
379+
properties: {
380+
addressPrefix: vnet.properties.subnets[0].properties.addressPrefix
381+
natGateway: {
382+
id: natgateway.id
383+
}
384+
}
385+
}
386+
387+
```
388+
285389
---
286390

287391
## Remove a NAT gateway from an existing subnet and delete the resource
@@ -310,7 +414,7 @@ You can now associate the NAT gateway with a different subnet or virtual network
310414

311415
1. Select **Yes**.
312416

313-
# [**PowerShell**](#tab/manage-nat-powershell)
417+
# [**Azure PowerShell**](#tab/manage-nat-powershell)
314418

315419
Removing the NAT gateway from a subnet by using Azure PowerShell isn't currently supported.
316420

@@ -334,6 +438,10 @@ az network nat gateway delete \
334438
--resource-group myResourceGroup
335439
```
336440

441+
# [**Bicep**](#tab/manage-nat-bicep)
442+
443+
Use the Azure portal, Azure PowerShell, or Azure CLI to remove a NAT gateway from a subnet and delete the resource.
444+
337445
---
338446

339447
> [!NOTE]
@@ -512,6 +620,10 @@ az network nat gateway update \
512620
--public-ip-addresses myPublicIP-NAT
513621
```
514622

623+
# [**Bicep**](#tab/manage-nat-bicep)
624+
625+
Use the Azure portal, Azure PowerShell, or Azure CLI to add or remove a public IP address from a NAT gateway.
626+
515627
---
516628

517629
## Add or remove a public IP prefix
@@ -556,7 +668,7 @@ Complete the following steps to add or remove a public IP prefix from a NAT gate
556668

557669
1. Select **Save**.
558670

559-
# [**PowerShell**](#tab/manage-nat-powershell)
671+
# [**Azure PowerShell**](#tab/manage-nat-powershell)
560672

561673
### Add public IP prefix
562674

@@ -688,6 +800,10 @@ az network nat gateway update \
688800
--public-ip-prefixes myPublicIPprefix-NAT
689801
```
690802

803+
# [**Bicep**](#tab/manage-nat-bicep)
804+
805+
Use the Azure portal, Azure PowerShell, or Azure CLI to add or remove a public IP prefix from a NAT gateway.
806+
691807
---
692808

693809
## Next steps

0 commit comments

Comments
 (0)