Python boto3를 이용한 AWS 리소스 컨트롤 - EC2현황 확인

2019-12-17

.

import boto3
ec2client = boto3.client('ec2')
response = ec2client.describe_instances(Filters=[{"Name" : "tag:Name", "Values" : ["minman"]}])
for reservation in response["Reservations"]:
    for instance in reservation['Instances']:
        print(instance)
import boto3
session = boto3.session.Session(profile_name="default")
ec2_cli = boto3.client('ec2')
for region in ec2_cli.describe_regions()["Regions"]:
    ec2_cli = session.client(service_name="ec2", region_name=region["RegionName"])
    print(region["RegionName"])
    try:
        response = ec2_cli.describe_instances()
        for reservation in response["Reservations"]:
            for instance in reservation["Instances"]:
                print(instance["InstanceId"], instance["PrivateIpAddress"], instance["Tags"])
    except Exception as e:
        print(e)
eu-north-1
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-2
i-05ce1f505042e2007 172.31.27.251 [{'Key': 'Name', 'Value': 'minman'}]
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2
import boto3
session = boto3.session.Session(profile_name="default")
ec2_cli = boto3.client('ec2')
for region in ec2_cli.describe_regions()["Regions"]:
    ec2_cli = session.client(service_name="ec2", region_name=region["RegionName"])
    print(region["RegionName"])
    try:
        response = ec2_cli.describe_instances()
        for reservation in response["Reservations"]:
            for instance in reservation["Instances"]:
                if 'terminated' == str(instance.get("State").get('Name')) or 'pending' == str(instance.get("State").get('Name')):
                    print("###################"+instance.get("State").get('Name'))
                else:
                    try:
                        for tags in instance['Tags']:
                            if tags["Key"] == 'Name':
                                vTag = tags["Value"]
                                print(vTag)
                                print(instance.get("PrivateIpAddress"))
                                print(instance.get("InstanceId"))
                                print("#####################################")
                            else:
                                print("No Tag!!!")
                                print(instance.get("PrivateIpAddress"))
                                print(instance.get("InstanceId"))
                                print("*************************************")
                    except Exception as e:
                        print(e)
                        
    except Exception as e1:
        print(e1)
eu-north-1
ap-south-1
eu-west-3
eu-west-2
eu-west-1
ap-northeast-2
minman
172.31.27.251
i-05ce1f505042e2007
#####################################
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2
# s3에서 GLACIER 제외 파일들을 뽑아냄
bucket_name = 'minman'
import boto3
s3_res = boto3.resource('s3')
each_bucket = s3_res.Bucket(bucket_name)
version = each_bucket.objects
for ver in version.all():
    if (ver.storage_class) in 'GLACIER':
        pass
    else:
        print(ver.key)
        print('##########')
        print(ver.storage_class)