-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindec2instanceid.py
53 lines (51 loc) · 1.68 KB
/
findec2instanceid.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
try:
import boto3
except:
print "You must install boto3 module in order to run this script"
import sys
from boto3.session import Session
import argparse
parser = argparse.ArgumentParser(description="This script can be used to find \
EC2 instance id from AWS Name Tags")
parser.add_argument('-a', '--akey', dest='akey', help='AWS Access Key',
required=True)
parser.add_argument('-s', '--skey', dest='skey', help='AWS Secret Key',
required=True)
parser.add_argument('-r', '--region', dest='region', help='AWS Region',
required=True)
parser.add_argument('-n', '--name', dest='name', help='AWS Instance Name',
required=True)
if len(sys.argv) < 4:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
akey = args.akey
skey = args.skey
region = args.region
name = args.name
try:
session = Session(aws_access_key_id=str(akey),
aws_secret_access_key=str(skey))
except:
sys.exc_info()
def findawsinstanceid(servername):
try:
client = session.client('ec2', region)
response = client.describe_instances(
Filters=[
{
'Name': 'tag:Name',
'Values': [
servername,
]
}
]
)
for r in response.get('Reservations'):
for q in r['Instances']:
return q['InstanceId']
except Exception as ex:
print ex
instanceid = findawsinstanceid(name)
if instanceid is not None:
print "\n", instanceid