Reputation: 623
I am writing a python script to get the LoadBalancerName
value from the ELBv2 endpoint using Boto3.
Because the returned ?dict? has multiple levels and I do not know the index of the items, I can't specify that. My current code is below:-
import boto3
import pprint
import json
pp = pprint.PrettyPrinter(indent=4)
elbv2 = boto3.client('elbv2', region_name='eu-west-2')
response = elbv2.describe_load_balancers()
pp.pprint(response['LoadBalancers'][0]['LoadBalancerName'])
for k in response:
print("Key: ", k)
print( "Value: ", dict[k])
I know that the value is currently printing the key as I am not sure how to get the key as it is nested. Ideally I want to be able to print the value for the subkey LoadBalancerName
Also, I am using Python 2.7 on this as that is what is installed on the server that will run this.
Upvotes: 1
Views: 2411
Reputation: 13541
There is an example of the response body for describe_load_balancers()
, see THIS.
Response Syntax
{
'LoadBalancerDescriptions': [
{
'LoadBalancerName': 'string',
'DNSName': 'string',
'CanonicalHostedZoneName': 'string',
'CanonicalHostedZoneNameID': 'string',
'ListenerDescriptions': [
{
'Listener': {
'Protocol': 'string',
'LoadBalancerPort': 123,
'InstanceProtocol': 'string',
'InstancePort': 123,
'SSLCertificateId': 'string'
},
'PolicyNames': [
'string',
]
},
],
'Policies': {
'AppCookieStickinessPolicies': [
{
'PolicyName': 'string',
'CookieName': 'string'
},
],
'LBCookieStickinessPolicies': [
{
'PolicyName': 'string',
'CookieExpirationPeriod': 123
},
],
'OtherPolicies': [
'string',
]
},
'BackendServerDescriptions': [
{
'InstancePort': 123,
'PolicyNames': [
'string',
]
},
],
'AvailabilityZones': [
'string',
],
'Subnets': [
'string',
],
'VPCId': 'string',
'Instances': [
{
'InstanceId': 'string'
},
],
'HealthCheck': {
'Target': 'string',
'Interval': 123,
'Timeout': 123,
'UnhealthyThreshold': 123,
'HealthyThreshold': 123
},
'SourceSecurityGroup': {
'OwnerAlias': 'string',
'GroupName': 'string'
},
'SecurityGroups': [
'string',
],
'CreatedTime': datetime(2015, 1, 1),
'Scheme': 'string'
},
],
'NextMarker': 'string'
}
If you want to get the Load Balancer names for all, then
response = client.describe_load_balancers()
for item in response['LoadBalancerDescriptions']:
print(item['LoadBalancerName'])
will give you the names.
Upvotes: 2