-
Notifications
You must be signed in to change notification settings - Fork 229
New feature: terraform target by tags #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
igordust
wants to merge
7
commits into
awslabs:master
Choose a base branch
from
igordust:feature/terraform_target_by_tags
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
93980b5
Added support terraform pipelines for targeting accounts using tags
igordust 76ecbb5
Merge branch 'awslabs:master' into feature/terraform_target_by_tags
igordust 646a3b4
Removed a useless print and switched to logger for another one
igordust 217e717
Updated docs with new syntax for tag filtering
igordust d38de34
Switched to resourcegroupstaggingapi for tag filtering
igordust aeb42ce
Added clearer messages in adf_terraform.sh for account targeting
igordust 2a28924
Merge branch 'master' into feature/terraform_target_by_tags
igordust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
MANAGEMENT_ACCOUNT_ID = os.environ["MANAGEMENT_ACCOUNT_ID"] | ||
TARGET_OUS = os.environ.get("TARGET_OUS") | ||
TARGET_TAGS = os.environ.get("TARGET_TAGS") | ||
REGION_DEFAULT = os.environ["AWS_REGION"] | ||
PARTITION = get_partition(REGION_DEFAULT) | ||
sts = boto3.client('sts') | ||
|
@@ -38,6 +39,12 @@ def main(): | |
with open('accounts_from_ous.json', 'w', encoding='utf-8') as outfile: | ||
json.dump(accounts_from_ous, outfile) | ||
|
||
if TARGET_TAGS: | ||
print("filtering by tags") | ||
igordust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
accounts_from_tags = get_accounts_from_tags() | ||
with open('accounts_from_tags.json', 'w', encoding='utf-8') as outfile: | ||
json.dump(accounts_from_tags, outfile) | ||
|
||
|
||
def list_organizational_units_for_parent(parent_ou): | ||
organizations = get_boto3_client( | ||
|
@@ -90,6 +97,45 @@ def get_accounts(): | |
) | ||
|
||
|
||
def get_accounts_from_tags(): | ||
accounts = get_accounts() | ||
tag_filters = [] | ||
for tag in TARGET_TAGS.split(','): | ||
tag_name = tag.split('=')[0] | ||
tag_value = tag.split('=')[1] | ||
tag_filters.append({ | ||
"Key": tag_name, | ||
"Value": tag_value}) | ||
|
||
LOGGER.info( | ||
"Tag filters %s", | ||
tag_filters | ||
) | ||
|
||
organizations = get_boto3_client( | ||
'organizations', | ||
( | ||
f'arn:{PARTITION}:sts::{MANAGEMENT_ACCOUNT_ID}:role/' | ||
f'{CROSS_ACCOUNT_ACCESS_ROLE}-readonly' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be updated to the new role added in v4. |
||
), | ||
'getaccountIDs', | ||
) | ||
filtered_accounts = [] | ||
for account in accounts: | ||
tags = account['Tags'] = organizations.list_tags_for_resource( | ||
igordust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ResourceId=account['AccountId'] | ||
)['Tags'] | ||
for tag_filter in tag_filters: | ||
found = list(filter(lambda item: ( | ||
item["Key"] == tag_filter["Key"] and item["Value"] == tag_filter["Value"]), tags)) | ||
if len(found) > 0: | ||
print( | ||
igordust marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f"{account['AccountId']} matched {tag_filter['Key']}={tag_filter['Value']}") | ||
filtered_accounts.append(account) | ||
break | ||
return filtered_accounts | ||
|
||
|
||
def get_accounts_from_ous(): | ||
parent_ou_id = None | ||
account_list = [] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.