-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws_search_sr.py
66 lines (57 loc) · 2.18 KB
/
aws_search_sr.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
54
55
56
57
58
59
60
61
62
63
64
65
66
import boto3
def switching_role(arn, nome):
sts_client = boto3.client('sts')
credentials = ""
try:
assumedRoleObject = sts_client.assume_role(
RoleArn=arn,
RoleSessionName=nome
)
credentials = assumedRoleObject['Credentials']
except Exception as e:
#Tratamento de erro para caso nao seja possivel realizar o switch role
print ("Nao foi possivel realizar o switch role {}".format(e))
credentials = False
return credentials
def ec2_connection(credentials, region):
client = boto3.client(
'ec2',
aws_access_key_id = credentials['AccessKeyId'],
aws_secret_access_key = credentials['SecretAccessKey'],
aws_session_token = credentials['SessionToken'],
region_name = region
)
return client
def list_instances_sr(tagvalue, region, public, arn, name):
# When passed a tag key, tag value this will return a list of InstanceIds that were found.
try:
credentials = switching_role(arn, name)
except Exception as e:
print("%s"%e)
credentials = False
if credentials:
ec2client = ec2_connection(credentials, region)
response = ec2client.describe_instances(
Filters=[
{
'Name': "tag:Name",
'Values': [tagvalue,],
}
]
)
instancelist = []
for reservation in (response["Reservations"]):
for instance in reservation["Instances"]:
if (instance['State']['Name']+"\n") == "running\n":
if public and 'PublicIpAddress' in instance:
for tag in (instance['Tags']):
if tag['Key'] == 'Name':
instancelist.append({tag['Value']:instance['PublicIpAddress']})
else:
for tag in (instance['Tags']):
if tag['Key'] == 'Name':
instancelist.append({tag['Value']:instance['NetworkInterfaces'][0]['PrivateIpAddress']})
return instancelist
else:
instancelist = []
return instancelist