Skip to content

Commit 48b192c

Browse files
committed
added terraform article for Azure NAT Gateway
1 parent aed7d85 commit 48b192c

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: 'Quickstart: Create an Azure NAT Gateway using Terraform'
3+
titleSuffix: Azure NAT Gateway
4+
description: 'In this article, you create an Azure Virtual Machine with a NAT Gateway using Terraform'
5+
ms.topic: quickstart
6+
ms.date: 02/21/2024
7+
ms.custom: devx-track-terraform
8+
ms.service: virtual-network
9+
author: asudbring
10+
ms.author: allensu
11+
content_well_notification:
12+
- AI-contribution
13+
---
14+
15+
# Quickstart: Create a Azure NAT Gateway using Terraform
16+
17+
This article shows how to use Terraform to create a resource group, a virtual network with a subnet, a public IP address, a NAT gateway, a network security group with a rule, a network interface, a storage account, and a Linux virtual machine in Azure.
18+
19+
The script also generates a random SSH public key and associates it with the virtual machine for secure access. The public key is outputted at the end of the script execution.
20+
21+
The script uses the Random and AzAPI providers in addition to the AzureRM provider. The Random provider is used to generate a unique name for the resource group and the SSH key. The AzAPI provider is used to generate the SSH public key.
22+
23+
As with the public key, the names of the created resource group, virtual network, subnet, and NAT gateway are printed when the script is run.
24+
25+
[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)]
26+
27+
## Prerequisites
28+
29+
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
30+
31+
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
32+
33+
## Implement the Terraform code
34+
35+
> [!NOTE]
36+
> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-nat-gateway-create). You can view the log file containing the [test results from current and previous versions of Terraform](https://github.com/Azure/terraform/tree/master/quickstart/101-nat-gateway-create/TestRecord.md).
37+
>
38+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
39+
40+
1. Create a directory in which to test and run the sample Terraform code and make it the current directory.
41+
42+
1. Create a file named `main.tf` and insert the following code:
43+
44+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/main.tf":::
45+
46+
1. Create a file named `outputs.tf` and insert the following code:
47+
48+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/outputs.tf":::
49+
50+
1. Create a file named `providers.tf` and insert the following code:
51+
52+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/providers.tf":::
53+
54+
1. Create a file named `ssh.tf` and insert the following code:
55+
56+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/ssh.tf":::
57+
58+
1. Create a file named `variables.tf` and insert the following code:
59+
60+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/variables.tf":::
61+
62+
63+
## Initialize Terraform
64+
65+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
66+
67+
## Create a Terraform execution plan
68+
69+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
70+
71+
## Apply a Terraform execution plan
72+
73+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
74+
75+
## Verify the results
76+
77+
#### [Azure CLI](#tab/azure-cli)
78+
79+
1. Get the Azure resource group name.
80+
81+
```console
82+
resource_group_name=$(terraform output -raw resource_group_name)
83+
```
84+
85+
1. Get the NAT gateway ID.
86+
87+
```console
88+
nat_gateway=$(terraform output -raw nat_gateway)
89+
```
90+
91+
1. Run [az network nat gateway show](/cli/azure/network/nat/gateway#az-network-nat-gateway-show) to display the details about the NAT gateway.
92+
93+
```azurecli
94+
az network nat gateway show --resource-group $resource_group_name \
95+
--ids $nat_gateway
96+
97+
```
98+
99+
#### [Azure PowerShell](#tab/azure-powershell)
100+
101+
1. Get the Azure resource group name.
102+
103+
```console
104+
$resource_group_name=$(terraform output -raw resource_group_name)
105+
```
106+
107+
1. Get the NAT gateway ID.
108+
109+
```console
110+
$nat_gateway=$(terraform output -raw nat_gateway)
111+
```
112+
113+
1. Run [Get-AzNatGateway](/powershell/module/az.network/get-aznatgateway) to display the the details about the NAT gateway.
114+
115+
```azurepowershell
116+
Get-AzNatGateway -ResourceGroupName $resource_group_name `
117+
-Name $nat_gateway
118+
```
119+
120+
---
121+
122+
## Clean up resources
123+
124+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
125+
126+
## Troubleshoot Terraform on Azure
127+
128+
[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot)
129+
130+
## Next steps
131+
132+
> [!div class="nextstepaction"]
133+
> [Learn more about using Terraform in Azure](/azure/terraform)

articles/nat-gateway/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ items:
1919
- name: ARM template
2020
displayName: Resource Manager
2121
href: quickstart-create-nat-gateway-template.md
22+
- name: Terraform
23+
href: quickstart-create-nat-gateway-terraform.md
2224
- name: Tutorials
2325
items:
2426
- name: Configure dual-stack outbound with a NAT gateway

0 commit comments

Comments
 (0)