Package: ec2objects
ec2 objects (pip3 install ec2objects)everyone:I wish, for once, to just have a simple object oriented experience with the api.ec2objects:Please visitGitHubpage for documentation that has navigation that works.Table of ContentsHow to installConfigurationsRegionsRegion ManagerRetrieve All RegionsRetrieve Regions Enabled For Your AccountRegion ObjectImagesImage ManagerRetrieve Image By ImageIdRetrieve Ubuntu ImagesImage ObjectSSH Key PairsKeyPair ManagerRetrieve KeypairsRetrieve Keypair By NameRetrieve Keypair By IDRetrieve Keypair By FingerprintRetrieve Keypairs By Tag KeynameRetrieve Keypairs By TagUpload KeypairKeyPair ObjectDelete KeypairHow to installHere are your options.Install from pypi repositoryThe most popular way is to install the latest package available on pypi.You can install ec2objects usingpip3pip3 install -U ec2objectsYou can uninstall if you like using,pip3 uninstall ec2objectsInstall from the cloned git hub repoThere are a few ways to install this python package from a clone of its github repo.
Check out a copy and try the following...Build a .tar.gz install packageFrom the root of the repo build a repo, and check the repo.python3 setup.py sdist
twine check dist/*Check the newly created dist directory for newly created .tar.gz files.
This is your .tar.gz package and you can install using...pip3 install ./dist/ec2objects-0.0.17.tar.gzYou can still uninstall using the same commands,pip3 uninstall ec2objectsInstall using the setup.py file!WARNING! Install does not track which files, and where they are placed.
So, you need to keep a record of there python3 does this.This is how... from the github repo root directory.sudo python3 setup.py install --record files.txtYou can uninstall using by playing back that files.txt file,sudo xargs rm -rf < files.txtLocal interactive installUsing this method you can modify this packages code and have changes immediately available.
Perfect for if you want to tinker with the library, poke around and contribute to the project.From the cloned repository root directory.pip3 install -e ./You can uninstall using the usual command,pip3 uninstall ec2objects⬆ back to topConfigurationsBOTO 3ec2objects usesboto3to interact with the amazon api.
Decided to go with boto3 as it gives nice json responses instead of the xml mess direct interaction with the api gives.You can find further token requirements and details at the above link.Here is are the quick start requirements for ec2objects.Token: RequiredSet the AWS_ACCESS_KEY_ID, and the AWS_SECRET_ACCESS_KEY environment variables with your amazon aws access credentials.export AWS_ACCESS_KEY_ID='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

export AWS_SECRET_ACCESS_KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'Default Region: RequiredPlease set the AWS_DEFAULT_REGION environment variable to a region you know you  have enabled for your credentials.For example:export AWS_DEFAULT_REGION='us-east-1'For relevant commands you can specify a completely different region if you need to act on a different region.⬆ back to topRegionsAnAWS Regionis a collection of AWS resources in a geographic area.Each AWS Region is isolated and independent of the other Regions.Regions provide fault tolerance, stability, and resilience, and can also reduce latency. They enable you to create redundant resources that remain available and unaffected by a Regional outage.Import the ec2object Region and RegionManger to interact with Regions.fromec2objectsimportRegion,RegionManagerRegion ManagerCreate a region manager.region_manager=RegionManager()Retrieve All RegionsRetrieve a list of region objects.list_of_all_region_objects=region_manager.retrieve_all_regions()Retrieve Regions Enabled For Your AccountRetrieve a list of region objects enabled for your account.list_of_enabled_region_objects=region_manager.retrieve_regions_enabled_for_my_account()Region ObjectRegion objects contains an attributes data class with the standard ec2region attributes.Region objects also contain a list of availability zones data classes for that region.classRegion:def__init__(self):self.attributes=RegionAttributes()self.availabilityzones=[]...@dataclassclassRegionAttributes:RegionName:str=NoneEndpoint:str=NoneOptInStatus:str=None@dataclassclassAvailabilityZones:State:str=NoneOptInStatus:str=NoneMessages:list=field(default_factory=list)RegionName:str=NoneZoneName:str=NoneZoneId:str=NoneGroupName:str=NoneNetworkBorderGroup:str=NoneZoneType:str=NoneParentZoneName:str=NoneParentZoneId:str=None⬆ back to topImagesAn Amazon Machine Image (AMI User Guide) provides the information required to launch an instance.AMI BOTO3 API GUIDEdetails at this link.An AMI includes the following:One or more Amazon Elastic Block Store (Amazon EBS) snapshots, or, for instance-store-backed AMIs, a template for the root volume of the instance (for example, an operating system, an application server, and applications).Launch permissions that control which AWS accounts can use the AMI to launch instances.A block device mapping that specifies the volumes to attach to the instance when it's launched.Import the ec2object Image and ImageManger to interact with Regions.fromec2objectsimportImage,ImageManagerImage ManagerCreate an image manager.image_manager=ImageManager()Retrieve Image By ImageIdRetrieve an image object by ImageId.image_object=image_manager.retrieve_image("ami-fd534b97")Retrieve Ubuntu ImagesOkay, so the AWS documentation is a bit difficult.
Going to break it down and try to extract and common sense.. thanks aws.The following methods retrieve Ubuntu images that have...Architechture:'x86_64' or 'arm64'
ImageType:'machine'hvm-ssd:ImportantWhat does this mean?
The instance created from this image(an hvm-ssd) will have small root storage,
/dev/sda1, to hold the OS.
/dev/sda1 is ephemeral storage is thevolatile temporary storage attached to your instanceswhich is only present during the running lifetime of the instance. Data will be available on restart but will be destroyed on instance termination.For longer term data storage you do need to attach an existing Elastic Block Storage (EBS) device for or create and attach new EBS and attach that....and HVM?HVM (known asHardware Virtual Machine) is the type of instance that mimics bare-metal server setup which provides better hardware isolation. With this instance type, the OS can run directly on top of the Virtual Machine without additional configuration making it to look like it is running on a real physical server.Retrieve All x86_64 Ubuntu hvm-ssd machine imagesRetrieve a list of image objects.Argument 'name' is optional and can be excluded entirely.
You can request individual Ubuntu releases using the first string of the release code names found athttps://wiki.ubuntu.com/Releasese.g. name="focal", name="bionic", name="trusty"list_of_image_objects=image_manager.retrieve_all_ubuntu_x86_64_machine_images_hvm_ssd(self,name="")Retrieve All arm64 Ubuntu hvm-ssd machine imagesRetrieve a list of image objects.Argument 'name' is optional and can be excluded entirely.
You can request individual Ubuntu releases using the first string of the release code names found athttps://wiki.ubuntu.com/Releasese.g. name="focal", name="bionic", name="trusty"list_of_image_objects=image_manager.retrieve_all_ubuntu_arm64_machine_images_hvm_ssd(self,name="")⬆ back to topImage ObjectImage objects contains an attributes data class with the standard ec2image attributes, descriptions available at this link.Each object also contains a list of ebsblockdevices, and a list of virtuablockdevices - empty if none exists.ebsblockdevices  holds a list of BlockDeviceMappingEBS if they exist.virtualblockdevices holds a list of BlockDeviceMappingVirtual if they exist.classImage:def__init__(self):self.attributes=ImageAttributes()self.ebsblockdevices=[]self.virtualblockdevices=[]@dataclassclassImageAttributes:Architecture:str=NoneCreationDate:str=NoneImageId:str=NoneImageLocation:str=NoneImageType:str=NonePublic:bool=NoneKernelId:str=NoneOwnerId:str=NonePlatform:str=NonePlatformDetails:str=NoneUsageOperation:str=NoneRamdiskId:str=NoneState:str=NoneDescription:str=NoneEnaSupport:bool=NoneHypervisor:str=NoneImageOwnerAlias:str=NoneName:str=NoneRootDeviceName:str=NoneRootDeviceType:str=NoneSriovNetSupport:str=NoneVirtualizationType:str=NoneBootMode:str=NoneDepricationTime:str=None@dataclassclassBlockDeviceMappingEBS:DeviceName:str=NoneSnapshotId:str=NoneVolumeSize:str=NoneDeleteOnTermination:bool=NoneVolumeType:str=NoneEncrypted:bool=None@dataclassclassBlockDeviceMappingVirtual:DeviceName:str=NoneVirtualName:str=None⬆ back to topSSH Key PairsAkey pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an EC2 instance.
Import the ec2object KeyPair and KeyPairManger to interact with SSH Key Pairs.fromec2objectsimportKeyPair,KeyPairManagerKeyPair ManagerCreate a keypair manager.keypair_manager=KeyPairManager()Retrieve KeypairsRetrieve all of your keypairs.list_of_keypair_objects=keypair_manager.retrieve_keypairs()Retrieve Keypair By NameRetrieve all of your keypairs by keypair name.
AWS keypair names are unique.list_of_keypair_objects=keypair_manager.retrieve_keypair_by_name(str:name)Retrieve Keypair By IDRetrieve all of your keypairs by keypair ID.list_of_keypair_objects=keypair_manager.retrieve_keypair_by_id(str:keypairid)Retrieve Keypair By FingerprintRetrieve all of your keypairs by key fingerprint.list_of_keypair_objects=keypair_manager.retrieve_keypair_by_fingerprint(str:fingerprint)Retrieve Keypairs By Tag KeynameRetrieve all of your keypairs by the name of a tag key, not it's actual tag content.list_of_keypair_objects=keypair_manager.retrieve_keypair_by_tag_keyname(str:tagkeyname)Retrieve Keypairs By TagRetrieve all of your keypairs by supplying the name of a tag, and the tag value.list_of_keypair_objects=keypair_manager.retrieve_keypair_by_tag(str:key,str:value)Upload KeypairAWS accepts the following ssh key types: (ssh-rsa, ssh-ed25519)
Exception KeyPairTypeNotSupportedByAWS will be raised if attempt is made to upload an unsupported key type.
This method will return ONE keypair object with the details of the new key uploaded to AWS.uploadedkey=keypair_manager.upload_keypair(str:keyname,str:ssh_public_key,dict:tags)uploadedkey=keypair_manager.upload_keypair(str:keyname,str:ssh_public_key)where you provide tags for your keypair in the form of a dict, e.g.tags={}tags["tag1"]="value1"tags["apple"]="banana"⬆ back to topKeyPair ObjectA keypair object contains the attributes, and tags for an AWS keypair.classKeyPair:def__init__(self):self.attributes=KeyPairAttributes()self.tags=[]...@dataclassclassKeyPairAttributes:KeyPairId:str=NoneKeyFingerprint:str=NoneKeyName:str=NoneKeyType:str=None@dataclassclassTags:key:str=NoneValue:str=NoneDelete KeypairYou can delete a keypair from AWS by calling the delete method on your keypair_object.keypair_object.delete()⬆ back to top