Last active
April 8, 2019 16:42
-
-
Save graham-thomson/16cc05338530dc0c1b52f7413d6a30ef to your computer and use it in GitHub Desktop.
always growing list of aws boto3 helper functions in python3
This file contains 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
import boto3 | |
import urllib.request | |
import re | |
from urllib.error import URLError | |
from subprocess import run, PIPE | |
def get_instance_id(timeout=5): | |
try: | |
return urllib.request.urlopen( | |
"http://169.254.169.254/latest/meta-data/instance-id", | |
timeout=timeout | |
).read().decode() | |
except URLError: | |
return None | |
def get_instance_name(region="us-east-1"): | |
ec2 = boto3.resource("ec2", region) | |
instance = ec2.Instance(get_instance_id()) | |
for t in instance.tags: | |
for k in t.keys(): | |
if t[k] == "Name": | |
return t["Value"] | |
return None | |
def return_ec2_region(): | |
avregex = r"^(us|eu|ap|sa|cn)-([a-zA-Z-]{4,10})-(\d)(\w{0,2})$" | |
avz = urllib.request.urlopen("http://169.254.169.254/latest/meta-data/placement/availability-zone/").read().decode() | |
return "-".join(re.search(avregex, avz).groups()[:3]) if re.match(avregex, avz) else None | |
def aws_file_size(s3_path, oom="gb", profile=None): | |
output = run( | |
"""aws s3 ls {profile} {s3_path}""".format( | |
profile="--profile {}".format(profile) if profile else "", | |
s3_path=s3_path | |
), | |
shell=True, | |
stdout=PIPE | |
).stdout.decode().split("\n") | |
output.pop() | |
total_bytes = sum([int(p.split()[2]) for p in output]) | |
if oom == "mb": | |
size = total_bytes/1e6 | |
elif oom == "kb": | |
size = total_bytes/1e3 | |
elif oom == "tb": | |
size = total_bytes/1e12 | |
else: | |
size = total_bytes/1e9 | |
print("Found {} files, totaling {:.02f} {}(s).".format(len(output), size, oom)) | |
return size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment