Skip to main content

How to Clean Up AWS Load Balancers

When compute instances are removed, their load balancers are sometimes left behind.

Fix Inventory's cleanup-aws-loadbalancers infrastructure app can find and delete these orphaned load balancers.

Prerequisites​

This guide assumes that you have already installed and configured Fix Inventory to collect your AWS resources.

Directions​

  1. Execute the following command in Fix Inventory Shell to open the Fix Inventory Worker configuration for editing:

    > config edit fix.worker
  2. Enable cleanup by modifying the fixworker section of the configuration as follows:

    fixworker:
    # Enable cleanup of resources
    cleanup: true
    # Do not actually cleanup resources, just create log messages
    cleanup_dry_run: false
    # How many cleanup threads to run in parallel
    cleanup_pool_size: 16

    When cleanup is enabled, marked resources will be deleted as a part of the collect_and_cleanup workflow, which runs each hour by default.

    tip

    Set cleanup_dry_run to true to simulate cleanup without actually deleting resources.

  3. Use the app install command to install the cleanup-aws-loadbalancers app:

    > app install cleanup-aws-loadbalancers
    info

    Fix Inventory will create a default config fix.apps.cleanup_aws_loadbalancers.

  4. Execute the following command in Fix Inventory Shell to open the infrastructure app configuration for editing:

    > config edit fix.apps.cleanup_aws_loadbalancers
  5. Update the configuration and set the min_age property to the desired threshold for load balancer cleanup:

    cleanup_aws_loadbalancers configuration
    min_age: 7d
    note

    The above infrastructure app configuration cleans up ELBs, ALBs, and ALB target groups older than the minimum age with no attached backends. Items tagged with expiration: never will not be flagged for cleanup.

  6. Run the app using the app run command:

    > app run cleanup-aws-loadbalancers
    tip

    Add the optional --dry-run flag to see what commands the app would perform, without actually executing them.

    note

    Items tagged with expiration: never will not be flagged for cleanup.

  7. Create an event-based job to run the app automatically:

    > jobs create --name "Clean Up AWS Load Balancers" --wait-for-event cleanup_plan 'app run cleanup-aws-loadbalancers'
    info

    The cleanup-aws-loadbalancers infrastructure app will now run each time Fix Inventory emits the cleanup_plan event. The post_cleanup_plan event is emitted in the cleanup phase of the collect_and_cleanup workflow.

Each time the cleanup-aws-loadbalancers infrastructure app runs, orphaned load balancers will be flagged for removal during the next cleanup run.

Further Reading​