Reputation: 3
I have below code
import boto3
client = session.client('ec2')
response = client.describe_iam_instance_profile_associations()
print(response) {'IamInstanceProfileAssociations': [{'AssociationId': 'iip-assoc-0c7941c0858c84652', 'InstanceId': 'i-xxx', 'IamInstanceProfile': {'Arn': 'arn:aws:iam::xxxx:instance-profile/xxx', 'Id': 'xxx'}, 'State': 'associated'}], 'ResponseMetadata': {'RequestId': '15bdc08e-ff66-431e-968e-1930557847ef', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '15bdc08e-ff66-431e-968e-1930557847ef', 'cache-control': 'no-cache, no-store', 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'vary': 'accept-encoding', 'content-type': 'text/xml;charset=UTF-8', 'transfer-encoding': 'chunked', 'date': 'Wed, 27 Jul 2022 22:04:12 GMT', 'server': 'AmazonEC2'}, 'RetryAttempts': 0}}
I would like to get info from output
so i tried
for key in response:
... print (key)
I get response
Also for
> for r in response['IamInstanceProfileAssociations']: print
> (r['InstanceId'] , r['AssociationId'])
With Above also i get expected response.
Now i need to get InstanceId & ARN of the instance profile, so i tried below but i got an errro 'TypeError: string indices must be integers'
Any suggestions pls ? i checked nested dictionary pages from google & other but not able to find any solution.
for r in response['IamInstanceProfileAssociations']:
... #print (r['InstanceId'] , r['AssociationId'])
... for i in r['IamInstanceProfile']:
... print (i['Arn'],r['InstanceId'])
Upvotes: 0
Views: 342
Reputation: 58
The variable i
is a string than you cannot access the position Arn
Here is a example showing how you can get the instanceId and Profile's Arn:
import boto3
client = boto3.client('ec2')
response = client.describe_iam_instance_profile_associations()
for association in response['IamInstanceProfileAssociations']:
print((association['InstanceId'], association['IamInstanceProfile']['Arn']))
Using you example the output must be the following:
('i-xxx', 'arn:aws:iam::xxxx:instance-profile/xxx')
Upvotes: 1