-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopytags.py
34 lines (28 loc) · 911 Bytes
/
copytags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
import boto3
import sys
ec2 = boto3.resource('ec2')
source = sys.argv[1]
target = sys.argv[2]
if 'i-' not in source:
source = 'i-' + source
if 'i-' not in target:
target = 'i-' + target
source_instance = ec2.Instance(source)
target_instance = ec2.Instance(target)
if source_instance.tags is not None:
try:
for tag in source_instance.tags:
if tag['Key'] != 'Name' and tag['Key'] != 'Customer Name':
print ('Copying Tag Value {} to Key {}.'.format(tag['Value'], tag['Key']))
target_instance.create_tags(
DryRun=False,
Tags=[
{
'Key': tag['Key'],
'Value': tag['Value']
}
]
)
except Exception, e:
print ('Error message: {}'.format(e))