AWS Volume tagging based on the block device
#!/usr/bin/env python2
import boto3
REGION = 'ap-southeast-1'
client = boto3.client('ec2',REGION)
#instanceName=''
response = client.describe_volumes()
#response['Volumes'][0]['Attachments'][0]['VolumeId']
for i in response['Volumes']:
for j in i['Attachments']:
print "Volume_Id:"+ j['VolumeId']
volume_id=j['VolumeId']
print "Instance_Id:"+j['InstanceId']
instance_id=j['InstanceId']
print "Device:"+j['Device']
device=j['Device']
response = client.describe_instances(
InstanceIds=[
instance_id,
],
)
#print response
for i in response['Reservations']:
temp= i['Instances']
#print temp
for j in temp:
#print j['InstanceId']
for m in j['Tags']:
key=m['Key']
if ( key == 'Name' ):
print"InstanceName:"+ m['Value']+ "\n"
#global instanceName
instanceName=m['Value']
response = client.create_tags(
DryRun=False,
Resources=[
volume_id,
],
Tags=[
{
'Key': 'Name',
'Value':instanceName + ":" + device
},
]
)
#!/usr/bin/env python2
import boto3
REGION = 'ap-southeast-1'
client = boto3.client('ec2',REGION)
#instanceName=''
response = client.describe_volumes()
#response['Volumes'][0]['Attachments'][0]['VolumeId']
for i in response['Volumes']:
for j in i['Attachments']:
print "Volume_Id:"+ j['VolumeId']
volume_id=j['VolumeId']
print "Instance_Id:"+j['InstanceId']
instance_id=j['InstanceId']
print "Device:"+j['Device']
device=j['Device']
response = client.describe_instances(
InstanceIds=[
instance_id,
],
)
#print response
for i in response['Reservations']:
temp= i['Instances']
#print temp
for j in temp:
#print j['InstanceId']
for m in j['Tags']:
key=m['Key']
if ( key == 'Name' ):
print"InstanceName:"+ m['Value']+ "\n"
#global instanceName
instanceName=m['Value']
response = client.create_tags(
DryRun=False,
Resources=[
volume_id,
],
Tags=[
{
'Key': 'Name',
'Value':instanceName + ":" + device
},
]
)