I have ~400 redis servers that need to have 60gb EBS vols attached. Some older nodes are going to be less than 60gb.
My approach was going to be iterate over each instance, find the volume, if volume is less than 60GB then return instance_id but it doesn't seem to work.
redis = []
def has_small_vols(instlist):
for i in instlist:
instance = ec2.Instance(str(i))
instid = i.instance_id
vols = instance.volumes.all()
for volume in vols:
if volume.size < 60:
redis.append(instid)
but something is wrong in the for volume in vols
loop and I'm unsure why.
I got that idea from here
I have tried boto3.resource('ec2') and I am unsure I need to use boto3.client('ec2') as well.
Creating the list of instances is not the problem:
import boto3
ec2 = boto3.resource('ec2')
def get_redis_nodes():
filters = [{'Name':'tag:Service', 'Values':['redis']}]
filt = [{'Name':'tag:Environment', 'Values':['production*']}]
instlist = list(ec2.instances.filter(Filters=filters).filter(Filters=filt).instance_id)
return instlist
What I expected is that the code goes through filtered instances, grab EBS volumes, finds EBS volumes that fit the IF and then append to a list.
But, if I try and print through the iterations I don't get the volume size:
for i in instlist:
instance = ec2.Instance(str(i))
vols = instance.volumes.all()
print(i.instance_id)
print(vols)
for volume in vols:
print(volume.size)
>>> get_redis_info()
i-a689ba6efa
ec2.Instance.volumesCollection(ec2.Instance(id="ec2.Instance(id='i-a689ba6efa')"), ec2.Volume)
i-f4b8212aev5748d
ec2.Instance.volumesCollection(ec2.Instance(id="ec2.Instance(id='i-f4b8212aev5748d')"), ec2.Volume)
i-0Ad235afh3a1d0f4
ec2.Instance.volumesCollection(ec2.Instance(id="ec2.Instance(id='i-0Ad235afh3a1d0f4')"), ec2.Volume)