How to see all running Amazon EC2 instances across all regions?

前端 未结 17 1252
囚心锁ツ
囚心锁ツ 2021-01-29 23:03

I switch instances between different regions frequently and sometimes I forget to turn off my running instance from a different region. I couldn\'t find any way to see all the r

17条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-29 23:32

    Based on @hansaplast code I created Windows friendly version that supports multiple profiles as an argument. Just save that file as cmd or bat file. You also need to have jq command.

    @echo off 
    setlocal enableDelayedExpansion
    
    set PROFILE=%1
    IF "%1"=="" (SET PROFILE=default)
    
    echo checkin instances in all regions for %PROFILE% account
    FOR /F "tokens=* USEBACKQ" %%F IN (`aws ec2 describe-regions --query Regions[*].[RegionName] --output text --profile %PROFILE%`) DO (
    echo === region: %%F
    aws ec2 describe-instances --region %%F --profile %PROFILE%| jq ".Reservations[].Instances[] | {type: .InstanceType, state: .State.Name, tags: .Tags, zone: .Placement.AvailabilityZone}"
    )
    

提交回复
热议问题