Python Forum
boto3 - Error - TypeError: string indices must be integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
boto3 - Error - TypeError: string indices must be integers
#1
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
    )
Reply
#2
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.
Reply
#3
Can you provide value of snapshots variable
Reply
#4
(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'}],...
>>>
Reply
#5
(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 ..
Reply
#6
In your print, snapshot['Tags'][0] is a dictionary, but is it always a dictionary?
Reply
#7
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]
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 522 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  tuple indices must be integers or slices, not str cybertooth 16 11,601 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,178 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,431 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,045 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,470 Dec-30-2022, 05:38 PM
Last Post: dee
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,952 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: string indices must be integers JonWayn 12 3,410 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  boto3 Help Needed gw1500se 0 767 Jul-30-2022, 04:06 PM
Last Post: gw1500se
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,888 Jul-01-2022, 01:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020