diff --git a/environments/prod/main.tf b/environments/prod/main.tf index c69a65b..c0f8e5f 100644 --- a/environments/prod/main.tf +++ b/environments/prod/main.tf @@ -45,7 +45,6 @@ module "billing_report" { lambda_function_name = "billing-report-lambda" sns_topic_name = "billing-report-topic" email_subscription = "tfgoncalves@xpto.it" - dynamodb_table_name = "CostExplorerProcessedDates" } module "route53" { diff --git a/modules/billing_report/lambda_function.py b/modules/billing_report/lambda_function.py index 81410ec..07e9474 100644 --- a/modules/billing_report/lambda_function.py +++ b/modules/billing_report/lambda_function.py @@ -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): """ @@ -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) @@ -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: diff --git a/modules/billing_report/main.tf b/modules/billing_report/main.tf index 24b5a9e..af5a75e 100644 --- a/modules/billing_report/main.tf +++ b/modules/billing_report/main.tf @@ -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}" } ] }) @@ -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 - } -} diff --git a/modules/billing_report/variables.tf b/modules/billing_report/variables.tf index ec878c5..4d722a7 100644 --- a/modules/billing_report/variables.tf +++ b/modules/billing_report/variables.tf @@ -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" -}