Introduction
Azure Bastion is a fully managed service that provides secure and seamless RDP and SSH connectivity to your virtual machines directly through the Azure portal. By leveraging Azure Bastion, you can enhance the security of your virtual network without exposing your VMs to the public internet. However, to further optimise costs and resource usage, you can automate the creation and deletion of Azure Bastion instances based on a schedule using Azure Logic Apps and Automation Accounts.
Step-by-Step Implementation Guide
Step 1: Create an Azure Automation Account
- Sign in to the Azure portal.
- Click on “Create a resource” and search for “Automation”.
- Click “Create” and fill in the required details:
- Name: Enter a name for your Automation Account.
- Resource Group: Create a new resource group or select an existing one.
- Location: Choose the region closest to your resources.
- Click “Review + create” and then “Create”.
Step 2: Create a Runbook to Delete Azure Bastion
-
Navigate to your Automation Account in the Azure portal.
-
Click on “Runbooks” in the left-hand menu and then “Create a runbook”.
-
Fill in the required details:
- Name: Enter a name for your runbook (e.g., DeleteBastion).
- Runbook type: Select “PowerShell”.
-
Click “Create”.
-
In the runbook editor, enter the following PowerShell script to delete the Azure Bastion instance:
1 2 3 4 5 6
param ( [string]$ResourceGroupName, [string]$BastionName ) Remove-AzBastion -ResourceGroupName $ResourceGroupName -Name $BastionName
-
Click “Save” and then “Publish”.
Step 3: Create a Runbook to Create Azure Bastion
-
Repeat the steps to create another runbook, but this time name it “CreateBastion”.
-
In the runbook editor, enter the following PowerShell script to create an Azure Bastion instance:
1 2 3 4 5 6 7 8 9
param ( [string]$ResourceGroupName, [string]$BastionName, [string]$VNetName, [string]$SubnetName ) $bastionConfig = New-AzBastionConfig -Name $BastionName -VirtualNetworkName $VNetName -SubnetName $SubnetName -ResourceGroupName $ResourceGroupName New-AzBastion -ResourceGroupName $ResourceGroupName -Name $BastionName -BastionConfig $bastionConfig
-
Click “Save” and then “Publish”.
Step 4: Create a Logic App to Schedule the Runbooks
- Navigate to the Azure portal.
- Click on “Create a resource” and search for “Logic App”.
- Click “Create” and fill in the required details:
- Name: Enter a name for your Logic App.
- Resource Group: Create a new resource group or select an existing one.
- Location: Choose the region closest to your resources.
- Click “Review + create” and then “Create”.
Step 5: Configure the Logic App
- In the Logic App Designer, select “Recurrence” as the trigger and set the schedule for when you want to delete and create the Azure Bastion instance.
- Add an action to “Create Job” for the “DeleteBastion” runbook in your Automation Account.
- Add another action to “Create Job” for the “CreateBastion” runbook in your Automation Account.
- Configure the parameters for each action (e.g., ResourceGroupName, BastionName, VNetName, SubnetName).
- Save and run your Logic App.
Conclusion
By automating the creation and deletion of Azure Bastion instances based on a schedule, you can optimise your deployment and reduce costs. Azure Logic Apps and Automation Accounts provide a powerful combination to achieve this automation seamlessly.