Skip to content

Instantly share code, notes, and snippets.

@falco694
Created September 17, 2019 02:18
Show Gist options
  • Save falco694/fdc80499e1aaa4abb28b516989cef79f to your computer and use it in GitHub Desktop.
Save falco694/fdc80499e1aaa4abb28b516989cef79f to your computer and use it in GitHub Desktop.
AMIが存在しないEBSスナップショットを出力
# %%
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