Save Time and Money by Automatically Deleting Unused Azure Load Balancers
Published: 2021-01-26 19:30:00

---
title: Save Time and Money by Automatically Deleting Unused Azure Load Balancers
published: true
date: 2021-01-12 00:00:00 UTC
tags: 
canonical_url: https://relay.sh/blog/save-time-and-money-by-automatically-deleting-unused-azure-load-balancers/
---

 ![](https://relay.sh/static/8b4ceff77a8c32b96fb20578ddb20f06/0ff54/dollar-4492709_1280.jpg)

Using the cloud reduces on-premises infrastructure costs and related maintenance. Instead of deploying more servers, storage, and networking components to your own datacenter, you are now deploying these as cloud resources. Using the cloud is supposed to reduce infrastructure and maintenance costs. However, deploying cloud resources also risks over-commissioning, under-usage, and keeping resources running that are not always needed or, even worse, no longer in use.

To help you avoid wasting these unused resources, Puppet created Relay. This tool enables you to automate DevOps cloud maintenance, including automatically cleaning up resources you no longer need. This reduces resource waste while saving DevOps time, helping your team focus on delivering exciting new product features.

# Cleaning up Azure

In this article, we walk you through a common scenario. You may be using Azure Infrastructure components, like Virtual Machines and related Virtual Networking resources, together with Azure Load Balancers. When you no longer need the Virtual Machine, and delete it, you may forget about the Azure Load Balancer. The Load Balancer then continues to use your valuable cloud computing resources. Relay workflow helps you clean up.

To use Relay:

1. Activate your Relay account at [relay.sh](https://relay.sh).
2. Define a connection between Relay and Azure, using an Azure service principal.
3. Create your Relay workflow, which then performs the cleanup.

Let’s examine each of these steps with step-by-step guidance. If you already have an Azure subscription with administrative access, you can follow these steps in your own environment.

# Setting Up Relay

Setting up a Relay account is rather straightforward. It is a hosted-cloud service with nothing to download, install, update, or maintain.

First, create a new Relay account by completing the required fields on the [signup page](https://app.relay.sh/signup).

[![Relay sign up screen](https://relay.sh/static/016c2de45ba4f756051ff3ddf4bfb395/0342e/image1.png "Relay sign up screen")](https://relay.sh/static/016c2de45ba4f756051ff3ddf4bfb395/0342e/image1.png)

After accepting the activation link in your email, create a complex password, confirm it, and that’s all it takes.

[![Welcome to Relay screen](https://relay.sh/static/7a54529869b1bc8659e60a44bf14d771/347c0/image2.png "Welcome to Relay screen")](https://relay.sh/static/7a54529869b1bc8659e60a44bf14d771/347c0/image2.png)

Then, after successfully logging on to the Relay platform, you are ready to start. From the Relay portal, browse sample workflows or create a new one from scratch.

[![Relay Sidebar](https://relay.sh/static/5d528dc9fdc1bcf90ba78665a7272684/4c61e/image3.png "Relay Sidebar")](https://relay.sh/static/5d528dc9fdc1bcf90ba78665a7272684/4c61e/image3.png)

There is also an option to define _connections_, where you specify the service account of your cloud platform. Relay supports many different public and private cloud environments, including Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP), and Kubernetes. In our example, we will use Azure, but the process is similar in all environments.

[![Add connection](https://relay.sh/static/c886bf087c40d9386c590062247e1338/7ecec/image4.png "Add connection")](https://relay.sh/static/c886bf087c40d9386c590062247e1338/7ecec/image4.png)

# Configuring Access to Azure

Before creating a workflow, let’s start by defining a connection for Azure. This relies on an Azure Active Directory service principal. Think of this object as just another user account, used by an application and similar to an Azure administrative account.

Once you create the service principal, apply Azure Role Based Access Control (RBAC) permissions, limiting this account’s administrative capabilities to keep your production environment secure. [Optionally, you can read more about service principal objects.](https://docs.microsoft.com/en-us/azure/active-directory/develop/app-objects-and-service-principals)

There are several ways to create a service principal, such as Azure Portal, Azure Cloud Shell, PowerShell, ARM Template, or REST API. We will show you how to create the service principal using Azure Cloud Shell.

First, navigate to the [Azure Portal](https://portal.azure.com), and open Azure Cloud Shell from the top right menu. Note: if this is the first time you use Azure Cloud Shell, it will ask you to create an Azure Storage Account and Azure FileShare – complete this step to continue. Select Bash as the interface.

[![Azure Cloud Shell](https://relay.sh/static/e1d8057a552568aaf91a06a420d83559/64756/image5.png "Azure Cloud Shell")](https://relay.sh/static/e1d8057a552568aaf91a06a420d83559/a66f5/image5.png)

Type the following Azure command-line interface (CLI) command:

```
az ad sp create-for-rbac -n "Relaysp"

```

This creates the service principal in your Azure Active Directory, and shows the actual account credential details as Shell output. Copy this information for later use.

[![Azure account credentials](https://relay.sh/static/434fc493443fc467098289a782aeac40/64756/image6.png "Azure account credentials")](https://relay.sh/static/434fc493443fc467098289a782aeac40/f27d7/image6.png)

Go to the [Relay portal](https://app.relay.sh) and select “Connections”. Next, click the “Add Connection” button and choose Azure from the list. This opens the “Set up your Azure Connection” window. Complete the fields, copying the information from the Cloud Shell output, as shown in the below example:

Relay Names Azure Service Connection Names

- Subscription ID - Subscription
- Client ID - appId
- Tenant ID - tenant
- Secret - password

[![Set up your Azure connection](https://relay.sh/static/62ed4eccbfed4efb68b2520630ffa716/65dc2/image7.png "Set up your Azure connection")](https://relay.sh/static/62ed4eccbfed4efb68b2520630ffa716/65dc2/image7.png)

Save the information. This creates your connection.

[![Azure connection created](https://relay.sh/static/6546b3fcbaaac2a87b3bfaa62f78e8a6/64756/image8.png "Azure connection created")](https://relay.sh/static/6546b3fcbaaac2a87b3bfaa62f78e8a6/37cfc/image8.png)

We are now ready to create our workflow.

# Creating a Workflow

There are a couple of different ways to create the workflow. Remember, it is a YAML dialect, allowing us to author the file in any intelligent text editor, like Visual Studio Code or similar.

You could write the YAML from scratch, but Relay provides an extensive open source library of sample workflows on GitHub, integrated into the Relay portal. So, let’s have a look.

First, from the Relay portal, navigate to Workflows. Select “Explore our workflow library_”._

[![Explore our workflow Library](https://relay.sh/static/740980c01291e7eee5389ff071ec9511/64756/image9.png "Explore our workflow Library")](https://relay.sh/static/740980c01291e7eee5389ff071ec9511/35d52/image9.png)

From the category list, select “Cost optimization”.

[![Select cost optimization](https://relay.sh/static/69b1f4313eda30ea1ff1d999aa2e2a15/64756/image10.png "Select cost optimization")](https://relay.sh/static/69b1f4313eda30ea1ff1d999aa2e2a15/2bea0/image10.png)

Locate the “Delete empty Azure Load Balancers” workflow.

[![Delete empty Azure Load Balancers workflow](https://relay.sh/static/fc1f715ed700241ec0ae57b980abfb2a/64756/image11.png "Delete empty Azure Load Balancers workflow")](https://relay.sh/static/fc1f715ed700241ec0ae57b980abfb2a/c807c/image11.png)

Click “Use this workflow”, provide a unique name, and confirm by pressing “Create workflow”.

[![Create Relay workflow](https://relay.sh/static/04a1e88780a15712ecb46af4fb1949d3/bb7f4/image12.png "Create Relay workflow")](https://relay.sh/static/04a1e88780a15712ecb46af4fb1949d3/bb7f4/image12.png)

This imports the workflow into your dashboard. From here, you need to complete some minimal configuration settings.

[![Workflow created](https://relay.sh/static/e348ff81f4f58fcf47fa1c99713a0a57/64756/image13.png "Workflow created")](https://relay.sh/static/e348ff81f4f58fcf47fa1c99713a0a57/7bd25/image13.png)

The first warning we get is “missing required connection”. You need to specify which Azure Connection the workflow should use, namely, the one you created in the previous step. To fix this error, click the “Fill in missing connections” link.

[![Ensure youre Azure connection is added](https://relay.sh/static/dcf7f795aa729f37831501c7061d99f1/25327/image14.png "Ensure youre Azure connection is added")](https://relay.sh/static/dcf7f795aa729f37831501c7061d99f1/25327/image14.png)

Here, it shows a default Connection of “my-azure-account”. You could add a new connection here, however, we already created one. To use it, we need to switch back to the Code view of the workflow and make a change in the YAML.

[![Update your connection in the code view](https://relay.sh/static/cb8881aa0932d72e1e992439dc7d8656/64756/image15.png "Update your connection in the code view")](https://relay.sh/static/cb8881aa0932d72e1e992439dc7d8656/a1898/image15.png)

In the code editor, search for the keyword “connection” (line 27 in the sample file), and replace the name “my-azure-account” with the name of the Azure Connection you created earlier (Azure-Relay in our setup). Make sure you save the changes.

[![Updated connection](https://relay.sh/static/0ffea7fb9e65808cd1818423e5fc6d96/64756/image16.png "Updated connection")](https://relay.sh/static/0ffea7fb9e65808cd1818423e5fc6d96/55954/image16.png)

Notice how the connection is ‘recognized’ under the settings pane to the right.

From here, let’s test the workflow to ensure it can successfully connect to our Azure subscription and detect Load Balancers. Click “Run” and confirm by pressing “run workflow” in the popup window. Also, notice this flow can run in dryRun mode, which means it won’t actually change anything in our environment.

[![Run the workflow manually](https://relay.sh/static/1d9e02f9b3c92bf6ddbb85f7fb2bffb0/64756/image17.png "Run the workflow manually")](https://relay.sh/static/1d9e02f9b3c92bf6ddbb85f7fb2bffb0/684d5/image17.png)

The workflow kicks off and displays the step-by-step sequence:

[![Queued worflow](https://relay.sh/static/4bea8381799a8493fa8f8b280edcfd17/66712/image18.png "Queued worflow")](https://relay.sh/static/4bea8381799a8493fa8f8b280edcfd17/66712/image18.png)

[![Running workflow](https://relay.sh/static/632c67936671167e9824f776c7c7d03d/afe45/image19.png "Running workflow")](https://relay.sh/static/632c67936671167e9824f776c7c7d03d/afe45/image19.png)

The dry run workflow worked fine. To look at each step in a bit more detail, hover over the step, for example, “list-azure-load-balancers”, and select view logs.

[![View logs](https://relay.sh/static/784815dcc328853a7d054ba6a705373b/8c7b4/image20.png "View logs")](https://relay.sh/static/784815dcc328853a7d054ba6a705373b/8c7b4/image20.png)

The following output is displayed:

[![Final output](https://relay.sh/static/5fbcae8d05c8783f7f81facc17b27751/64756/image21.png "Final output")](https://relay.sh/static/5fbcae8d05c8783f7f81facc17b27751/e69c5/image21.png)

As you can see, the workflow lists the valid load balancers. Note: we didn’t deploy a Load Balancer yet, so it is normal that the result is zero, but at least it ran fine.

Remember, this workflow is checking for “empty load balancers”, which means it looks for Azure Load Balancers without any endpoint connection parameters. To make this a more viable test, let’s deploy a Load Balancer in Azure. If you need some assistance on how to do this, you might use this [sample from Azure QuickStart Templates](https://azure.microsoft.com/en-us/resources/templates/101-load-balancer-standard-create/).

This deploys a Standard-type Azure Load Balancer, together with three Virtual Machines (VMs) as a back-end pool. If you want, you can update the Azure deployment template to deploy only a single VM if you don’t want to test three.

[![Create a load balancer in Azure](https://relay.sh/static/410cbf0644244710c3553432fab25359/64756/image22.png "Create a load balancer in Azure")](https://relay.sh/static/410cbf0644244710c3553432fab25359/f8041/image22.png)

After deploying this template, the setup looks similar to this:

[![Load balancer list page](https://relay.sh/static/b7b5c36d3d881c83643164a2757ed011/64756/image23.png "Load balancer list page")](https://relay.sh/static/b7b5c36d3d881c83643164a2757ed011/cdade/image23.png)

Let’s go back to Relay and run our workflow once more. Notice that, this time, the Load Balancer is actually detected.

[![Logs for list load balancers step](https://relay.sh/static/8a1d091bc11e1046be96eada99301852/64756/image24.png "Logs for list load balancers step")](https://relay.sh/static/8a1d091bc11e1046be96eada99301852/dc4d2/image24.png)

Next, select the next step in the flow, filter-loadbalancers, and view logs. This reveals that the detected Load Balancer is not empty.

[![Logs for filter step](https://relay.sh/static/ce142c63d2c822e86a83394bd5afa774/64756/image25.png "Logs for filter step")](https://relay.sh/static/ce142c63d2c822e86a83394bd5afa774/27773/image25.png)

It is interesting to know how Relay identifies if the Load Balancer configuration is empty or not. Let’s have a look at the actual YAML code.

[![Input file for filtering](https://relay.sh/static/4e6349911687b195bc8613caf8265f00/64756/image26.png "Input file for filtering")](https://relay.sh/static/4e6349911687b195bc8613caf8265f00/93b57/image26.png)

The script includes a pointer to a Python script, which looks like this:

[![Logic within filtering input file](https://relay.sh/static/1f65d7c5593185e6ef235632c2b18f7a/64756/image27.png "Logic within filtering input file")](https://relay.sh/static/1f65d7c5593185e6ef235632c2b18f7a/c8502/image27.png)

You can see it is checking for any Load Balancers with empty “backend_address_pools”.

To run a more valid test, let’s go back to the Azure Load Balancer, and clear the backend pool configuration. The easiest way to remove this is by using the following PowerShell cmdlet:

```
Remove-AzureRMLoadBalancerBackendAddressPoolConfig

```

For more information on doing this, refer to the following Microsoft Doc:

[Remove-AzureRmLoadBalancerBackendAddressPoolConfig](https://docs.microsoft.com/en-us/powershell/module/azurerm.network/remove-azurermloadbalancerbackendaddresspoolconfig?view=azurermps-6.13.0)

Note: if you used the default settings from the Azure Quickstart Template sample deployment earlier, use the following PowerShell script:

```
Get-AzureRmLoadBalancer -Name "MyLB-lb" -ResourceGroupName "MyLBRG" | Remove-AzureRmLoadBalancerBackendAddressPool -Name "LoadBalancerBackendPool" | Set-AzureRmLoadBalancer

```

[![Running the Azure PowerShell script](https://relay.sh/static/ff831ee5e138edc00c42401bd88949ed/64756/image28.png "Running the Azure PowerShell script")](https://relay.sh/static/ff831ee5e138edc00c42401bd88949ed/ef7d6/image28.png)

The Load Balancer configuration shows an empty BackEndPool now:

[![Azure load balancer configuration](https://relay.sh/static/5cd702b001e1577bf894ea2ee56f76de/64756/image29.png "Azure load balancer configuration")](https://relay.sh/static/5cd702b001e1577bf894ea2ee56f76de/2c0f3/image29.png)

Run the Relay workflow once more to see what happens with the Azure Resource. To test, make sure you keep the DryRun value set as True. The result is the following:

[![Logs for filter step](https://relay.sh/static/a1e2d312819aa065c06b57191d1b6ecc/64756/image30.png "Logs for filter step")](https://relay.sh/static/a1e2d312819aa065c06b57191d1b6ecc/cb670/image30.png)

This time, the empty Load Balancer is detected for removal.

Next, trigger the workflow once more, this time setting the DryRun to False, which means it will actually force the removal of the Azure Load Balancer resource. You will also need to confirm “Yes” on the approval step in the workflow.

[![Logs for delete Azure load balancers step](https://relay.sh/static/72944be0e621febc5752e0f263e2f960/64756/image31.png "Logs for delete Azure load balancers step")](https://relay.sh/static/72944be0e621febc5752e0f263e2f960/fc2b1/image31.png)

After about a minute, we get positive confirmation saying all specified load balancers are deleted. Validating from the Azure Portal confirms this.

As a last step in this process, I want to highlight the trigger option at the beginning of the workflow. Up till now, we kicked off the workflow manually. This works fine for testing, but doesn’t work well in production. You may want to schedule this cleanup, validating your environment every week or maybe every night.

To schedule your load balancer cleanup, first, select the first step in the workflow, “Add trigger”.

[![Add a trigger](https://relay.sh/static/7f7aba1f807bea31ac782ecdf198f2cb/cec09/image32.png "Add a trigger")](https://relay.sh/static/7f7aba1f807bea31ac782ecdf198f2cb/cec09/image32.png)

This opens a list with different triggers to choose from, like running a “per day” job or triggering the workflow using an HTTP interaction. This could be interesting if you want to automate the process from your systems management tool. Whenever you connect to the trigger-HTTP-URL, the workflow executes. Let’s go back to the day-trigger scenario for now.

Select the “Run a trigger every day” option, which reflects the corresponding code. The most important setting here is “schedule”, using the standardized cron notation to define the scheduling format.

[![Add a scheduled trigger](https://relay.sh/static/6be10daed8202f532eee30570a4d190f/b38af/image33.png "Add a scheduled trigger")](https://relay.sh/static/6be10daed8202f532eee30570a4d190f/b38af/image33.png)

For more information on the cron definition, I can recommend this [helpful link](https://cron.help/examples). You now have one less thing to monitor. You can check it off your busy DevOps team’s to-do list.

# Next Steps

In this article, we introduced you to Relay, a product Puppet created to automate cloud management tasks. From first configuring the cloud connection, we explored an example scenario, cleaning up empty Azure Load Balancers. We then learned how to trigger the workflow as a daily scheduled task.

Other [example workflows](https://github.com/puppetlabs/relay-workflows) enable you to easily set up maintenance of VMs, network interface controllers (NICs), and Disks.

While this example detailed how to clean up Azure, Relay also works with other cloud environments. You can adjust the example above to develop workflows for any Azure, AWS or GCP resources.

Automating your load balancer cleanup and other DevOps tasks saves you time and money as your team instead focuses on creating great new features for your software applications. Check out [Relay](https://relay.sh/) to automate your DevOps maintenance today.