Python Scypting
AWS EC2 Boto3 Sample Describe Image Dictionary Response
In = {'Images': [{'Architecture': 'x86_64', 'CreationDate': '2021-09-20T05:25:47.000Z', 'ImageId': 'ami-00ad7e16caedcdf9f', 'ImageLocation': '123456789012/Jenkins Master 2 Backup', 'ImageType': 'machine', 'Public': False, 'OwnerId': '123456789012', 'PlatformDetails': 'Linux/UNIX', 'UsageOperation': 'RunInstances', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/xvda', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-09129825799916e19', 'VolumeSize': 8, 'VolumeType': 'gp2', 'Encrypted': True}}], 'Description': 'Backup', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'Jenkins Master 2 Backup', 'RootDeviceName': '/dev/xvda', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0364af9c850d9deeb'}, {'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '123456789012/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '123456789012', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'}, {'Architecture': 'x86_64', 'CreationDate': '2022-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '123456789012/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '123456789012', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'}]}
Codes.. & Outputs..
Fetch Latest AMI with Name Statswith Specific AMIName from Dictionarynew = max(image['CreationDate'] for image in In['Images'] if image['Name'].startswith('AD-CCC-SAML')) print('new',new) filtered_images = [image for image in In['Images'] if image['CreationDate'] == new] print('filtered_images',filtered_images)Get only Names to ListOutput new 2023-08-21T09:21:31.000Z filtered_images [{'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '750344256621/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '750344256621', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'}]
ami_list =[] for Images in In['Images']: #print(Images['CreationDate']) ami_list.append(Images['Name']) print('ami_list',ami_list) print(len(ami_list))Get only CreationDate to List & Latest CreationDateOutput ami_list ['Jenkins Master 2 Backup', 'AD-CCC-SAML-21-08-20-02-51-pm', 'AD-CCC-SAML-21-08-20-02-51-pm'] 3
creation_dates = [image['CreationDate'] for image in In['Images']] latest_creation_dates = max(creation_dates) print('creation_dates',creation_dates) print('latest_creation_dates',latest_creation_dates)Get Latest Creation Image DictionaryOutput creation_dates ['2021-09-20T05:25:47.000Z', '2023-08-21T09:21:31.000Z', '2022-08-21T09:21:31.000Z'] latest_creation_dates 2023-08-21T09:21:31.000Z
new = max(image['CreationDate'] for image in In['Images'] if image['Name'].startswith('AD-CCC-SAML')) print('new',new) # Step 1 TO Get Latest Creation Image Dictionary filtered_images = [image for image in In['Images'] if image['CreationDate'] == new] print('filtered_images',filtered_images) # Step 2 TO Get Latest Creation Image Dictionary for Images in In['Images']: if Images['CreationDate'] == new: print(Images) else : print('no')Check Latest Date StringStep 1 Output filtered_images [{'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '750344256621/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '750344256621', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'}] Step 2 Output no {'Architecture': 'x86_64', 'CreationDate': '2023-08-21T09:21:31.000Z', 'ImageId': 'ami-0d4ba7e978aee7f42', 'ImageLocation': '750344256621/AD-CCC-SAML-21-08-20-02-51-pm', 'ImageType': 'machine', 'Public': False, 'OwnerId': '750344256621', 'Platform': 'windows', 'PlatformDetails': 'Windows', 'UsageOperation': 'RunInstances:0002', 'State': 'available', 'BlockDeviceMappings': [{'DeviceName': '/dev/sda1', 'Ebs': {'DeleteOnTermination': True, 'SnapshotId': 'snap-0857501855cca3b71', 'VolumeSize': 40, 'VolumeType': 'gp2', 'Encrypted': False}}], 'Description': 'AD-CCC-SAML-21-08-20-02-51-pm', 'EnaSupport': True, 'Hypervisor': 'xen', 'Name': 'AD-CCC-SAML-21-08-20-02-51-pm', 'RootDeviceName': '/dev/sda1', 'RootDeviceType': 'ebs', 'SriovNetSupport': 'simple', 'VirtualizationType': 'hvm', 'SourceInstanceId': 'i-0a0ef1bb1e35cfab8'} no
a = '2022-08-20T05:21:47.000Z' b = '2022-08-22T05:21:47.000Z' if a > b: print('a greater') elif a < b: print('b greater') else: print('a = b')Get 'b'List non matching numbers from 'a'ListOutput b greater
a = [2,3,4,5,6,7,8,9,0] b = [0,12,4,6,242,7,9] for x in (x for x in b if x not in a): print(x)Compare Both Lists and Get matching valuesOutput 12 242
a = [2,3,4,5,6,7,8,9,0] xyz = [0,12,4,6,242,7,9] for x in filter(lambda w: w in a, xyz): print (x)Output 0 4 6 7 9