Posts: 5
Threads: 1
Joined: Jun 2023
Jun-09-2023, 05:07 PM
(This post was last modified: Jun-09-2023, 05:07 PM by kpatil.)
Hello,
Can someone help me how to fix the error i am getting here . Thanks
error is
Traceback (most recent call last):
File "/root/temp0/server", line 10, in <module>
snapshot_name = snapshot['Tags'][0]['Value']
TypeError: string indices must be integers
import boto3
AWS_REGION = "us-west-2"
AWS_PROFILE = "kiran"
session=boto3.session.Session(profile_name=AWS_PROFILE)
client = session.client('ec2',region_name=AWS_REGION)
snapshots = client.describe_snapshots(SnapshotIds=['snap-xxxxxx'])
# Iterate over the snapshots
for snapshot in snapshots:
# Get the snapshot name
snapshot_name = snapshot['Tags'][0]['Value']
# Append the snapshot name to the description
description = snapshot['Description'] + ' - ' + snapshot_name
# Update the description
client.modify_snapshot_attribute(
SnapshotId=snapshot['SnapshotId'],
Attribute='description',
OperationType='replace',
Description=description
)
Posts: 6,783
Threads: 20
Joined: Feb 2020
The error says that either screenshot or snapshot_name = snapshot["Tags"][0] is a string. The boto3 documentation says Client.describe_snapshots returns a dictionary, so that means snapshot["Tags"][0] is a string.
Posts: 1
Threads: 0
Joined: Jun 2023
Can you provide value of snapshots variable
Posts: 5
Threads: 1
Joined: Jun 2023
(Jun-09-2023, 05:23 PM)hardik07 Wrote: Can you provide value of snapshots variable
here you go
>>> print (snapshots)
{'Snapshots': [{'Description': 'xxxx', 'Encrypted': False, 'OwnerId': 'xxx', 'Progress': '100%', 'SnapshotId': 'snap-xxx', 'StartTime': datetime.datetime(2021, 8, 25, 3, 53, 51, 759000, tzinfo=tzutc()), 'State': 'completed', 'VolumeId': 'vol-xxx', 'VolumeSize': 10, 'Tags': [{'Key': 'xxxx', 'Value': 'xxxx'}, {'Key': 'Description', 'Value': 'xxxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'xxxx', 'Value': 'xxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'Name', 'Value': 'xxx'}], 'StorageTier': 'standard'}],...
>>>
Posts: 5
Threads: 1
Joined: Jun 2023
(Jun-09-2023, 05:37 PM)kpatil Wrote: (Jun-09-2023, 05:23 PM)hardik07 Wrote: Can you provide value of snapshots variable
here you go
>>> print (snapshots)
{'Snapshots': [{'Description': 'xxxx', 'Encrypted': False, 'OwnerId': 'xxx', 'Progress': '100%', 'SnapshotId': 'snap-xxx', 'StartTime': datetime.datetime(2021, 8, 25, 3, 53, 51, 759000, tzinfo=tzutc()), 'State': 'completed', 'VolumeId': 'vol-xxx', 'VolumeSize': 10, 'Tags': [{'Key': 'xxxx', 'Value': 'xxxx'}, {'Key': 'Description', 'Value': 'xxxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'xxxx', 'Value': 'xxx'}, {'Key': 'xxx', 'Value': 'xxx'}, {'Key': 'Name', 'Value': 'xxx'}], 'StorageTier': 'standard'}],...
>>>
I think it takes
['Tags'][0] = as the Tag called Name ..
Posts: 6,783
Threads: 20
Joined: Feb 2020
In your print, snapshot['Tags'][0] is a dictionary, but is it always a dictionary?
Posts: 5
Threads: 1
Joined: Jun 2023
it is always dictionary , i found not all snapshots have Name tag. Also i found index 0 is not always Name tag :(
Posts: 5
Threads: 1
Joined: Jun 2023
Jun-09-2023, 06:56 PM
(This post was last modified: Jun-09-2023, 06:56 PM by kpatil.)
below code print names only for those who have Name tag
import boto3
AWS_REGION = "us-west-2"
AWS_PROFILE = "kiran"
session=boto3.session.Session(profile_name=AWS_PROFILE)
#
client = session.client('ec2',region_name=AWS_REGION)
# Get all snapshots
snapshots = client.describe_snapshots(OwnerIds=['self'],Filters=[{'Name': 'tag:Name', 'Values': ['*']}])
# To print only the Name of the snapshots
for snapshot in snapshots['Snapshots']:
for tag in snapshot['Tags']:
if tag['Key'] == 'Name':
print (snapshot['SnapshotId'],tag['Value'])
break
|