简体   繁体   中英

how to filter dictionary data in python for aws boto3 ec2 dedicated host

I am trying to write a lambda function to deallocate dedicated hosts

I need help filtering the dict data

Here is the code

import boto3 
client = boto3.client('ec2')
response = client.describe_hosts(Filters=[{'Name': 'state', 'Values': 
['available']}])
print(response)

The above returns dict data (2 dedicated hosts) .. So far all example i saw online about python

What is this HOSTS>>> Its very similar to RESERVATION for EC2 instance

 {
  u'Hosts': [
    {
      u'HostId': 'h-0e9--some-ID',
      u'Tags': [

      ],
      u'HostProperties': {
        u'Cores': 36,
        u'TotalVCpus': 72,
        u'InstanceType': 'c5.large',
        u'Sockets': 2
      },
      u'Instances': [

      ],
      u'State': 'available',
      u'AvailabilityZone': 'us-east-1a',
      u'AvailableCapacity': {
        u'AvailableInstanceCapacity': [
          {
            u'AvailableCapacity': 36,
            u'InstanceType': 'c5.large',
            u'TotalCapacity': 36
          }
        ],
        u'AvailableVCpus': 72
      },
      u'AllocationTime': datetime.datetime(2019,
      1,
      24,
      4,
      30,
      48,
      tzinfo=tzlocal()),
      u'AutoPlacement': 'off'
    },
    {
      u'HostId': 'h-0e9--some-ID',
      u'Tags': [

      ],
      u'HostProperties': {
        u'Cores': 36,
        u'TotalVCpus': 72,
        u'InstanceType': 'c5.large',
        u'Sockets': 2
      },
      u'Instances': [

      ],
      u'State': 'available',
      u'AvailabilityZone': 'us-east-1a',
      u'AvailableCapacity': {
        u'AvailableInstanceCapacity': [
          {
            u'AvailableCapacity': 36,
            u'InstanceType': 'c5.large',
            u'TotalCapacity': 36
          }
        ],
        u'AvailableVCpus': 72
      },
      u'AllocationTime': datetime.datetime(2019,
      1,
      24,
      4,
      30,
      48,
      tzinfo=tzlocal()),
      u'AutoPlacement': 'off'
    }
  ],

I would like to use the output to filter HostId and state "available" and release them using below api

response = client.release_hosts(
    HostIds=[
        'string',
    ]
)

In short I want to describe_host,get hostids and provide hostids to release_host using python

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.release_hosts

I tried to find sample code for describe_instance and start_instance or stop_instance or terminate_instance but couldnt make it

For EC2 instance , there is something reservations and for dedicated hosts i noticed Hosts.. I dont really understand this .. please let me know

Typical a dictionary would be like my_dict = {'name':'Jack', 'age': 26}

For s3 > Its very similar to above dictionary sample ...for ec2 its confusing .

s3client = boto3.client('s3')
list_buckets_resp = s3client.list_buckets()
   for bucket in list_buckets_resp['Buckets']:
       print bucket

Your code returns a list of all the hosts that satisfy the filters you have passed along with metadata information about each of those hosts. If all you need is the hostID info from each of these hosts, you could using something like below

HostIds=[]
for host in response['Hosts']:
    HostIds.append(host['hostID'])

HostIds_string = ','.join(HostIds)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM