-
Notifications
You must be signed in to change notification settings - Fork 224
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
New feature: terraform target by tags #685
base: master
Are you sure you want to change the base?
Changes from all commits
93980b5
76ecbb5
646a3b4
217e717
d38de34
aeb42ce
2a28924
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ | |||||||
import boto3 | ||||||||
from paginator import paginator | ||||||||
from partition import get_partition | ||||||||
from partition import get_organization_api_region | ||||||||
|
||||||||
# Configure logging | ||||||||
logging.basicConfig(level=logging.INFO) | ||||||||
|
@@ -20,10 +21,12 @@ | |||||||
|
||||||||
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') | ||||||||
ssm = boto3.client('ssm') | ||||||||
organizations = boto3.client('organizations') | ||||||||
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. Could you change this to include a higher retry count? An example how to change it: https://github.com/awslabs/aws-deployment-framework/pull/774/files#diff-05a19c0ecc528b81f98611295fccbb53ec8ce9f20937f67308f3bc886e4a5018R83-R89 |
||||||||
response = ssm.get_parameter(Name='cross_account_access_role') | ||||||||
CROSS_ACCOUNT_ACCESS_ROLE = response['Parameter']['Value'] | ||||||||
|
||||||||
|
@@ -38,6 +41,11 @@ def main(): | |||||||
with open('accounts_from_ous.json', 'w', encoding='utf-8') as outfile: | ||||||||
json.dump(accounts_from_ous, outfile) | ||||||||
|
||||||||
if TARGET_TAGS: | ||||||||
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 +98,41 @@ def get_accounts(): | |||||||
) | ||||||||
|
||||||||
|
||||||||
def get_accounts_from_tags(): | ||||||||
tag_filters = [] | ||||||||
for tags in TARGET_TAGS.split(";"): | ||||||||
tag_name = tags.split(",", 1)[0].split("=")[1] | ||||||||
tag_values = tags.split(",", 1)[1].split("=")[1].split(",") | ||||||||
tag_filters.append({ | ||||||||
"Key": tag_name, | ||||||||
"Values": tag_values}) | ||||||||
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.
Suggested change
|
||||||||
LOGGER.info( | ||||||||
"Tag filters %s", | ||||||||
tag_filters | ||||||||
) | ||||||||
organization_api_region = get_organization_api_region(REGION_DEFAULT) | ||||||||
print(organization_api_region) | ||||||||
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.
Suggested change
|
||||||||
tags_client = get_boto3_client( | ||||||||
'resourcegroupstaggingapi', | ||||||||
( | ||||||||
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. |
||||||||
), | ||||||||
'getaccountIDsFromTags', | ||||||||
region_name=organization_api_region, | ||||||||
) | ||||||||
account_ids = [] | ||||||||
for resource in paginator( | ||||||||
tags_client.get_resources, | ||||||||
TagFilters=tag_filters, | ||||||||
ResourceTypeFilters=["organizations"], | ||||||||
): | ||||||||
arn = resource["ResourceARN"] | ||||||||
account_id = arn.split("/")[::-1][0] | ||||||||
account_ids.append({"AccountId": account_id}) | ||||||||
return account_ids | ||||||||
|
||||||||
|
||||||||
def get_accounts_from_ous(): | ||||||||
parent_ou_id = None | ||||||||
account_list = [] | ||||||||
|
@@ -142,7 +185,7 @@ def get_accounts_from_ous(): | |||||||
return account_list | ||||||||
|
||||||||
|
||||||||
def get_boto3_client(service, role, session_name): | ||||||||
def get_boto3_client(service, role, session_name, region_name=''): | ||||||||
role = sts.assume_role( | ||||||||
RoleArn=role, | ||||||||
RoleSessionName=session_name, | ||||||||
|
@@ -153,7 +196,10 @@ def get_boto3_client(service, role, session_name): | |||||||
aws_secret_access_key=role['Credentials']['SecretAccessKey'], | ||||||||
aws_session_token=role['Credentials']['SessionToken'] | ||||||||
) | ||||||||
return session.client(service) | ||||||||
if region_name != '': | ||||||||
return session.client(service, region_name=region_name) | ||||||||
else: | ||||||||
return session.client(service) | ||||||||
|
||||||||
|
||||||||
def get_account_recursive(org_client: boto3.client, ou_id: str, path: str) -> list: | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.