title | description | ms.topic | ms.date | ms.custom | ms.service | author | ms.author | content_well_notification | |
---|---|---|---|---|---|---|---|---|---|
Quickstart: Use Terraform to deploy Azure Bastion | In this quickstart, you learn how to use Terraform to create Azure resources for an Azure Bastion deployment.' | quickstart | 01/22/2025 | devx-track-terraform | azure-bastion | cherylmc | cherylmc |
|
In this Quickstart, you learn how to use Terraform to deploy Azure Bastion 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:
:::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":::
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 Bastion Developer instead, see Quickstart: Connect with Azure Bastion Developer. See the Azure Bastion deployment guidance for more information about how to customize your Azure Bastion deployment.
[!INCLUDE About Terraform]
[!div class="checklist"]
- Create an Azure resource group with a unique name.
- Establish a virtual network with a specified name and address.
- Set up a subnet specifically for Azure Bastion within the created virtual network.
- Allocate a static, standard public IP for Azure Bastion within the resource group.
- Construct an Azure Bastion host with a specified IP configuration within the resource group.
- Output the names and IP address of the resource group, plus the Azure Bastion host.
Create an Azure account with an active subscription. You can create an account for free.
Note
The sample code for this article is located in the Azure Terraform GitHub repo. You can view the log file containing the test results from current and previous versions of Terraform.
See more articles and sample code showing how to use Terraform to manage Azure resources.
Create a directory in which to test and run the sample Terraform code, and make it the current directory.
Create a file named
main.tf
, and insert the following code: :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/main.tf":::Create a file named
outputs.tf
, and insert the following code: :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/outputs.tf":::Create a file named
providers.tf
, and insert the following code: :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/providers.tf":::Create a file named
variables.tf
, and insert the following code: :::code language="Terraform" source="~/terraform_samples/quickstart/101-azure-bastion-host/variables.tf":::
[!INCLUDE terraform-init.md]
[!INCLUDE terraform-plan.md]
[!INCLUDE terraform-apply-plan.md]
Get the Azure resource group name.
resource_group_name=$(terraform output -raw resource_group_name)
Get the Azure Bastion host name.
bastion_host_name=$(terraform output -raw bastion_host_name)
Get the Azure Bastion host ip address.
bastion_host_ip=$(terraform output -raw bastion_host_ip)
Run
az network bastion show
to view the Azure Bastion host.az network bastion show --name $bastion_host_name --resource-group $resource_group_name
Get the Azure resource group name.
$resource_group_name=$(terraform output -raw resource_group_name)
Get the Azure Bastion host name.
$bastion_host_name=$(terraform output -raw bastion_host_name)
Get the Azure Bastion host ip address.
$bastion_host_ip=$(terraform output -raw bastion_host_ip)
Run
Get-AzBastionHost
to view the Azure Bastion host.Get-AzBastionHost -ResourceGroupName $resource_group_name -Name $bastion_host_name
[!INCLUDE terraform-plan-destroy.md]
Troubleshoot common problems when using Terraform on Azure.
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.
[!div class="nextstepaction"] Azure Bastion and Terraform articles