Home > Software design >  KeyError: 'GroupName'
KeyError: 'GroupName'

Time:09-14

I'm creating an IAM group and trying to print the group name that gets created. When I try that, it's giving me this error

KeyError: 'GroupName'

Here's my function

def cf_admin_iam_group():
iam = boto3.client('iam')

try:
    response = iam.create_group(GroupName='Test')
    print(response['GroupName'])
except botocore.exceptions.ClientError as error:
    print(error)

When I try to just print(response)

I get the expected output

{'Group': {'Path': '/', 'GroupName': 'Test', 'GroupId': 'AGPAXVCO7KXYHZP24FQFZ', 'Arn':
 'arn:aws:iam::526299125232:group/Test', 'CreateDate': datetime.datetime(2022, 9, 13, 19, 17, 51, tzinfo=tzutc())}, 
'ResponseMetadata': {'RequestId': '7b2e7c6c-a811-497a-b2c5-177c70a0464c', 
'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '7b2e7c6c-a811-497a-b2c5-177c70a0464c', 
'content-type': 'text/xml', 'content-length': '490', 'date': 'Tue, 13 Sep 2022 19:17:51 GMT'}, 'RetryAttempts': 0}}

I'm not sure why running print(response['GroupName']) is giving me an error instead of printing the group name.

CodePudding user response:

response is a dictionary, and GroupName is a key of the Group value, so you need to use:

print(response['Group']['GroupName'])
  • Related