Skip to content

Instantly share code, notes, and snippets.

@gmariette
Created March 27, 2021 13:40
Show Gist options
  • Select an option

  • Save gmariette/8507e55676efc0ad6f9425aa56c05102 to your computer and use it in GitHub Desktop.

Select an option

Save gmariette/8507e55676efc0ad6f9425aa56c05102 to your computer and use it in GitHub Desktop.
def assumeRole(self, action):
client = self.initStsClient()
assumedRoleObject = client.assume_role(
RoleArn="arn:aws:iam::"+self.aws_accounts[self.env]["id"]+":role/"+self.aws_accounts[self.env]["role"],
RoleSessionName="Drp-" + action
)
credentials=assumedRoleObject['Credentials']
ACCESS_KEY=credentials.get("AccessKeyId")
SECRET_KEY=credentials.get("SecretAccessKey")
SESSION_TOKEN=credentials.get("SessionToken")
return ACCESS_KEY, SECRET_KEY, SESSION_TOKEN
def initeASGClient(self, action):
'''
DOCSTRING: Return the autoscaling client
INPUT: None
OUTPUT: boto3.client('autoscaling')
'''
ACCESS_KEY, SECRET_KEY, SESSION_TOKEN = self.assumeRole(action)
self.logger.debug('Launching asg client with role %s on the account %s', self.aws_accounts[self.env]["role"], self.aws_accounts[self.env]["id"])
return boto3.Session(
region_name=self.aws_accounts[self.env]["region"],
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
aws_session_token=SESSION_TOKEN
).client('autoscaling')
def describeASG(self):
'''
DOCSTRING: List available ASGs
INPUT: None
OUTPUT: ASGs available
'''
# Client init
client = self.initeASGClient("DescribeASG")
return client.describe_auto_scaling_groups()['AutoScalingGroups']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment