Skip to content

Commit

Permalink
fix: billing extract tags lowercase props (#321)
Browse files Browse the repository at this point in the history
Update the billing extract tags Lambda function so that it outputs the
property keys as lowercase.  This will cause the written JSON file properties to match
the AWS Glue schema that gets created when the file is crawled.
  • Loading branch information
patheard authored Nov 13, 2024
1 parent b7f5770 commit 8785287
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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"}
]""",
)

0 comments on commit 8785287

Please sign in to comment.