-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add iam user tagging helper script (#11)
* add iam user tagging helper script * update readme and rename iam user tagging file
- Loading branch information
1 parent
15babf3
commit 447e4f5
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,8 +33,8 @@ override.tf.json | |
terraform.rc | ||
|
||
__pycache__ | ||
*.json | ||
*.sh | ||
*/*.json | ||
*/*.sh | ||
header.tf | ||
creator.zip | ||
destructor.zip |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"IAM_USERNAME_1": { | ||
"TAG_KEY_1": "TAG_VALUE_1", | ||
"TAG_KEY_2": "TAG_VALUE_2" | ||
}, | ||
"IAM_USERNAME_2": { | ||
"TAG_KEY": "TAG_VALUE" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
import boto3 | ||
import os | ||
import concurrent.futures | ||
|
||
from botocore.exceptions import ClientError | ||
|
||
# AWS Profile to use for API calls | ||
IKR_AWS_PROFILE = os.environ.get('IKR_AWS_PROFILE', None) | ||
|
||
# AWS Access Key to use for API calls | ||
IKR_AWS_ACCESS_KEY_ID = os.environ.get('IKR_AWS_ACCESS_KEY_ID', None) | ||
|
||
# AWS Secret Access Key to use for API calls | ||
IKR_AWS_SECRET_ACCESS_KEY = os.environ.get('IKR_AWS_SECRET_ACCESS_KEY', None) | ||
|
||
# AWS Session Token to use for API calls | ||
IKR_AWS_SESSION_TOKEN = os.environ.get('IKR_AWS_SESSION_TOKEN', None) | ||
|
||
# AWS region to use | ||
IKR_AWS_REGION = os.environ.get('IKR_AWS_REGION', 'us-east-1') | ||
|
||
session = boto3.Session(aws_access_key_id=IKR_AWS_ACCESS_KEY_ID, aws_secret_access_key=IKR_AWS_SECRET_ACCESS_KEY, aws_session_token=IKR_AWS_SESSION_TOKEN, region_name=IKR_AWS_REGION, profile_name=IKR_AWS_PROFILE) | ||
iam = session.client('iam') | ||
|
||
iamUsers = json.load(open('iam-user-tags.json')) | ||
|
||
def tag_user(userName, tags): | ||
print('Tagging user {}'.format(userName)) | ||
try: | ||
userTags = [] | ||
for t in tags: | ||
userTags.append({ | ||
'Key': t, | ||
'Value': tags[t] | ||
}) | ||
|
||
iam.tag_user( | ||
UserName=userName, | ||
Tags=userTags | ||
) | ||
print('Tag(s) added to user {}'.format(userName)) | ||
except (Exception, ClientError) as ce: | ||
print('Failed to tag user {}. Reason: {}'.format(userName, ce)) | ||
|
||
return userName | ||
|
||
if len(iamUsers) == 0: | ||
print('No IAM users present in user-tagging.json file') | ||
else: | ||
# Tagging each user using a separate thread | ||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor: | ||
results = [executor.submit(tag_user, u, iamUsers[u]) for u in iamUsers] |