Created
September 17, 2019 02:18
-
-
Save falco694/fdc80499e1aaa4abb28b516989cef79f to your computer and use it in GitHub Desktop.
AMIが存在しないEBSスナップショットを出力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
import boto3 | |
client = boto3.client("ec2") | |
def main(): | |
response = client.describe_snapshots( | |
OwnerIds=[ | |
get_account_id(), | |
], | |
) | |
for item in response['Snapshots']: | |
snapshotId = item['SnapshotId'] | |
volumeSize = item['VolumeSize'] | |
if not exist_image(snapshotId): | |
print(snapshotId, volumeSize) | |
def exist_image(snapshotId): | |
response = client.describe_images( | |
Filters=[ | |
{ | |
'Name': 'block-device-mapping.snapshot-id', | |
'Values': [ | |
snapshotId, | |
] | |
}, | |
], | |
) | |
return len(response['Images']) > 0 if True else False | |
def get_account_id(): | |
return boto3.client('sts').get_caller_identity()['Account'] | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment