Skip to content

Commit 0af69c6

Browse files
committed
Be better at finding account id
1 parent a9f76fd commit 0af69c6

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

bin/aws-profiles

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import configparser
33
import os
44
import sys
55

6-
# script.py - List AWS profile names with account IDs extracted from role_arn in credentials file
6+
# script.py - List AWS profile names with account IDs from aws_account_id or extracted from role_arn
77

88
# Get patterns from command-line arguments
99
patterns = sys.argv[1:]
@@ -31,12 +31,10 @@ def get_account_id_from_arn(arn):
3131
# Iterate through the sections and print those that match the patterns along with the account ID
3232
for section in config.sections():
3333
if not patterns or any(pattern in section for pattern in patterns):
34-
try:
35-
# Attempt to fetch the role_arn from the credentials file
34+
account_id = config.get(section, 'aws_account_id') if config.has_option(section, 'aws_account_id') else 'Unknown'
35+
# If aws_account_id is not found, try to extract it from role_arn
36+
if account_id == 'Unknown' and config.has_option(section, 'role_arn'):
3637
role_arn = config.get(section, 'role_arn')
3738
account_id = get_account_id_from_arn(role_arn)
38-
except (configparser.NoSectionError, configparser.NoOptionError):
39-
# If the role_arn is not found, set account_id to 'Unknown'
40-
account_id = 'Unknown'
41-
39+
4240
print(f"{section}\t{account_id}")

0 commit comments

Comments
 (0)