Skip to content

Commit

Permalink
Fix - ensure the treasury generation step functions an invoke send-em…
Browse files Browse the repository at this point in the history
…ail lambda (#462)

* fix: typing issues

* fix: ensure step function has permissions to invoke send-email lambda

* fix: use os.environ for consistency

* fix: ensure notifications email is being passed as environment variable
  • Loading branch information
as1729 authored Oct 2, 2024
1 parent 9ed54ee commit be276ee
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
30 changes: 13 additions & 17 deletions python/src/functions/generate_presigned_url_and_send_email.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import os
from typing import Optional, Tuple

import boto3
import chevron
import structlog
from aws_lambda_typing.context import Context
from pydantic import BaseModel
from typing import Optional, Tuple

from src.lib.email import send_email
from src.lib.logging import get_logger, reset_contextvars
from src.lib.s3_helper import get_presigned_url
from src.lib.treasury_generation_common import (
OrganizationObj,
UserObj,
)
from src.lib.email import send_email

from src.lib.treasury_generation_common import OrganizationObj, UserObj

treasury_email_html = """
Your treasury report can be downloaded <a href={url}>here</a>.
Expand Down Expand Up @@ -116,14 +112,14 @@ def process_event(
organization = payload.organization

presigned_url = get_presigned_url(
s3_client = s3_client,
bucket = os.getenv("REPORTING_DATA_BUCKET_NAME"),
key = f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/report.zip",
expiration_time = 60 * 60, # 1 hour
s3_client=s3_client,
bucket=os.environ["REPORTING_DATA_BUCKET_NAME"],
key=f"treasuryreports/{organization.id}/{organization.preferences.current_reporting_period_id}/report.zip",
expiration_time=60 * 60, # 1 hour
)
if presigned_url is None:
raise Exception('Failed to generate signed-URL or file not found')

email_html, email_text, subject = generate_email(
user = user,
presigned_url = presigned_url,
Expand All @@ -133,10 +129,10 @@ def process_event(
return False

send_email(
dest_email = user.email,
email_html = email_html,
email_text = email_text,
subject = subject,
logger = logger,
dest_email=user.email,
email_html=email_html,
email_text=email_text or "",
subject=subject or "",
logger=logger,
)
return True
5 changes: 3 additions & 2 deletions terraform/treasury_generation_lambda_functions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,9 @@ module "lambda_function-email-presigned-url" {
timeout = 60 # 1 minute, in seconds
memory_size = 512
environment_variables = merge(local.lambda_default_environment_variables, {
DD_LAMBDA_HANDLER = "src.functions.generate_presigned_url_and_send_email"
DD_LOGS_INJECTION = "true"
DD_LAMBDA_HANDLER = "src.functions.generate_presigned_url_and_send_email"
DD_LOGS_INJECTION = "true"
NOTIFICATIONS_EMAIL = var.notifications_email_address
})

// Triggers
Expand Down
2 changes: 1 addition & 1 deletion terraform/treasury_generation_step_function.tf
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ module "treasury_generation_step_function" {

service_integrations = {
lambda = {
lambda = [module.lambda_function-treasuryProjectFileGeneration.lambda_function_arn, module.lambda_function-subrecipientTreasuryReportGen.lambda_function_arn, module.lambda_function-cpfCreateArchive.lambda_function_arn]
lambda = [module.lambda_function-treasuryProjectFileGeneration.lambda_function_arn, module.lambda_function-subrecipientTreasuryReportGen.lambda_function_arn, module.lambda_function-cpfCreateArchive.lambda_function_arn, module.lambda_function-email-presigned-url.lambda_function_arn]
}
}

Expand Down

0 comments on commit be276ee

Please sign in to comment.