Skip to content

Commit

Permalink
chore(billing-report): make recepients a list
Browse files Browse the repository at this point in the history
  • Loading branch information
kiraum committed Oct 4, 2024
1 parent 60e9a13 commit eaaff47
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
4 changes: 1 addition & 3 deletions environments/prod/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ module "billing_report" {
lambda_function_name = "billing-report-lambda"
ses_sender_email = "[email protected]"
ses_domain = "kiraum.it"
recipient_email = "[email protected]"
recipient_emails = ["[email protected]"]
notification_service = "SNS"
daily_cost_threshold = "0.01"
weekly_cost_threshold = "1.00"
monthly_cost_threshold = "5.00"
yearly_cost_threshold = "60.00"
}



module "route53" {
source = "../../modules/route53"

Expand Down
23 changes: 12 additions & 11 deletions modules/billing_report/lambda_function.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""AWS Cost Explorer Lambda function to report costs for various time periods."""

import datetime
import json
import os

import boto3
Expand Down Expand Up @@ -116,15 +117,15 @@ def generate_text_report(
text_template = """
AWS Cost Report for {time_period}
Period: {start_date} to {end_date}
- Period: {start_date} to {end_date}
Summary:
- Summary:
Current {time_period} cost: {current_costs:.7f} {unit}
Previous {time_period} cost: {compare_costs:.7f} {unit}
Difference: {difference:.7f} {unit}
Threshold: {threshold:.7f} {unit}
Breakdown by Service:
- Breakdown by Service:
{service_breakdown}
"""

Expand Down Expand Up @@ -290,8 +291,8 @@ def send_sns(message, subject):
try:
response = sns.publish(TopicArn=topic_arn, Message=message, Subject=subject)
print(f"Message sent to SNS! Message ID: {response['MessageId']}")
except ClientError as e:
print(f"An error occurred while sending message via SNS: {e}")
except ClientError as exc:
print(f"An error occurred while sending message via SNS: {exc}")


def send_ses(message, subject):
Expand All @@ -303,18 +304,18 @@ def send_ses(message, subject):
"""
ses = boto3.client("ses")
sender = os.environ["SES_SENDER_EMAIL"]
recipient = os.environ["SES_RECIPIENT_EMAIL"]
recipients = json.loads(os.environ["RECIPIENT_EMAILS"])

if not sender or not recipient:
if not sender or not recipients:
raise ValueError(
"SES_SENDER_EMAIL and SES_RECIPIENT_EMAIL must be set in the environment variables"
"SES_SENDER_EMAIL and RECIPIENT_EMAILS must be set in the environment variables"
)

try:
response = ses.send_email(
Source=sender,
Destination={
"ToAddresses": [recipient],
"ToAddresses": recipients,
},
Message={
"Subject": {
Expand All @@ -328,8 +329,8 @@ def send_ses(message, subject):
},
)
print(f"Email sent! Message ID: {response['MessageId']}")
except ClientError as e:
print(f"An error occurred while sending email via SES: {e}")
except ClientError as exc:
print(f"An error occurred while sending email via SES: {exc}")


def send_notification(message, subject):
Expand Down
5 changes: 3 additions & 2 deletions modules/billing_report/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ resource "aws_lambda_function" "billing_report" {
environment {
variables = {
SES_SENDER_EMAIL = var.ses_sender_email
recipient_email = var.recipient_email
RECIPIENT_EMAILS = jsonencode(var.recipient_emails)
SNS_TOPIC_ARN = aws_sns_topic.billing_report.arn
NOTIFICATION_SERVICE = var.notification_service
DAILY_COST_THRESHOLD = var.daily_cost_threshold
Expand Down Expand Up @@ -264,7 +264,8 @@ resource "aws_ses_domain_mail_from" "ses_domain_mail_from" {
}

resource "aws_sns_topic_subscription" "billing_report_email" {
count = length(var.recipient_emails)
topic_arn = aws_sns_topic.billing_report.arn
protocol = "email"
endpoint = var.recipient_email
endpoint = var.recipient_emails[count.index]
}
4 changes: 2 additions & 2 deletions modules/billing_report/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ output "ses_sender_email" {
value = var.ses_sender_email
}

output "recipient_email" {
output "recipient_emails" {
description = "The email address receiving emails"
value = var.recipient_email
value = var.recipient_emails
}
6 changes: 3 additions & 3 deletions modules/billing_report/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ variable "ses_sender_email" {
type = string
}

variable "recipient_email" {
description = "Email address to receive emails"
type = string
variable "recipient_emails" {
description = "List of email addresses to receive emails"
type = list(string)
}

variable "ses_domain" {
Expand Down

0 comments on commit eaaff47

Please sign in to comment.