Reputation: 431
I have a bit of an issue, I have this code that I created months ago and I am trying to modify it to get more info, this is where I am stuck.
I am calling describe_instances and iterating through getting the info I need. But I need to get the encryption details also of each ec2 instance volume . I believe that is under "describe_volumes"
How would I add this so my prints seem seemless, is it possible?
response = client.describe_instances(Filters=[{'Name':'tag-key','Values':['Name']}])
ec2tags = client.describe_tags()
# pprint(response)
for item in response['Reservations']:
#pprint(item['Instances'])
pprint("AWS Account ID: {}".format(item['OwnerId']))
for instance_id in item['Instances']:
#print(instance_id)
Tags = instance_id['Tags']
tag_name_value = ""
for tag in Tags:
if tag['Key'] == "Name":
tag_name_value = tag["Value"]
break
#Tags = instance_id['Tags']['Value']
State = instance_id['State']['Name']
#print("EC2 Name: {}".format(Tags))
print("EC2 Name: {}".format(tag_name_value))
print("Instance Id is: {}\nInstance Type is: {}".format(instance_id['InstanceId'],instance_id['InstanceType']))
print("EC2 State is: {}".format(State))
if 'VpcId' in instance_id:
print("VPC Id is: {}".format(instance_id['VpcId']))
for volumes in instance_id['BlockDeviceMappings']:
vol_list = [ vol['Ebs']['VolumeId'] for vol in instance_id['BlockDeviceMappings']]
when I run it, I get this:
'AWS Account ID: 123456789012'
EC2 Name: ec2_web
Instance Id is: i-0d3c64d8771ru57574
Instance Type is: t2.small
EC2 State is: stopped
VPC Id is: vpc-026efa5966396
I want it to look like this
'AWS Account ID: 123456789012'
EC2 Name: ec2_web
Instance Id is: i-0d3c64d8771ru57574
Instance Type is: t2.small
EC2 State is: stopped
VPC Id is: vpc-026efa5966396
Volume Id: ['vol-054f5ef5eeb2025b0']
Volume Encrypt: true or false
Upvotes: 1
Views: 826
Reputation: 238209
You can use describe_volumes.
response = client.describe_instances(Filters=[{'Name':'tag-key','Values':['Name']}])
ec2tags = client.describe_tags()
# pprint(response)
for item in response['Reservations']:
#pprint(item['Instances'])
pprint("AWS Account ID: {}".format(item['OwnerId']))
for instance_id in item['Instances']:
#print(instance_id)
Tags = instance_id['Tags']
tag_name_value = ""
for tag in Tags:
if tag['Key'] == "Name":
tag_name_value = tag["Value"]
break
#Tags = instance_id['Tags']['Value']
State = instance_id['State']['Name']
#print("EC2 Name: {}".format(Tags))
print("EC2 Name: {}".format(tag_name_value))
print("Instance Id is: {}\nInstance Type is: {}".format(instance_id['InstanceId'],instance_id['InstanceType']))
print("EC2 State is: {}".format(State))
if 'VpcId' in instance_id:
print("VPC Id is: {}".format(instance_id['VpcId']))
for volumes in instance_id['BlockDeviceMappings']:
vol_list = [vol['Ebs']['VolumeId'] for vol in instance_id['BlockDeviceMappings']]
volume_infos = client.describe_volumes(VolumeIds=vol_list)
for vol in volume_infos['Volumes']:
print(f"Volume Id: {vol['VolumeId']}")
print(f"Volume Encrypt: {vol['Encrypted']}")
Note that there are some indentation issues in your code. Thus you have to fix them as well.
Upvotes: 3