2 min read

AWS Direct Connect Utilization Analysis

I recently began the task of cleaning up AWS Direct Connect connections that were no longer in use within our network. Unlike some other services, AWS does not readily indicate whether a Direct Connect connection is actively in use.

If you're not familiar, AWS Direct Connect lets you hook up your on-prem network directly to AWS.

We had about 85 Direct Connect connections spread out over various AWS regions. Not all of these were active, and manually checking each one wasn't a practical solution. Plus, these connections are managed by different teams and external partners, so just turning one off to see if someone complains wasn't an option—reactivating them can be a real pain.

I went with Python for the script because it's straightforward and widely used. Boto3 is the go-to AWS SDK for Python, letting you directly manage AWS resources. It was the perfect tool for automating checks across our AWS services.

The script does a few simple things:

  1. Connects to AWS Direct Connect to get a list of all our connections.
  2. Pulls data transfer metrics from CloudWatch for each connection.
  3. Calculates the average data transfer rates to figure out if a connection is active or just sitting idle.

Here’s a peek at part of the script:

import boto3
from datetime import datetime, timedelta

# Initialize clients for Direct Connect and CloudWatch
cloudwatch = boto3.client('cloudwatch')
directconnect = boto3.client('directconnect')

# Set up the time period to check for the last 30 days
end_time = datetime.now()
start_time = end_time - timedelta(days=30)

# Fetch all Direct Connect connections
dx_connections = directconnect.describe_connections()['connections']

for connection in dx_connections:
    connection_id = connection['connectionId']
    print(f"Checking metrics for Direct Connect ID: {connection_id}")

    # Check data transfer rates
    for metric_name in ['DataInRate', 'DataOutRate']:
        response = cloudwatch.get_metric_statistics(
            Namespace='AWS/DX',
            MetricName=metric_name,
            Dimensions=[{'Name': 'ConnectionId', 'Value': connection_id}],
            StartTime=start_time,
            EndTime=end_time,
            Period=3600,  # One hour
            Statistics=['Average']
        )

        # Analyze the response
        if response['Datapoints']:
            average = sum(d['Average'] for d in response['Datapoints']) / len(response['Datapoints'])
            print(f" - {metric_name} average: {average} Mbps")
        else:
            print(f" - {metric_name} has no data. This connection might not be in use.")

I’ve put this script on GitHub so anyone can use it, tweak it, or improve it. It’s all there in the README on how to set it up and run it.

Eventually, we found out that none of the Direct Connect connections were being used anymore. We ended up removing all of them. However, automating this check has saved us a lot of time.