Python Forum

Full Version: boto3 - Error - TypeError: string indices must be integers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
    )
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.
Can you provide value of snapshots variable
(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'}],...
>>>
(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 ..
In your print, snapshot['Tags'][0] is a dictionary, but is it always a dictionary?
it is always dictionary , i found not all snapshots have Name tag. Also i found index 0 is not always Name tag :(

[Image: tagoncreation_screenshot.png]
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