Skip to content

Commit

Permalink
chore(billing-report): we are using cronjobs, so dynamodb is not requ…
Browse files Browse the repository at this point in the history
…ired anymore
  • Loading branch information
kiraum committed Oct 3, 2024
1 parent 7014769 commit 1709398
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 81 deletions.
1 change: 0 additions & 1 deletion environments/prod/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ module "billing_report" {
lambda_function_name = "billing-report-lambda"
sns_topic_name = "billing-report-topic"
email_subscription = "[email protected]"
dynamodb_table_name = "CostExplorerProcessedDates"
}

module "route53" {
Expand Down
51 changes: 0 additions & 51 deletions modules/billing_report/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,6 @@
MONTHLY_COST_THRESHOLD = float(os.environ.get("MONTHLY_COST_THRESHOLD", "0.01"))
YEARLY_COST_THRESHOLD = float(os.environ.get("YEARLY_COST_THRESHOLD", "0.01"))

DYNAMODB_TABLE = os.environ.get("DYNAMODB_TABLE", "CostExplorerProcessedDates")


def get_last_processed_date(time_period):
"""
Retrieve the last processed date for a given time period from DynamoDB.
Args:
time_period (str): The time period to check (daily, weekly, monthly, yearly).
Returns:
str: The last processed date as an ISO format string, or None if not found.
"""
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(DYNAMODB_TABLE)

try:
response = table.get_item(Key={"time_period": time_period})
return response.get("Item", {}).get("last_processed_date")
except ClientError as e:
print(f"Error retrieving last processed date: {e}")
return None


def update_last_processed_date(time_period, date):
"""
Update the last processed date for a given time period in DynamoDB.
Args:
time_period (str): The time period to update (daily, weekly, monthly, yearly).
date (datetime.date): The date to set as the last processed date.
"""
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(DYNAMODB_TABLE)

try:
table.put_item(
Item={"time_period": time_period, "last_processed_date": date.isoformat()}
)
except ClientError as e:
print(f"Error updating last processed date: {e}")


def calculate_time_periods(time_period, current_date):
"""
Expand Down Expand Up @@ -316,13 +274,6 @@ def lambda_handler(event, context):
time_period, current_date
)

last_processed_date = get_last_processed_date(time_period)
if last_processed_date and last_processed_date >= end.isoformat():
return {
"statusCode": 200,
"body": f"Cost data for {time_period} ending on {end.isoformat()} has already been processed.",
}

if time_period == "daily":
end = end + datetime.timedelta(days=1)
compare_end = compare_end + datetime.timedelta(days=1)
Expand Down Expand Up @@ -378,8 +329,6 @@ def lambda_handler(event, context):
f"Total cost ({current_costs:.7f} {unit}) did not exceed the threshold ({cost_threshold:.7f} {unit}). No notification sent."
)

update_last_processed_date(time_period, end - datetime.timedelta(days=1))

return {"statusCode": 200, "body": "Cost report generated successfully."}

except ClientError as exc:
Expand Down
23 changes: 0 additions & 23 deletions modules/billing_report/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ resource "aws_iam_role_policy" "lambda_policy" {
"sns:Publish"
]
Resource = aws_sns_topic.billing_report.arn
},
{
Effect = "Allow"
Action = [
"dynamodb:GetItem",
"dynamodb:PutItem"
]
Resource = "arn:aws:dynamodb:*:*:table/${var.dynamodb_table_name}"
}
]
})
Expand Down Expand Up @@ -248,18 +240,3 @@ resource "aws_sns_topic_subscription" "email_subscription" {
protocol = "email"
endpoint = var.email_subscription
}

resource "aws_dynamodb_table" "cost_explorer_processed_dates" {
name = var.dynamodb_table_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "time_period"

attribute {
name = "time_period"
type = "S"
}

tags = {
Name = var.dynamodb_table_name
}
}
6 changes: 0 additions & 6 deletions modules/billing_report/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@ variable "email_subscription" {
description = "Email address for SNS subscription"
type = string
}

variable "dynamodb_table_name" {
description = "Name of the DynamoDB table for storing processed dates"
type = string
default = "CostExplorerProcessedDates"
}

0 comments on commit 1709398

Please sign in to comment.