Reputation: 127
when I run my code I get the error
TypeError: string indices must be integers
This is my code
import boto3
import csv
ec2 = boto3.client('ec2')
vpcendpoints = ec2.describe_vpc_endpoints(DryRun=False)['VpcEndpoints']
results = list(map(lambda endpoint:(endpoint['VpcEndpointType'],endpoint['ServiceName'],endpoint['VpcEndpointId'],endpoint['VpcId'],endpoint['DnsEntries'['DnsName']]),vpcendpoints))
for (vpcendpointtype,servicename,vpcendpointid,vpcid,dnsname) in results:
print (vpcendpointtype+','+ servicename+ ','+ vpcendpointid+ ','+ vpcid+ ','+ dnsname)
I think the problem is the ['DnsEntries'['DnsName']] part, but how to resolve that?
Help very appreciated!!!
print out of whole vpcendpoints (cutted & blanked)
{'VpcEndpointId': 'XXXXXXXXXXX', 'VpcEndpointType': 'Interface', 'VpcId': 'vpc-XXXXXXXXXXXXX', 'ServiceName': 'com.amazonaws.eu-central-1.logs',
'State': 'available', 'PolicyDocument': '{\n "Statement": [\n {\n "Action": "*", \n "Effect": "Allow", \n "Principal": "*", \n "Resource": "*"\n }\n ]\n}', 'RouteTableIds': [],
'SubnetIds': ['subnet-XXXXXXXXXX', 'subnet-XXXXXXXX', 'subnet-XXXXXXXXX'], 'Groups': [{'GroupId': 'sg-XXXXXXXXXX', 'GroupName': 'XXXXXXXXXX'}], 'PrivateDnsEnabled': True, 'RequesterManaged': False, 'NetworkInterfaceIds': ['eni-XXXXXXXXXX', 'eni-XXXXXXXXX', 'eni-XXXXXXXXXXXX'],
'DnsEntries': [{'DnsName': 'vpce-XXXXXXXXXXXXX.logs.eu-central-1.vpce.amazonaws.com', 'HostedZoneId': 'XXXXXXXX'}, {'DnsName': 'vpce-XXXXXXXXXXX-XXXXXXX-eu-central-1a.logs.eu-central-1.vpce.amazonaws.com', 'HostedZoneId': 'XXXXXXXX'}, {'DnsName': 'vpce-XXXXXXXXX-XXXXXXX-eu-central-1b.logs.eu-central-1.vpce.amazonaws.com', 'HostedZoneId': 'XXXXXXXXX'}, {'DnsName': 'vpce-XXXXXXXXX-XXXXXX-eu-central-1c.logs.eu-central-1.vpce.amazonaws.com', 'HostedZoneId': 'XXXXXXXXXX'}, {'DnsName': 'logs.eu-central-1.amazonaws.com', 'HostedZoneId': 'XXXXXXXXX'}],
'CreationTimestamp': datetime.datetime(2020, 2, 27, 13, 46, 4, tzinfo=tzutc()), 'Tags': [{'Key': 'Name', 'Value': 'XXXXXXXX'}],
'OwnerId': 'XXXXXXX'},
Upvotes: 1
Views: 227
Reputation: 238111
You have mistake there.:
endpoint['DnsEntries'['DnsName']]
DnsEntries
is an array:
'DnsEntries': [
{
'DnsName': 'string',
'HostedZoneId': 'string'
},
]
Thus you have to call it, for example assuming an array of length 1:
endpoint['DnsEntries'][0]['DnsName']]
Simplified code which works:
for endpoint in vpcendpoints:
print('VPC', endpoint['VpcEndpointType'],
endpoint['ServiceName'],
endpoint['VpcEndpointId'],
endpoint['VpcId'])
for dns_entry in endpoint['DnsEntries']:
print(" - DnsName: " + dns_entry['DnsName'])
Example output:
VPC Gateway com.amazonaws.us-east-1.dynamodb vpce-032a826a vpc-aabb1122
VPC Interface com.amazonaws.us-east-1.elasticloadbalancing vpce-0f89a33420c1931d7 vpc-1a2b3c4d
- DnsName: vpce-0f89a33420c1931d7-bluzidnv.elasticloadbalancing.us-east-1.vpce.amazonaws.com
- DnsName: vpce-0f89a33420c1931d7-bluzidnv-us-east-1b.elasticloadbalancing.us-east-1.vpce.amazonaws.com
- DnsName: vpce-0f89a33420c1931d7-bluzidnv-us-east-1a.elasticloadbalancing.us-east-1.vpce.amazonaws.com
Upvotes: 2
Reputation: 696
Dns Entries is a list. Hence you need to iterate on the DNS entries to pick up the DnsName instead of the considering it as a dictionary.
'DnsEntries': [
{
'DnsName': 'string',
'HostedZoneId': 'string'
}
]
Upvotes: 1