|
| 1 | +--- |
| 2 | +title: 'Quickstart: Use Terraform to deploy Azure Bastion' |
| 3 | +description: In this quickstart, you learn how to use Terraform to create Azure resources for an Azure Bastion deployment.' |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 01/22/2025 |
| 6 | +ms.custom: devx-track-terraform |
| 7 | +ms.service: azure-bastion |
| 8 | +author: cherylmc |
| 9 | +ms.author: cherylmc |
| 10 | +#customer intent: As a Terraform user, I want to learn how to use Terraform to create Azure resources that set up Azure Bastion host. The Azure resources in an Azure Bastion deployment include an Azure resource group, a virtual network, an Azure Bastion subnet, a public IP, and an Azure Bastion host. |
| 11 | +content_well_notification: |
| 12 | + - AI-contribution |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Use Terraform to deploy Azure Bastion |
| 16 | + |
| 17 | +In this Quickstart, you learn how to use Terraform to deploy [Azure Bastion](bastion-overview.md) automatically in the Azure portal. To do this, you create an Azure Bastion host and its corresponding Azure resources, which include a resource group, virtual network, Azure Bastion subnet, and a public IP. This setup ensures a secure, private network environment for your Azure services. The following diagram provides an overview of Azure Bastion deployments: |
| 18 | + |
| 19 | +:::image type="content" source="./media/create-host/host-architecture.png" alt-text="Diagram that shows the Azure Bastion architecture." lightbox="./media/create-host/host-architecture.png"::: |
| 20 | + |
| 21 | +Deploying Azure Bastion allows you to use RDP and SSH to access to your virtual machines within the Azure portal. This service is provisioned directly in your virtual network and supports all virtual machines there, reducing exposure to public network connections. When you deploy Bastion automatically, Bastion is deployed with the Standard SKU. To deploy with the Developer SKU instead, see [Quickstart: Deploy Azure Bastion - Developer SKU](quickstart-developer-sku.md). See the [Azure Bastion deployment guidance](quickstart-host-portal.md) for more information about how to customize your Azure Bastion deployment. |
| 22 | + |
| 23 | +[!INCLUDE [About Terraform](~/azure-dev-docs-pr/articles/terraform/includes/abstract.md)] |
| 24 | + |
| 25 | +> [!div class="checklist"] |
| 26 | +> * Create an Azure resource group with a unique name. |
| 27 | +> * Establish a virtual network with a specified name and address. |
| 28 | +> * Set up a subnet specifically for Azure Bastion within the created virtual network. |
| 29 | +> * Allocate a static, standard public IP for Azure Bastion within the resource group. |
| 30 | +> * Construct an Azure Bastion host with a specified IP configuration within the resource group. |
| 31 | +> * Output the names and IP address of the resource group, plus the Azure Bastion host. |
| 32 | +
|
| 33 | +## Prerequisites |
| 34 | + |
| 35 | +- Create an Azure account with an active subscription. You can [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 36 | + |
| 37 | +- [Install and configure Terraform](/azure/developer/terraform/quickstart-configure). |
| 38 | + |
| 39 | +## Implement the Terraform code |
| 40 | + |
| 41 | +> [!NOTE] |
| 42 | +> The sample code for this article is located in the [Azure Terraform GitHub repo](https://github.com/Azure/terraform/tree/master/quickstart/101-azure-bastion-host). 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-azure-bastion-host/TestRecord.md). |
| 43 | +> |
| 44 | +> See more [articles and sample code showing how to use Terraform to manage Azure resources](/azure/terraform). |
| 45 | +
|
| 46 | +1. Create a directory in which to test and run the sample Terraform code, and make it the current directory. |
| 47 | + |
| 48 | +1. Create a file named `main.tf`, and insert the following code: |
| 49 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/main.tf"::: |
| 50 | + |
| 51 | +1. Create a file named `outputs.tf`, and insert the following code: |
| 52 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/outputs.tf"::: |
| 53 | + |
| 54 | +1. Create a file named `providers.tf`, and insert the following code: |
| 55 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/providers.tf"::: |
| 56 | + |
| 57 | +1. Create a file named `variables.tf`, and insert the following code: |
| 58 | + :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/variables.tf"::: |
| 59 | + |
| 60 | +## Initialize Terraform |
| 61 | + |
| 62 | +[!INCLUDE [terraform-init.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-init.md)] |
| 63 | + |
| 64 | +## Create a Terraform execution plan |
| 65 | + |
| 66 | +[!INCLUDE [terraform-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan.md)] |
| 67 | + |
| 68 | +## Apply a Terraform execution plan |
| 69 | + |
| 70 | +[!INCLUDE [terraform-apply-plan.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-apply-plan.md)] |
| 71 | + |
| 72 | +## Verify the results |
| 73 | + |
| 74 | +### [Azure CLI](#tab/azure-cli) |
| 75 | + |
| 76 | +1. Get the Azure resource group name. |
| 77 | + |
| 78 | + ```console |
| 79 | + resource_group_name=$(terraform output -raw resource_group_name) |
| 80 | + ``` |
| 81 | + |
| 82 | +1. Get the Azure Bastion host name. |
| 83 | + |
| 84 | + ```console |
| 85 | + bastion_host_name=$(terraform output -raw bastion_host_name) |
| 86 | + ``` |
| 87 | + |
| 88 | +1. Get the Azure Bastion host ip address. |
| 89 | + |
| 90 | + ```console |
| 91 | + bastion_host_ip=$(terraform output -raw bastion_host_ip) |
| 92 | + ``` |
| 93 | + |
| 94 | +1. Run [`az network bastion show`](/cli/azure/network/bastion#az-network-bastion-show) to view the Azure Bastion host. |
| 95 | + |
| 96 | + ```azurecli |
| 97 | + az network bastion show --name $bastion_host_name --resource-group $resource_group_name |
| 98 | + ``` |
| 99 | + |
| 100 | +### [Azure PowerShell](#tab/azure-powershell) |
| 101 | + |
| 102 | +1. Get the Azure resource group name. |
| 103 | + |
| 104 | + ```console |
| 105 | + $resource_group_name=$(terraform output -raw resource_group_name) |
| 106 | + ``` |
| 107 | + |
| 108 | +1. Get the Azure Bastion host name. |
| 109 | + |
| 110 | + ```console |
| 111 | + $bastion_host_name=$(terraform output -raw bastion_host_name) |
| 112 | + ``` |
| 113 | + |
| 114 | +1. Get the Azure Bastion host ip address. |
| 115 | + |
| 116 | + ```console |
| 117 | + $bastion_host_ip=$(terraform output -raw bastion_host_ip) |
| 118 | + ``` |
| 119 | + |
| 120 | +1. Run [`Get-AzBastionHost`](/powershell/module/az.network/get-azbastion) to view the Azure Bastion host. |
| 121 | + |
| 122 | + ```azurepowershell |
| 123 | + Get-AzBastionHost -ResourceGroupName $resource_group_name -Name $bastion_host_name |
| 124 | + ``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Clean up resources |
| 129 | + |
| 130 | +[!INCLUDE [terraform-plan-destroy.md](~/azure-dev-docs-pr/articles/terraform/includes/terraform-plan-destroy.md)] |
| 131 | + |
| 132 | +## Troubleshoot Terraform on Azure |
| 133 | + |
| 134 | +[Troubleshoot common problems when using Terraform on Azure](/azure/developer/terraform/troubleshoot). |
| 135 | + |
| 136 | +## Next steps |
| 137 | + |
| 138 | +In this quickstart, you used Terraform to create an Azure resource group and other complimentary Azure resources to set up an Azure Bastion host. Next, you can explore the following resources to learn more about Azure Bastion and Terraform. |
| 139 | + |
| 140 | +> [!div class="nextstepaction"] |
| 141 | +> [Azure Bastion and Terraform articles](/search/?terms=Azure%20bastion%20host%20and%20terraform) |
0 commit comments