User Tools

Site Tools


python:python-boto3

Python - boto3 library for AWS

Boto3 is AWS's python library for interacting with the AWS API. As everything (even the console under the surface) is API based, boto3 allows you to do pretty much everything you can do elsewhere. It may not always be up to date enough to follow new feateres, so ensure you have a up to date copy of the library with pip.

Get account Id or number

import boto3

def get_account_number():
    stsclient = boto3.client("sts")
    response = stsclient.get_caller_identity()["Account"]
    return response

print(get_account_number())

GetListOfAccounts.py

#!/usr/bin/env python
 
import boto3
import sys
 
profile = 'nonprod_admin'
context = 'dummy'
 
session = boto3.session.Session(profile_name=profile)
ec2 = session.client('ec2')
 
ASsession = boto3.session.Session(profile_name=profile)
ec2as = ASsession.client('autoscaling')
 
# ec2 = boto3.client('ec2')
# ec2as = boto3.client('autoscaling')
 
 
def GetListOfAccounts(AMIimageID, context):
    try:
        response = ec2.describe_image_attribute(ImageId=AMIimageID['ImageID'], Attribute='launchPermission')
    except:
        print ('Exception! Error with AWS response.')
 
    # print ('LC = ', response['LaunchPermissions'])
    return (response['LaunchPermissions'])
 
 
try:
    Accounts = GetListOfAccounts(AMIimageID, context)
except:
    print ('Exception! Not enough arguments supplied.', sys.exc_info())
python/python-boto3.txt · Last modified: by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki