Skip to content

Commit 05891ae

Browse files
authored
Merge pull request #266908 from asudbring/nat-terraform-qs
Terraform quickstart article for Azure NAT Gateway
2 parents 0132b59 + 2f61921 commit 05891ae

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 an Azure NAT Gateway using Terraform
16+
17+
Get started with Azure NAT Gateway using Terraform. This Terraform file deploys a virtual network, a NAT gateway resource, and Ubuntu virtual machine. The Ubuntu virtual machine is deployed to a subnet that is associated with the NAT gateway resource.
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+
:::image type="content" source="./media/quickstart-create-nat-gateway-portal/nat-gateway-qs-resources.png" alt-text="Diagram of resources created in nat gateway quickstart.":::
28+
29+
## Prerequisites
30+
31+
- An Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
32+
33+
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure).
34+
35+
## Implement the Terraform code
36+
37+
> [!NOTE]
38+
> 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).
39+
>
40+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
41+
42+
1. Create a directory in which to test and run the sample Terraform code and make it the current directory.
43+
44+
1. Create a file named `main.tf` and insert the following code:
45+
46+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/main.tf":::
47+
48+
1. Create a file named `outputs.tf` and insert the following code:
49+
50+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/outputs.tf":::
51+
52+
1. Create a file named `providers.tf` and insert the following code:
53+
54+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/providers.tf":::
55+
56+
1. Create a file named `ssh.tf` and insert the following code:
57+
58+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/ssh.tf":::
59+
60+
1. Create a file named `variables.tf` and insert the following code:
61+
62+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-nat-gateway-create/variables.tf":::
63+
64+
65+
## Initialize Terraform
66+
67+
[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)]
68+
69+
## Create a Terraform execution plan
70+
71+
[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)]
72+
73+
## Apply a Terraform execution plan
74+
75+
[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)]
76+
77+
## Verify the results
78+
79+
#### [Azure CLI](#tab/azure-cli)
80+
81+
1. Get the Azure resource group name.
82+
83+
```console
84+
resource_group_name=$(terraform output -raw resource_group_name)
85+
```
86+
87+
1. Get the NAT gateway ID.
88+
89+
```console
90+
nat_gateway=$(terraform output -raw nat_gateway)
91+
```
92+
93+
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.
94+
95+
```azurecli
96+
az network nat gateway show \
97+
--resource-group $resource_group_name \
98+
--ids $nat_gateway
99+
```
100+
101+
#### [Azure PowerShell](#tab/azure-powershell)
102+
103+
1. Get the Azure resource group name.
104+
105+
```console
106+
$resource_group_name=$(terraform output -raw resource_group_name)
107+
```
108+
109+
1. Get the NAT gateway ID.
110+
111+
```console
112+
$nat_gateway=$(terraform output -raw nat_gateway)
113+
```
114+
115+
1. Run [Get-AzNatGateway](/powershell/module/az.network/get-aznatgateway) to display the details about the NAT gateway.
116+
117+
```azurepowershell
118+
$nat = @{
119+
Name = $nat_gateway
120+
ResourceGroupName = $resource_group_name
121+
}
122+
Get-AzNatGateway @nat
123+
```
124+
125+
---
126+
127+
## Clean up resources
128+
129+
[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)]
130+
131+
## Troubleshoot Terraform on Azure
132+
133+
[Troubleshoot common problems when using Terraform on Azure.](/azure/developer/terraform/troubleshoot)
134+
135+
## Next steps
136+
137+
> [!div class="nextstepaction"]
138+
> [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)