Skip to content

Commit

Permalink
fix: add newlines back to billing tag JSON export (#237)
Browse files Browse the repository at this point in the history
Update the Lambda function to add newlines between each
billing tag export line.  These are required by AWS Glue
for parsing the JSON data.

Remove the `import` blocks as the resources have new been
brought into the TF state.
  • Loading branch information
patheard authored Mar 5, 2024
1 parent f285b4b commit 1d5cdab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
TARGET_BUCKET = os.getenv("TARGET_BUCKET")


def lambda_handler(event, context):
def handler(event, context):
"""
Get the tags for all accounts in the organization and save them to an s3 bucket
"""
Expand Down Expand Up @@ -62,8 +62,8 @@ def lambda_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)
# accounts = accounts.replace('},', '},\n')
# accounts = accounts.replace('[{', '[\n{')
accounts = accounts.replace("}, ", "},\n")
accounts = accounts.replace("[{", "[\n{")
logging.info(f"Accounts: {accounts}")

# save accounts to an s3 bucket
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
import os
from unittest.mock import call, patch, MagicMock
from main import lambda_handler
from unittest.mock import patch, MagicMock
from main import handler

TARGET_BUCKET = "TARGET_BUCKET"
ACCOUNT_TAGS_KEY = "account_tags.json"
Expand All @@ -24,14 +23,15 @@ def test_lambda_handler(
}
mock_orgs_list_tags.return_value = {"Tags": [{"Key": "Name", "Value": "Dev"}]}

response = lambda_handler(self.event, self.context)
response = handler(self.event, self.context)

mock_orgs_list_accounts.assert_called()
mock_orgs_list_tags.assert_called_with(ResourceId="123")
mock_s3_put.assert_called_with(
Bucket=TARGET_BUCKET,
Key=ACCOUNT_TAGS_KEY,
Body='[{"Id": "123", "tag_Name": "Dev"}]',
Body="""[
{"Id": "123", "tag_Name": "Dev"}]""",
)

self.assertEqual(response, {"statusCode": 200})
Expand All @@ -52,13 +52,15 @@ def test_lambda_handler_pagination(
{"Tags": [{"Key": "Name", "Value": "Prod"}]},
]

lambda_handler(self.event, self.context)
handler(self.event, self.context)

mock_orgs_list_accounts.assert_any_call(NextToken="token")
mock_orgs_list_tags.assert_any_call(ResourceId="123")
mock_orgs_list_tags.assert_any_call(ResourceId="456")
mock_s3_put.assert_called_with(
Bucket=TARGET_BUCKET,
Key=ACCOUNT_TAGS_KEY,
Body='[{"Id": "123", "tag_Name": "Dev"}, {"Id": "456", "tag_Name": "Prod"}]',
Body="""[
{"Id": "123", "tag_Name": "Dev"},
{"Id": "456", "tag_Name": "Prod"}]""",
)
15 changes: 0 additions & 15 deletions terragrunt/org_account/billing_extract_tags/s3.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
import {
to = module.billing_extract_tags.aws_s3_bucket.this
id = "5bf89a78-1503-4e02-9621-3ac658f558fb"
}

import {
to = module.billing_extract_tags.aws_s3_bucket_public_access_block.this
id = "5bf89a78-1503-4e02-9621-3ac658f558fb"
}

import {
to = aws_s3_bucket_policy.billing_extract_tags
id = "5bf89a78-1503-4e02-9621-3ac658f558fb"
}

module "billing_extract_tags" {
source = "github.com/cds-snc/terraform-modules//S3?ref=v9.2.5"
bucket_name = "5bf89a78-1503-4e02-9621-3ac658f558fb"
Expand Down

0 comments on commit 1d5cdab

Please sign in to comment.