Created
April 17, 2024 06:26
-
-
Save made2591/8d5b556fb08a57c4fd7df41863a14dd1 to your computer and use it in GitHub Desktop.
Create a profile config for each of the accounts inside an org
This file contains hidden or 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
import boto3 | |
import csv | |
def list_accounts_in_organization(): | |
# Initialize the Organizations client | |
org_client = boto3.client('organizations') | |
accounts = [] | |
# Pagination loop | |
next_token = None | |
while True: | |
# Get a list of accounts with pagination | |
if next_token: | |
response = org_client.list_accounts(NextToken=next_token) | |
else: | |
response = org_client.list_accounts() | |
# Extract accounts from the response | |
for account in response['Accounts']: | |
account_id = account['Id'] | |
account_name = account['Name'] | |
accounts.append({ | |
'AccountID': account_id, | |
'Name': account_name | |
}) | |
# Check if there are more accounts to retrieve | |
if 'NextToken' in response: | |
next_token = response['NextToken'] | |
else: | |
break | |
return accounts | |
def generate_sso_config(account_id, profile_name): | |
sso_start_url = "<YOUR_SSO_START_URL>" | |
sso_region = "<YOUR_SSO_REGION>" | |
sso_role_name = "<YOUR_SSO_ROLE_NAME>" | |
region = "<YOUR_DEFAULT_REGION>" | |
config_content = f"""[profile {profile_name}] | |
sso_start_url={sso_start_url} | |
sso_region={sso_region} | |
sso_account_id={account_id} | |
sso_role_name={sso_role_name} | |
region={region} | |
""" | |
return config_content | |
def write_sso_config_file(accounts): | |
with open(f'all_sso_config', 'w') as file: | |
for account in accounts: | |
account_id = account['AccountID'] | |
profile_name = account['Name'] | |
config_content = generate_sso_config(account_id, profile_name) | |
file.write(config_content) | |
def main(): | |
# Call function to list accounts in the organization | |
accounts = list_accounts_in_organization() | |
# Write accounts data to CSV | |
write_sso_config_file(accounts) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment