Skip to content

Commit 34be910

Browse files
Merge pull request #245231 from TomArcherMsft/UserStory125304
User Story 125304
2 parents b0270d9 + 843c862 commit 34be910

File tree

1 file changed

+48
-34
lines changed

1 file changed

+48
-34
lines changed

articles/virtual-machines/linux/quick-create-terraform.md

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ ms.service: virtual-machines
66
ms.collection: linux
77
ms.topic: quickstart
88
ms.workload: infrastructure
9-
ms.date: 08/31/2022
9+
ms.date: 07/24/2023
1010
ms.author: tarcher
1111
ms.custom: devx-track-terraform
12+
content_well_notification:
13+
- AI-contribution
1214
---
1315

1416
# Quickstart: Use Terraform to create a Linux VM
@@ -17,55 +19,58 @@ ms.custom: devx-track-terraform
1719

1820
Article tested with the following Terraform and Terraform provider versions:
1921

20-
- [Terraform v1.2.7](https://releases.hashicorp.com/terraform/)
21-
- [AzureRM Provider v.3.20.0](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs)
22-
2322
This article shows you how to create a complete Linux environment and supporting resources with Terraform. Those resources include a virtual network, subnet, public IP address, and more.
2423

2524
[!INCLUDE [Terraform abstract](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)]
2625

2726
In this article, you learn how to:
2827
> [!div class="checklist"]
29-
30-
> * Create a virtual network
31-
> * Create a subnet
32-
> * Create a public IP address
33-
> * Create a network security group and SSH inbound rule
34-
> * Create a virtual network interface card
35-
> * Connect the network security group to the network interface
36-
> * Create a storage account for boot diagnostics
37-
> * Create SSH key
38-
> * Create a virtual machine
39-
> * Use SSH to connect to virtual machine
40-
41-
> [!NOTE]
42-
> The example code in this article is located in the [Microsoft Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-vm-with-infrastructure). See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
28+
> * Create a random value for the Azure resource group name using [random_pet](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet).
29+
> * Create an Azure resource group using [azurerm_resource_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group).
30+
> * Create a virtual network (VNET) using [azurerm_virtual_network](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_network).
31+
> * Create a subnet using [azurerm_subnet](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet).
32+
> * Create a public IP using [azurerm_public_ip](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/public_ip).
33+
> * Create a network security group using [azurerm_network_security_group](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group).
34+
> * Create a network interface using [azurerm_network_interface](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface).
35+
> * Create an association between the network security group and the network interface using [azurerm_network_interface_security_group_association](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_interface_security_group_association).
36+
> * Generate a random value for a unique storage account name using [random_id](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id).
37+
> * Create a storage account for boot diagnostics using [azurerm_storage_account](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/storage_account).
38+
> * Create a Linux VM using [azurerm_linux_virtual_machine](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_virtual_machine)
39+
> * Create an AzAPI resource [azapi_resource](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource).
40+
> * Create an AzAPI resource to generate an SSH key pair using [azapi_resource_action](https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource_action).
4341
4442
## Prerequisites
4543

46-
[!INCLUDE [open-source-devops-prereqs-azure-subscription.md](~/azure-dev-docs-pr/articles/includes/open-source-devops-prereqs-azure-subscription.md)]
47-
4844
- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure)
4945

5046
## Implement the Terraform code
5147

48+
> [!NOTE]
49+
> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-vm-with-infrastructure). 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-vm-with-infrastructure/TestRecord.md).
50+
>
51+
> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform)
52+
5253
1. Create a directory in which to test the sample Terraform code and make it the current directory.
5354

5455
1. Create a file named `providers.tf` and insert the following code:
5556

56-
[!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/providers.tf)]
57+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/providers.tf":::
58+
59+
1. Create a file named `ssh.tf` and insert the following code:
60+
61+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/ssh.tf":::
5762

5863
1. Create a file named `main.tf` and insert the following code:
5964

60-
[!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/main.tf)]
65+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/main.tf":::
6166

6267
1. Create a file named `variables.tf` and insert the following code:
6368

64-
[!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/variables.tf)]
69+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/variables.tf":::
6570

6671
1. Create a file named `outputs.tf` and insert the following code:
6772

68-
[!code-terraform[master](~/terraform_samples/quickstart/101-vm-with-infrastructure/outputs.tf)]
73+
:::code language="Terraform" source="~/terraform_samples/quickstart/101-vm-with-infrastructure/outputs.tf":::
6974

7075
## Initialize Terraform
7176

@@ -81,28 +86,37 @@ In this article, you learn how to:
8186

8287
## Verify the results
8388

84-
To use SSH to connect to the virtual machine, do the following steps:
89+
#### [Azure CLI](#tab/azure-cli)
8590

86-
1. Run [terraform output](https://www.terraform.io/cli/commands/output) to get the SSH private key and save it to a file.
91+
1. Get the Azure resource group name.
8792

8893
```console
89-
terraform output -raw tls_private_key > id_rsa
94+
resource_group_name=$(terraform output -raw resource_group_name)
9095
```
9196

92-
1. Run [terraform output](https://www.terraform.io/cli/commands/output) to get the virtual machine public IP address.
97+
1. Run [az vm list](/cli/azure/vm#az-vm-list) with a [JMESPath](/cli/azure/query-azure-cli) query to display the names of the virtual machines created in the resource group.
9398

94-
```console
95-
terraform output public_ip_address
99+
```azurecli
100+
az vm list \
101+
--resource-group $resource_group_name \
102+
--query "[].{\"VM Name\":name}" -o table
96103
```
97104

98-
1. Use SSH to connect to the virtual machine.
105+
#### [Azure PowerShell](#tab/azure-powershell)
106+
107+
1. Get the Azure resource group name.
99108

100109
```console
101-
ssh -i id_rsa azureuser@<public_ip_address>
110+
$resource_group_name=$(terraform output -raw resource_group_name)
102111
```
103112

104-
**Key points:**
105-
- Depending on the permissions of your environment, you might get an error when trying to ssh into the virtual machine using the `id_rsa` key file. If you get an error stating that the private key file is unprotected and can't be used, try running the following command: `chmod 600 id_rsa`, which will restrict read and write access to the owner of the file.
113+
1. Run [Get-AzVm](/powershell/module/az.compute/get-azvm) to display the names of all the virtual machines in the resource group.
114+
115+
```azurepowershell
116+
Get-AzVm -ResourceGroupName $resource_group_name
117+
```
118+
119+
---
106120

107121
## Clean up resources
108122

0 commit comments

Comments
 (0)