-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
44 lines (37 loc) · 1.62 KB
/
main.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
__author__ = "Alon Shrestha"
import boto3
from botocore.config import Config
import config
retryConfig = Config(
retries=dict(
max_attempts=10
)
)
sts_client = boto3.client('sts')
def getSession(assumeRole, service, sessionType, region='us-east-1'):
session = None
sts_response = sts_client.assume_role(
RoleArn=assumeRole,
RoleSessionName='aws-sts-boto3-cross-account-access',
)
if sessionType == "client":
session = boto3.client(service, aws_access_key_id=sts_response['Credentials']['AccessKeyId'],
aws_secret_access_key=sts_response['Credentials']['SecretAccessKey'],
aws_session_token=sts_response['Credentials']['SessionToken'],
region_name=region,
config=retryConfig
)
elif sessionType == "resource":
session = boto3.resource(service,
aws_access_key_id=sts_response['Credentials']['AccessKeyId'],
aws_secret_access_key=sts_response['Credentials']['SecretAccessKey'],
aws_session_token=sts_response['Credentials']['SessionToken'],
region_name=region,
config=retryConfig)
return session
if __name__ == "__main__":
for account in config.accountList:
iamRole = config.accountConfig[account]['iamRoleArn']
ec2Client = getSession(iamRole, "ec2", "client")
ec2Metadata = ec2Client.describe_instances()
print(ec2Metadata)