Reputation: 1872
Here I want to filter snapshots by the tags in python3 as below :
res = c.describe_snapshots(OwnerIds=['012345678900'],Filters=[{'Name': 'tag:Name', 'Value': ['nonprod*']}])
for s in res:
If 'nonprod' in s.tags :
if s.tags == 'nonprod':
s.delete()
print ("snapshotlist1: %s" % s.id)
elif 'prod' in c.tags
if s.tags == 'prod':
print ("snapshotlist2: %s" % s.id)
getting error in python3 is "attributeerror 'str' object has no attribute 'tags'"
Upvotes: 1
Views: 1535
Reputation: 1872
Here's a version is also works :
Added Value()
method which compare values in dictionary :
for s in res['Snapshots']:
for tag in s['Tags']:
if 'nonprod' in tag.value():
s.delete()
print("snapshotlist1[Deleted]: %s" % s['SnapshotId'])
elif 'prod': in tag.values():
print("snapshotlist2: %s" % s['SnapshotId'])
Upvotes: 0
Reputation: 238975
describe_snapshots returns output in the form of:
{
'Snapshots': [
{
# others not shown
'Tags': [
{
'Key': 'string',
'Value': 'string'
},
]
},
],
'NextToken': 'string'
}
Thus you should have in the beginning of the loop:
for s in res['Snapshots']:
Also you have to iterate over all tags, as Tags
is a list:
for s in res['Snapshots']:
for tag in s['Tags']:
if tag['Key'] == 'nonprod':
print("snapshotlist1: %s" % s['SnapshotId'])
elif tag['Key'] == 'prod':
print("snapshotlist2: %s" % s['SnapshotId'])
Upvotes: 1