├── 1.Introduction and Environmet setup for boto3 ├── 10.Document-custom-and-default-sessions.txt ├── 5.Document-Installing-python-3.x-and-boto3-on-Linux-Server.txt ├── 6.Document-Environment-setup-to-work-with-boto3-on-Windows.pdf ├── 7.Document-Environment-setup-to-work-with-boto3-on-Linux.pdf ├── 8.Document-verify-the-environment-setup.txt └── 9.Document-about-session-client-resouce.txt ├── 11.Working with IAM Users ├── 2.Location_of_scripts.txt ├── 3.Get All Groups Info using Python script.txt ├── 6.Create an IAM User with programatic Access Keys.py ├── 7.Create an IAM User with Programatic and AWS Managment Console Login Access.txt ├── 8.Create mutliple IAM Users using python boto3.txt └── 9.IAM Users Inventory Script.txt ├── 12.Lambda-PART-1 AWS Automation with boto3 and Lambda Functions └── 5.Automate Snapshots for EBS Volumes using resource object.py ├── 2.Boto3 Documentation help to implement Python boto3 Scripts ├── 1.Document-help-on-client-object.py └── 2.Document-help-on-resource-object.py ├── 3.Working with STS └── 1.Document-get-aws-account-id.py ├── 4.EC2-PART-1 Working with EC2 Services ├── 1.1.Document-listing-some-of-the-ec2-services-using-client.py ├── 1.2.Document-listing-some-of-the-ec2-services-using-resource-object.py ├── 2.1.Document-menu-driven-script-on-ec2-actions-using-client.py └── 2.2.Document-menu-driven-script-on-ec2-actions-using-resource.py ├── 5.EC2 Waiters └── 1.Document-introduction-to-waiter.py ├── 6.Usage of Meta object └── 1.Document-list-all-regions-for-ec2.py ├── 7.EC2 Collections ├── 1.Document-introduction-to-collections.py └── 2.Document-start-stop-terminate-all-instances-at-once.py ├── 8.EC2-PART-2 Practice with EC2 Services ├── 1.Document-delete-unused-untagged-volumes-using-resource.py ├── 2.Document-EC2-Instance-Discovery-Script-using-resource.py ├── 3.Document-list-all-snaps-based-on-size.py └── 4.Document-list-all-snaps-based-on-size-1.py ├── LICENSE └── README.md /1.Introduction and Environmet setup for boto3/10.Document-custom-and-default-sessions.txt: -------------------------------------------------------------------------------- 1 | aws configure --profile p_name 2 | 3 | [root] 4 | aws_access_key_id = AKIAJRCMPX5GEZQLWWTA 5 | aws_secret_access_key = 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 6 | [ec2_developer] 7 | aws_access_key_id = AKIA5XNJJZL57MQ4WGWM 8 | aws_secret_access_key = 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 9 | [s3_developer] 10 | aws_access_key_id = AKIA5XNJJZL5QC3Q7IUX 11 | aws_secret_access_key = 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 12 | ==================================================================== 13 | Custom Session: 14 | 15 | import boto3 16 | aws_mag_con=boto3.session.Session(profile_name="root") 17 | 18 | iam_con_re=aws_mag_con.resource(service_name='iam',region_name="us-east-2") 19 | iam_con_client=aws_mag_con.client(service_name='iam',region_name="us-east-2") 20 | 21 | =============================================================================== 22 | Default: 23 | 24 | import boto3 25 | iam_con_re=boto3.resource(service_name="iam",region_name="us-east-1") 26 | 27 | =============================================================================== 28 | 29 | 30 | import boto3 31 | aws_mag_con=boto3.session.Session(profile_name="root") 32 | 33 | ec2 = aws_mag_con.resource('ec2') 34 | --------------------------------------------------------- -------------------------------------------------------------------------------- /1.Introduction and Environmet setup for boto3/5.Document-Installing-python-3.x-and-boto3-on-Linux-Server.txt: -------------------------------------------------------------------------------- 1 | You can get the steps from the url: https://tecadmin.net/install-python-3-7-on-centos/ 2 | 3 | or 4 | Use below steps: 5 | 6 | yum install gcc openssl-devel bzip2-devel libffi-devel 7 | cd /usr/src 8 | wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz 9 | tar xzf Python-3.7.4.tgz 10 | cd Python-3.7.4 11 | ./configure --enable-optimizations 12 | make altinstall 13 | cd /usr/local/bin/ 14 | ./python3.7 --version 15 | ./pip3.7 --version 16 | pwd 17 | ln -s /usr/local/bin/python3.7 /bin/python3 18 | python3 --versio 19 | ln -s /usr/local/bin/pip3.7 /bin/pip3 20 | pip3 --version 21 | pip3 install boto3 22 | 23 | 24 | -------------------------------------------------------------------------------- /1.Introduction and Environmet setup for boto3/6.Document-Environment-setup-to-work-with-boto3-on-Windows.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-AWS-Automation-with-boto3-Python-and-Lambda-Functions/d71e3b4d1a646f8c8aec0b5fb3d06299c6c35e72/1.Introduction and Environmet setup for boto3/6.Document-Environment-setup-to-work-with-boto3-on-Windows.pdf -------------------------------------------------------------------------------- /1.Introduction and Environmet setup for boto3/7.Document-Environment-setup-to-work-with-boto3-on-Linux.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learn-AWS-Automation-with-boto3-Python-and-Lambda-Functions/d71e3b4d1a646f8c8aec0b5fb3d06299c6c35e72/1.Introduction and Environmet setup for boto3/7.Document-Environment-setup-to-work-with-boto3-on-Linux.pdf -------------------------------------------------------------------------------- /1.Introduction and Environmet setup for boto3/8.Document-verify-the-environment-setup.txt: -------------------------------------------------------------------------------- 1 | Manual Steps to see/list all iam users: 2 | ======================================== 3 | step1: Get AWS Management Console 4 | Step2: Get IAM Console 5 | Options: Users, Groups, roles...... 6 | ======================================== 7 | import boto3 8 | 9 | aws_mag_con=boto3.session.Session(profile_name="root") 10 | iam_con=aws_mag_con.resource('iam') 11 | 12 | for each_user in iam_con.users.all(): 13 | print(each_user.name) 14 | ======================================== 15 | 16 | import boto3 17 | aws_mag_con=boto3.session.Session(profile_name="root") 18 | s3_con=aws_mag_con.resource('s3') 19 | ============================================================== 20 | -------------------------------------------------------------------------------- /1.Introduction and Environmet setup for boto3/9.Document-about-session-client-resouce.txt: -------------------------------------------------------------------------------- 1 | Manual Steps to see/list all iam users: 2 | ======================================== 3 | step1: Get AWS Management Console 4 | Step2: Get IAM Console 5 | Options: Users, Groups, roles...... 6 | ======================================== 7 | import boto3 8 | 9 | aws_mag_con_root=boto3.session.Session(profile_name="root") 10 | #aws_mag_con_root=boto3.session.Session(profile_name="ec2_developer") 11 | 12 | iam_con_re=aws_mag_con_root.resource(service_name='iam',region_name="us-east-2") 13 | iam_con_cli=aws_mag_con_root.client(service_name='iam',region_name="us-east-2") 14 | 15 | #Listiing iam users with resource object: 16 | 17 | for each_user in iam_con_re.users.all(): 18 | print(each_user.name) 19 | 20 | #Listing iam users with client object: 21 | 22 | for each in iam_con_cli.list_users()['Users']: 23 | print(each['UserName']) 24 | 25 | 26 | ====================================================== -------------------------------------------------------------------------------- /11.Working with IAM Users/2.Location_of_scripts.txt: -------------------------------------------------------------------------------- 1 | Location of all python boto3 scripts for this course 2 | 3 | You will get all python boto3 scripts for this course from my Github link. 4 | 5 | Github link is: https://github.com/AutomationWithScripting/UdemyBoto3Scripts -------------------------------------------------------------------------------- /11.Working with IAM Users/3.Get All Groups Info using Python script.txt: -------------------------------------------------------------------------------- 1 | Get All Groups Info 2 | 3 | Write a Python boto3 script to get info about all groups present in your aws account ? -------------------------------------------------------------------------------- /11.Working with IAM Users/6.Create an IAM User with programatic Access Keys.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import boto3 3 | from random import choice 4 | import sys 5 | 6 | 7 | 8 | def get_iam_client_object(): 9 | session=boto3.session.Session(profile_name="dev_root") 10 | iam_client=session.client(service_name="iam",region_name="us-east-1") 11 | return iam_client 12 | 13 | def main(): 14 | iam_client=get_iam_client_object() 15 | Iam_user_name="dowithpython@gmail.com" 16 | 17 | PolicyArn="arn:aws:iam::aws:policy/AdministratorAccess" 18 | try: 19 | iam_client.create_user(UserName=Iam_user_name) 20 | except Exception as e: 21 | if e.response['Error']['Code']=="EntityAlreadyExists": 22 | print "Already Iam User with {} is exist".format(Iam_user_name) 23 | sys.exit(0) 24 | else: 25 | print "Please verify the following error and retry" 26 | print e 27 | sys.exit(0) 28 | response = iam_client.create_access_key(UserName=Iam_user_name) 29 | print "IAM User Name={}".format(Iam_user_name) 30 | print "AccessKeyId={}\nSecretAccessKey={}".format(response['AccessKey']['AccessKeyId'],response['AccessKey']['SecretAcc 31 | essKey']) 32 | 33 | iam_client.attach_user_policy(UserName=Iam_user_name,PolicyArn=PolicyArn) 34 | return None 35 | 36 | if __name__=="__main__": 37 | main() 38 | -------------------------------------------------------------------------------- /11.Working with IAM Users/7.Create an IAM User with Programatic and AWS Managment Console Login Access.txt: -------------------------------------------------------------------------------- 1 | It is an assignment: you can complete it by adding previous two lectures. 2 | -------------------------------------------------------------------------------- /11.Working with IAM Users/8.Create mutliple IAM Users using python boto3.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Take list of iam users in a csv file like 4 | 5 | 6 | S_NO, IAM_User_Name,Programatic_Access,Console_Access,PolicyARN 7 | 8 | 1,XYZ, Yes,No,arn:aws:iam::aws:policy/AdministratorAccess 9 | 10 | 2.pqr,Yes,Yes,arn:aws:iam::aws:policy/AdministratorAccess 11 | 12 | 3.abc,No,Yes,arn:aws:iam::aws:policy/AmazonAPIGatewayInvokeFullAccess 13 | -------------------------------------------------------------------------------- /11.Working with IAM Users/9.IAM Users Inventory Script.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | Write a Python boto3 script to export IAM User Details into a csv file. 4 | 5 | 6 | CSV file content is like IAM User Name, User Id, User ARN, User Creation Date, Attached Policies and Groups associated for IAM Users 7 | -------------------------------------------------------------------------------- /12.Lambda-PART-1 AWS Automation with boto3 and Lambda Functions/5.Automate Snapshots for EBS Volumes using resource object.py: -------------------------------------------------------------------------------- 1 | #!/bin/python 2 | import boto3 3 | from pprint import pprint 4 | 5 | session=boto3.session.Session(profile_name="dev_root") 6 | ec2_re=session.resource(service_name="ec2",region_name="us-east-1") 7 | vol_ids=[] 8 | #colleting volume Ids 9 | for each_vol in ec2_re.volumes.filter(Filters=[]): 10 | #print each_vol.id 11 | vol_ids.append(each_vol.id) 12 | 13 | print 'All volume ids are: ',vol_ids 14 | 15 | #Creating snapshots for volumes one by one 16 | snap_ids=[] 17 | for each_vo_id in vol_ids: 18 | response= ec2_re.create_snapshot( 19 | Description='Snap with Lambda', 20 | VolumeId=each_vo_id, 21 | TagSpecifications=[ 22 | { 23 | 'ResourceType': 'snapshot', 24 | 'Tags': [ 25 | { 26 | 'Key': 'Delete-on', 27 | 'Value':'90' 28 | } 29 | ] 30 | } 31 | ] 32 | ) 33 | snap_ids.append(response.id) 34 | 35 | print snap_ids 36 | #Creating waiter using client 37 | ec2_cli=session.client(service_name="ec2",region_name="us-east-1") 38 | waiter = ec2_cli.get_waiter('snapshot_completed') 39 | waiter.wait(SnapshotIds=snap_ids) 40 | -------------------------------------------------------------------------------- /2.Boto3 Documentation help to implement Python boto3 Scripts/1.Document-help-on-client-object.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | #iam,ec2 and s3 4 | iam_con_cli=aws_mag_con.client(service_name="iam",region_name="us-east-1") 5 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name="us-east-1") 6 | s3_con_cli=aws_mag_con.client(service_name="s3",region_name="us-east-1") 7 | ''' 8 | #List all iam users using client object 9 | response=iam_con_cli.list_users() 10 | for each_item in response['Users']: 11 | print(each_item['UserName']) 12 | ''' 13 | 14 | ''' 15 | #List all ec2 instaces ids 16 | response=ec2_con_cli.describe_instances() 17 | for each_item in response['Reservations']: 18 | for each_instance in each_item['Instances']: 19 | print(each_instance['InstanceId']) 20 | ''' 21 | #List all s3 buckets 22 | response=s3_con_cli.list_buckets() 23 | for each_item in response['Buckets']: 24 | print(each_item['Name']) 25 | #print(each_item.get('Name')) -------------------------------------------------------------------------------- /2.Boto3 Documentation help to implement Python boto3 Scripts/2.Document-help-on-resource-object.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | aws_mag_con=boto3.session.Session(profile_name="root") 4 | iam_con_re=aws_mag_con.resource(service_name="iam",region_name="us-east-1") 5 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 6 | s3_con_re=aws_mag_con.resource(service_name="s3",region_name="us-east-1") 7 | 8 | ''' 9 | #List all iam users 10 | for each_item in iam_con_re.users.all(): 11 | print(each_item.user_name) 12 | ''' 13 | 14 | for each_item in s3_con_re.buckets.limit(10): 15 | print(each_item.name) -------------------------------------------------------------------------------- /3.Working with STS/1.Document-get-aws-account-id.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | aws_mag_con_root=boto3.session.Session(profile_name="root") 4 | sts_con_cli=aws_mag_con_root.client(service_name="sts",region_name="us-east-1") 5 | response=sts_con_cli.get_caller_identity() 6 | print(response.get('Account')) 7 | 8 | aws_mag_con_ec2_dev=boto3.session.Session(profile_name="ec2_developer") 9 | sts_con_cli=aws_mag_con_ec2_dev.client(service_name="sts",region_name="us-east-1") 10 | response=sts_con_cli.get_caller_identity() 11 | print(response['Account']) -------------------------------------------------------------------------------- /4.EC2-PART-1 Working with EC2 Services/1.1.Document-listing-some-of-the-ec2-services-using-client.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | from pprint import pprint 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name="us-east-1") 5 | ''' 6 | response=ec2_con_cli.describe_instances()['Reservations'] 7 | for each_item in response: 8 | for each in each_item['Instances']: 9 | print("=============================") 10 | print("The Image Id is: {}\nThe Instance Id Is: {}\nThe Instance Launch Time is: {}".format(each['ImageId'],each['InstanceId'],each['LaunchTime'].strftime("%Y-%m-%d"))) 11 | ''' 12 | response=ec2_con_cli.describe_volumes()['Volumes'] 13 | for each_item in response: 14 | print("=======================") 15 | print("The volume id is: {}\nThe AvailabilityZone is: {}\nThe VolumeType is: {}".format(each_item['VolumeId'],each_item['AvailabilityZone'],each_item['VolumeType'])) 16 | -------------------------------------------------------------------------------- /4.EC2-PART-1 Working with EC2 Services/1.2.Document-listing-some-of-the-ec2-services-using-resource-object.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | from pprint import pprint 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 5 | ''' 6 | for each_instance in ec2_con_re.instances.all(): 7 | #print(dir(each_instance)) 8 | print("The Image Id is: {}\nThe Instance Id Is: {}\nThe Instance Launch Time is: {}".format(each_instance.image_id,each_instance.instance_id,each_instance.launch_time.strftime("%Y-%m-%d"))) 9 | print("-------------------") 10 | ''' 11 | for each_volume in ec2_con_re.volumes.all(): 12 | #print(dir(each_volume)) 13 | print("The volume id is: {}\nThe AvailabilityZone is: {}\nThe VolumeType is: {}".format(each_volume.volume_id,each_volume.availability_zone,each_volume.volume_type)) 14 | print("-----------------------------") 15 | 16 | -------------------------------------------------------------------------------- /4.EC2-PART-1 Working with EC2 Services/2.1.Document-menu-driven-script-on-ec2-actions-using-client.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 5 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name="us-east-1") 6 | 7 | while True: 8 | print("This script performs the following actions on ec2 instance") 9 | print(""" 10 | 1. start 11 | 2. stop 12 | 3. terminate 13 | 4. Exit""") 14 | opt=int(input("Enter your option: ")) 15 | if opt==1: 16 | instance_id=input('Enter your EC2 Instance Id: ') 17 | #print(dir(my_req_instance_object)) 18 | print("Starting ec2 instance.....") 19 | ec2_con_cli.start_instances(InstanceIds=[instance_id]) 20 | elif opt==2: 21 | instance_id=input('Enter your EC2 Instance Id: ') 22 | print("Stopping ec2 instance.....") 23 | ec2_con_cli.stop_instances(InstanceIds=[instance_id]) 24 | elif opt==3: 25 | instance_id=input('Enter your EC2 Instance Id: ') 26 | print("Terminating ec2 instance.....") 27 | ec2_con_cli.terminate_instances(InstanceIds=[instance_id]) 28 | elif opt==4: 29 | print("Thank you for using this script") 30 | sys.exit() 31 | else: 32 | print("Your option is invalid. Please try once again") -------------------------------------------------------------------------------- /4.EC2-PART-1 Working with EC2 Services/2.2.Document-menu-driven-script-on-ec2-actions-using-resource.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import sys 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 5 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name="us-east-1") 6 | 7 | while True: 8 | print("This script performs the following actions on ec2 instance") 9 | print(""" 10 | 1. start 11 | 2. stop 12 | 3. terminate 13 | 4. Exit""") 14 | opt=int(input("Enter your option: ")) 15 | if opt==1: 16 | instance_id=input('Enter your EC2 Instance Id: ') 17 | my_req_instance_object=ec2_con_re.Instance(instance_id) 18 | #print(dir(my_req_instance_object)) 19 | print("Starting ec2 instance.....") 20 | my_req_instance_object.start() 21 | elif opt==2: 22 | instance_id=input('Enter your EC2 Instance Id: ') 23 | my_req_instance_object=ec2_con_re.Instance(instance_id) 24 | print("Stopping ec2 instance.....") 25 | my_req_instance_object.stop() 26 | elif opt==3: 27 | instance_id=input('Enter your EC2 Instance Id: ') 28 | my_req_instance_object=ec2_con_re.Instance(instance_id) 29 | print("Terminating ec2 instance.....") 30 | my_req_instance_object.terminate() 31 | elif opt==4: 32 | print("Thank you for using this script") 33 | sys.exit() 34 | else: 35 | print("Your option is invalid. Please try once again") 36 | -------------------------------------------------------------------------------- /5.EC2 Waiters/1.Document-introduction-to-waiter.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import time 3 | aws_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_re=aws_con.resource(service_name="ec2",region_name="us-east-1") 5 | ec2_con_cli=aws_con.client(service_name="ec2",region_name="us-east-1") 6 | ''' 7 | my_inst_ob=ec2_con_re.Instance("i-002d4110f1199166f") 8 | print("Starting given instance....") 9 | my_inst_ob.start() 10 | my_inst_ob.wait_until_running() #Resource waiter waits for 200sec(40 checks after every 5 sec) 11 | print("Now your instance is up and running") 12 | ''' 13 | ''' 14 | print("Starting ec2 instace...") 15 | ec2_con_cli.start_instances(InstanceIds=['i-002d4110f1199166f']) 16 | waiter=ec2_con_cli.get_waiter('instance_running') 17 | waiter.wait(InstanceIds=['i-002d4110f1199166f']) #40 checks after every 15 sec 18 | print("Now your ec2 instace is up and running") 19 | ''' 20 | my_inst_ob=ec2_con_re.Instance("i-002d4110f1199166f") 21 | print("Starting given instance....") 22 | my_inst_ob.start() 23 | waiter=ec2_con_cli.get_waiter('instance_running') 24 | waiter.wait(InstanceIds=['i-002d4110f1199166f']) 25 | print("Now your ec2 instace is up and running") 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | ''' 38 | while True: 39 | my_inst_ob=ec2_con_re.Instance("i-002d4110f1199166f") 40 | print("The current status of ec2 is: ",my_inst_ob.state['Name']) 41 | if my_inst_ob.state['Name']=="running": 42 | break 43 | print("Wating to get running status....") 44 | time.sleep(5) 45 | ''' 46 | -------------------------------------------------------------------------------- /6.Usage of Meta object/1.Document-list-all-regions-for-ec2.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | ec2_con_re=aws_mag_con.resource(service_name="ec2") 4 | 5 | for each_item in ec2_con_re.meta.client.describe_regions()['Regions']: 6 | print(each_item['RegionName']) 7 | -------------------------------------------------------------------------------- /7.EC2 Collections/1.Document-introduction-to-collections.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 4 | f1={"Name": "instance-state-name", "Values":['running','stopped']} 5 | f2={"Name":"instance-type","Values":['t2.micro']} 6 | for each in ec2_con_re.instances.filter(Filters=[f1,f2]): 7 | print(each) -------------------------------------------------------------------------------- /7.EC2 Collections/2.Document-start-stop-terminate-all-instances-at-once.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 4 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name="us-east-1") 5 | ''' 6 | all_instances_ids=[] 7 | for each_in in ec2_con_re.instances.all(): 8 | all_instances_ids.append(each_in.id) 9 | #print(dir(ec2_con_re.instances)) 10 | waiter=ec2_con_cli.get_waiter('instance_running') 11 | print("Starting all instances ......") 12 | ec2_con_re.instances.start() 13 | waiter.wait(InstanceIds=all_instances_ids) 14 | print("your all instaces are up and running") 15 | ''' 16 | ''' 17 | np_sers_ids=[] 18 | f1={"Name": "tag:Name", "Values":['Non_Prod']} 19 | for each_in in ec2_con_re.instances.filter(Filters=[f1]): 20 | np_sers_ids.append(each_in.id) 21 | 22 | print(np_sers_ids) 23 | 24 | print("----------------------------") 25 | ''' 26 | 27 | np_sers_ids=[] 28 | f1={"Name": "tag:Name", "Values":['Non_Prod']} 29 | for each_item in ec2_con_cli.describe_instances(Filters=[f1])['Reservations']: 30 | for each_in in each_item['Instances']: 31 | np_sers_ids.append(each_in['InstanceId']) 32 | print(np_sers_ids) 33 | 34 | print("Starting intances with ids of : ",np_sers_ids) 35 | ec2_con_cli.start_instances(InstanceIds=np_sers_ids) 36 | waiter=ec2_con_cli.get_waiter('instance_running') 37 | waiter.wait(InstanceIds=np_sers_ids) 38 | print("Your np instances are up and running....") -------------------------------------------------------------------------------- /8.EC2-PART-2 Practice with EC2 Services/1.Document-delete-unused-untagged-volumes-using-resource.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ''' 5 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name='us-east-1') 6 | f_ebs_unused={"Name":"status","Values":["available"]} 7 | for each_volume in ec2_con_re.volumes.filter(Filters=[f_ebs_unused]): 8 | if not each_volume.tags: 9 | print(each_volume.id, each_volume.state,each_volume.tags) 10 | print("Deleting unused and untagged volumes.....") 11 | each_volume.delete() 12 | 13 | print("Delted all unused unatageed volumes.") 14 | ''' 15 | ec2_con_cli=aws_mag_con.client(service_name="ec2",region_name='us-east-1') 16 | for each_item in ec2_con_cli.describe_volumes()['Volumes']: 17 | if not "Tags" in each_item and each_item['State']=='available': 18 | print('Deleting ',each_item['VolumeId']) 19 | ec2_con_cli.delete_volume(VolumeId=each_item['VolumeId']) 20 | print("Delete all unused and untagged volumes.") -------------------------------------------------------------------------------- /8.EC2-PART-2 Practice with EC2 Services/2.Document-EC2-Instance-Discovery-Script-using-resource.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import csv 3 | aws_mag_con=boto3.session.Session(profile_name="ec2_developer") 4 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-east-1") 5 | cnt=1 6 | csv_ob=open("inventory_info.csv","w",newline='') 7 | csv_w=csv.writer(csv_ob) 8 | csv_w.writerow(["S_NO","Instance_Id",'Instance_Type','Architecture','LaunchTime','Privat_Ip']) 9 | for each in ec2_con_re.instances.all(): 10 | print(cnt,each,each.instance_id,each.instance_type,each.architecture,each.launch_time.strftime("%Y-%m-%d"),each.private_ip_address) 11 | csv_w.writerow([cnt,each.instance_id,each.instance_type,each.architecture,each.launch_time.strftime("%Y-%m-%d"),each.private_ip_address]) 12 | 13 | cnt+=1 14 | csv_ob.close() -------------------------------------------------------------------------------- /8.EC2-PART-2 Practice with EC2 Services/3.Document-list-all-snaps-based-on-size.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-west-1") 4 | 5 | sts_con_cli=aws_mag_con.client(service_name="sts",region_name="us-east-1") 6 | response=sts_con_cli.get_caller_identity() 7 | my_own_id=response.get('Account') 8 | f_size={"Name":"volume-size","Values":['10']} 9 | for each_snap in ec2_con_re.snapshots.filter(OwnerIds=[my_own_id],Filters=[f_size]): 10 | print(each_snap) -------------------------------------------------------------------------------- /8.EC2-PART-2 Practice with EC2 Services/4.Document-list-all-snaps-based-on-size-1.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | aws_mag_con=boto3.session.Session(profile_name="root") 3 | ec2_con_re=aws_mag_con.resource(service_name="ec2",region_name="us-west-1") 4 | 5 | sts_con_cli=aws_mag_con.client(service_name="sts",region_name="us-east-1") 6 | response=sts_con_cli.get_caller_identity() 7 | my_own_id=response.get('Account') 8 | f_size={"Name":"volume-size","Values":['10']} 9 | for each_snap in ec2_con_re.snapshots.filter(OwnerIds=[my_own_id],Filters=[f_size]): 10 | print(each_snap) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Learn-AWS-Automation-with-boto3-Python-and-Lambda-Functions 5 | Learn AWS Automation with boto3, Python, and Lambda Functions, by Packt Publishing 6 | --------------------------------------------------------------------------------