Created
December 28, 2022 09:39
-
-
Save Burekasim/2dfd8fc278474c4d39e4dd993a30851e to your computer and use it in GitHub Desktop.
AWS Assume role python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# assume role to the new account | |
import boto3 | |
sts_client = boto3.client('sts') | |
assume_account_id = '0123456789012' | |
assume_role_name = 'role_name' | |
role_session_name = 'check_cost_explorer_script' | |
assumed_role_object = sts_client.assume_role( | |
RoleArn=f'arn:aws:iam::{assume_account_id}:role/{assume_role_name}', | |
RoleSessionName=role_session_name | |
) | |
# temp credentials for organization account | |
credentials = assumed_role_object['Credentials'] | |
ce_client = boto3.client('ce', aws_access_key_id=credentials['AccessKeyId'], | |
aws_secret_access_key=credentials['SecretAccessKey'], | |
aws_session_token=credentials['SessionToken']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment