Created
June 20, 2025 13:32
-
-
Save michaelsanford/a4a4b9e8fe93471f097a1dd47866a129 to your computer and use it in GitHub Desktop.
AWS CLI to count ECS Fargate vCPUs in the account
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$totalVcpus = 0 | |
# Get all ECS clusters | |
$clusters = aws ecs list-clusters --query 'clusterArns[]' --output text | |
foreach ($cluster in $clusters -split "`t") { | |
if ([string]::IsNullOrWhiteSpace($cluster)) { continue } | |
# Get all services in the cluster | |
$services = aws ecs list-services --cluster $cluster --query 'serviceArns[]' --output text | |
foreach ($service in $services -split "`t") { | |
if ([string]::IsNullOrWhiteSpace($service)) { continue } | |
# Get service details to check launch type | |
$launchType = aws ecs describe-services --cluster $cluster --services $service --query 'services[0].launchType' --output text | |
if ($launchType -eq "FARGATE") { | |
# Get task definition ARN | |
$taskDef = aws ecs describe-services --cluster $cluster --services $service --query 'services[0].taskDefinition' --output text | |
# Get vCPU from task definition | |
$vcpu = [int](aws ecs describe-task-definition --task-definition $taskDef --query 'taskDefinition.cpu' --output text) | |
# Get running task count | |
$runningCount = [int](aws ecs describe-services --cluster $cluster --services $service --query 'services[0].runningCount' --output text) | |
# Convert CPU units to vCPUs (1024 CPU units = 1 vCPU) | |
$vcpuPerTask = $vcpu / 1024 | |
$serviceVcpus = $vcpuPerTask * $runningCount | |
$totalVcpus += $serviceVcpus | |
$serviceName = Split-Path $service -Leaf | |
Write-Host "Service: $serviceName - Tasks: $runningCount - vCPUs per task: $vcpuPerTask - Total vCPUs: $serviceVcpus" | |
} | |
} | |
# Also check standalone tasks | |
$tasks = aws ecs list-tasks --cluster $cluster --query 'taskArns[]' --output text | |
foreach ($task in $tasks -split "`t") { | |
if ([string]::IsNullOrWhiteSpace($task)) { continue } | |
# Get task details | |
$taskInfo = aws ecs describe-tasks --cluster $cluster --tasks $task --query 'tasks[0].[launchType,taskDefinitionArn]' --output text | |
$taskInfoParts = $taskInfo -split "`t" | |
$launchType = $taskInfoParts[0] | |
$taskDef = $taskInfoParts[1] | |
if ($launchType -eq "FARGATE") { | |
# Get vCPU from task definition | |
$vcpu = [int](aws ecs describe-task-definition --task-definition $taskDef --query 'taskDefinition.cpu' --output text) | |
$vcpuPerTask = $vcpu / 1024 | |
$totalVcpus += $vcpuPerTask | |
$taskName = Split-Path $task -Leaf | |
Write-Host "Standalone Task: $taskName - vCPUs: $vcpuPerTask" | |
} | |
} | |
} | |
Write-Host "Total Fargate vCPUs in account: $totalVcpus" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
total_vcpus=0 | |
# Get all ECS clusters | |
clusters=$(aws ecs list-clusters --query 'clusterArns[]' --output text) | |
for cluster in $clusters; do | |
# Get all services in the cluster | |
services=$(aws ecs list-services --cluster "$cluster" --query 'serviceArns[]' --output text) | |
for service in $services; do | |
# Get service details to check launch type | |
launch_type=$(aws ecs describe-services --cluster "$cluster" --services "$service" --query 'services[0].launchType' --output text) | |
if [ "$launch_type" = "FARGATE" ]; then | |
# Get task definition ARN | |
task_def=$(aws ecs describe-services --cluster "$cluster" --services "$service" --query 'services[0].taskDefinition' --output text) | |
# Get vCPU from task definition | |
vcpu=$(aws ecs describe-task-definition --task-definition "$task_def" --query 'taskDefinition.cpu' --output text) | |
# Get running task count | |
running_count=$(aws ecs describe-services --cluster "$cluster" --services "$service" --query 'services[0].runningCount' --output text) | |
# Convert CPU units to vCPUs (1024 CPU units = 1 vCPU) | |
vcpu_per_task=$((vcpu / 1024)) | |
service_vcpus=$((vcpu_per_task * running_count)) | |
total_vcpus=$((total_vcpus + service_vcpus)) | |
echo "Service: $(basename "$service") - Tasks: $running_count - vCPUs per task: $vcpu_per_task - Total vCPUs: $service_vcpus" | |
fi | |
done | |
# Also check standalone tasks | |
tasks=$(aws ecs list-tasks --cluster "$cluster" --query 'taskArns[]' --output text) | |
for task in $tasks; do | |
# Get task details | |
task_info=$(aws ecs describe-tasks --cluster "$cluster" --tasks "$task" --query 'tasks[0].[launchType,taskDefinitionArn]' --output text) | |
launch_type=$(echo "$task_info" | cut -f1) | |
task_def=$(echo "$task_info" | cut -f2) | |
if [ "$launch_type" = "FARGATE" ]; then | |
# Get vCPU from task definition | |
vcpu=$(aws ecs describe-task-definition --task-definition "$task_def" --query 'taskDefinition.cpu' --output text) | |
vcpu_per_task=$((vcpu / 1024)) | |
total_vcpus=$((total_vcpus + vcpu_per_task)) | |
echo "Standalone Task: $(basename "$task") - vCPUs: $vcpu_per_task" | |
fi | |
done | |
done | |
echo "Total Fargate vCPUs in account: $total_vcpus" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment