Package: easy-boto3
boto3made easyeasy_boto3simplifiesboto3usage by adding a command line interface (CLI) and abridged Python API that allows you to easily create, manage, and tear-down AWS resources usingboto3andawscliin a simple, easy to use, and easy to refactor.yamlconfiguration file.Contentsboto3made easyContentsInstallationUsingeasy_boto3CLIManaging ec2 instancesCreating an ec2 instance with cloudwatch alarmShow instance cloud_init logsShow instance syslog logsListing ec2 instancesStopping an ec2 instanceStarting a stopped an ec2 instanceTermianting ec2 instances by idManaging AWS profilesList all AWS profiles in~/.aws/credentialsList active AWS profile (currently used byeasy_boto3)Set active AWS profile (currently used byeasy_boto3)Usingeasy_boto3's Python APICreating an ec2 instanceInstallationYou can installeasy_boto3viapipaspipinstalleasy-boto3Usingeasy_boto3CLIManaging ec2 instancesCreating an ec2 instance with cloudwatch alarmeasy_boto3allows you to translate a standardboto3pythonic infrastructure task like instantiating anec2instance with an attachedcloudwatchcpu usage alarm from complex pythonic implementation like the followingimportboto3# read in aws_access_key_id and aws_secret_access_key based on input profile_name using boto3session=boto3.Session(profile_name=profile_name)# create ec2 controller from sessionec2_controller=session.resource('ec2')# read in startup scriptwithopen(startup_script_path,'r')asfile:startup_script=file.read()# create a new EC2 instanceinstances=ec2_controller.create_instances(ImageId='ami-03f65b8614a860c29',InstanceName='example_worker',NetworkInterfaces=[{'DeviceIndex':0,'Groups':['sg-1ed8w56f12347f63d'],'AssociatePublicIpAddress':True}],UserData=startup_script,TagSpecifications=[{'ResourceType':'instance','Tags':[{'Key':'Name','Value':'example_worker'}]}],InstanceType='t2.micro',KeyName=<ssh_key_name>,)# wait for the instance to enter running stateinstances[0].wait_until_running()instance_id=instances[0].id# create cloud watch clientcloudwatch_client=session.client('cloudwatch')# enable detailed monitoring for the instanceec2_client.monitor_instances(InstanceIds=[instance_id])# create alarmresult=cloudwatch_client.put_metric_alarm(AlarmName=cpu_alarm_name,ComparisonOperator='GreaterThanOrEqualToThreshold',EvaluationPeriods=1,MetricName='CPUUtilization',Namespace='AWS/EC2',Period=60,Statistic='Average',Threshold=threshold_value,Dimensions=[{'Name':'InstanceId','Value':instance_id},],)into easier to re / use and refactor.yamlconfiguration file using the sameboto3option syntax for to declaration of the same task.  So for example the above task can be accomplished using the analogous.yamlconfiguration file carrying over the sameboto3option syntax as follows:aws_profile:<your profile name in config/credentials of ~/.aws>ec2_instance:instance_details:InstanceName:example_workerInstanceType:t2.microImageId:ami-03f65b8614a860c29BlockDeviceMappings:DeviceName:/dev/sda1Ebs:DeleteOnTermination:trueVolumeSize:8VolumeType:gp2Groups:-<your security group>ssh_details:Config:User:ubuntuIdentityFile:<path to ssh key>ForwardAgent:yesOptions:add_to_known_hosts:truetest_connection:truescript_details:filepath:<path_to_startup>inject_aws_creds:truessh_forwarding:truegithub_host:truealarm_details:ComparisonOperator:GreaterThanOrEqualToThresholdEvaluationPeriods:1MetricName:CPUUtilizationNamespace:AWS/EC2Period:60Statistic:AverageThreshold:0.99Usingeasy_boto3and this configurationconfig.yamlthe same task - instantiating anec2instance - can be accomplished via the command line as follows:easy_boto3ec2createconfig.yamlShow instance cloud_init logseasy_boto3ec2check_cloud_init_logs<instance_id>Show instance syslog logseasy_boto3ec2check_syslog<instance_id>Listing ec2 instancesYou can useeasy_boto3to easy see (all/ running / stopped / terminated) instances in your AWS account as follows.See all instanceseasy_boto3ec2list_allSee just running instanceseasy_boto3ec2list_runningThe output of this command gives the instance id, name, type, and state of each instance in your account - looking like this{'instance_id':'instance_id','instance_state':'running','instance_type':'t2.micro'}You can filter by state - running, stopped, terminated - as followseasy_boto3ec2list_runningeasy_boto3ec2list_stoppedeasy_boto3ec2list_terminatedStopping an ec2 instanceeasy_boto3ec2stop<instance_id>Starting a stopped an ec2 instanceeasy_boto3ec2start<instance_id>Termianting ec2 instances by idYou can useeasy_boto3CLI to terminate an ec2 instance by id as followseasy_boto3ec2terminate<instance_id>Note: by default this will delete any cloudwatch alarms associated with the instance.Managing AWS profilesYou can useeasy_boto3CLI to manage AWS profiles as followsList all AWS profiles in~/.aws/credentialseasy_boto3profilelist_allList active AWS profile (currently used byeasy_boto3)easy_boto3profilelist_activeSet active AWS profile (currently used byeasy_boto3)easy_boto3profileset<profile_name>Usingeasy_boto3's Python APIIn addition to config driven command line use,easy_boto3also offers a simplified python API that makes creating and managing AWS resources withboto3easier.Creating an ec2 instanceIn this example an ec2 instance of user-specified type and AMI is created.Noteblock_device_mappingsandUserDatastartup bash script are optional.fromeasy_boto3importset_profilefromeasy_boto3.startup_script_managementimportread_startup_scriptfromeasy_boto3.ec2_instance_managementimportlaunch_instance# set aws profile - optional - set to 'default' profile by defaultset_profile.set('my_aws_profile')# -> returns None if profile is valid# read in startup script from fileUserData=read_startup_script('./path/to/startup.sh')# build ec2 launch instance commandInstanceName='example_worker'InstanceType='t2.micro'ImageId='ami-03f65b8614a860c29'Groups=['my_security_group_id']BlockDeviceMappings=[{'DeviceName':'/dev/sda1','Ebs':{'VolumeSize':300,'VolumeType':'gp2'}}]KeyName='my_ssh_key_name'# launch instancelaunch_result=launch_instance(KeyName=KeyName,InstanceName=InstanceName,InstanceType=InstanceType,ImageId=ImageId,Groups=Groups,BlockDeviceMappings=BlockDeviceMappings,UserData=UserData)# wait for the instance to enter running statelaunch_result.wait_until_running()# get instance idinstance_id=launch_result[0].idFurther uses of the Python API can be found in theexamples/python_apidirectory.