diff --git a/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/main.py b/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/main.py index eb746d4c..5dd21c89 100644 --- a/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/main.py +++ b/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/main.py @@ -3,6 +3,7 @@ This is then used to enrich the billing data with the account tags to allow for business unit filtering. """ + import json import logging import os @@ -61,11 +62,26 @@ def handler(event, context): # .write json to string and add a newline between each record logging.info("Writing account tags to json") - accounts = json.dumps(accounts, default=str, indent=2) - logging.info(f"Accounts: {accounts}") + accounts_json = ( + "[\n" + + ",\n".join( + json.dumps( + {k.lower(): v for k, v in account.items()}, + default=str, + separators=(",", ": "), + ) + for account in accounts + ) + + "\n]" + ) + logging.info(f"Accounts: {accounts_json}") # save accounts to an s3 bucket logging.info("Saving account tags to s3") - s3.put_object(Bucket=TARGET_BUCKET, Key="operations/aws/organization/account-tags.json", Body=accounts) + s3.put_object( + Bucket=TARGET_BUCKET, + Key="operations/aws/organization/account-tags.json", + Body=accounts_json, + ) return {"statusCode": 200} diff --git a/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/test_main.py b/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/test_main.py index dd98c31d..86733de4 100644 --- a/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/test_main.py +++ b/terragrunt/org_account/cost_usage_report/lambdas/billing_extract_tags/test_main.py @@ -3,7 +3,7 @@ from main import handler TARGET_BUCKET = "TARGET_BUCKET" -ACCOUNT_TAGS_KEY = "account_tags.json" +ACCOUNT_TAGS_KEY = "operations/aws/organization/account-tags.json" class TestLambdaHandler(unittest.TestCase): @@ -31,7 +31,8 @@ def test_lambda_handler( Bucket=TARGET_BUCKET, Key=ACCOUNT_TAGS_KEY, Body="""[ -{"Id": "123", "tag_Name": "Dev"}]""", +{"id": "123","tag_name": "Dev"} +]""", ) self.assertEqual(response, {"statusCode": 200}) @@ -61,6 +62,7 @@ def test_lambda_handler_pagination( Bucket=TARGET_BUCKET, Key=ACCOUNT_TAGS_KEY, Body="""[ -{"Id": "123", "tag_Name": "Dev"}, -{"Id": "456", "tag_Name": "Prod"}]""", +{"id": "123","tag_name": "Dev"}, +{"id": "456","tag_name": "Prod"} +]""", )