From f382093a0b9b590576559083823e6aa6c8967355 Mon Sep 17 00:00:00 2001 From: eugenchio Date: Mon, 13 Jan 2025 16:10:04 +0200 Subject: [PATCH] Unite contact form and newsletter signup into a single sevrerless function. --- lambda-functions/contact_form.py | 6 +- lambda-functions/handler.py | 29 +++++++ lambda-functions/newsletter_sign_up.py | 6 +- run.py | 101 ++++++++++++------------- 4 files changed, 82 insertions(+), 60 deletions(-) create mode 100644 lambda-functions/handler.py diff --git a/lambda-functions/contact_form.py b/lambda-functions/contact_form.py index 611ad7c..07f458e 100644 --- a/lambda-functions/contact_form.py +++ b/lambda-functions/contact_form.py @@ -106,19 +106,19 @@ def send_email(event_body): logger.info('send_email(): success') -def lambda_handler(event, context): +def handle_contact_form(event, context): assert event.get('subject') is not None assert event.get('text') is not None assert event.get('email') is not None assert event.get('name') is not None assert event.get('company') is not None - logger.info('lambda_handler(): invoked. event={}'.format(event)) + logger.info('handle_contact_form(): invoked. event={}'.format(event)) try: # try to create CRM lead create_crm_lead(event) except Exception as e: sentry_sdk.capture_exception(e) - logger.exception('lambda_handler(): create_lead failed') + logger.exception('handle_contact_form(): create_lead failed') # fallback to sending email if CRM lead creation fails send_email(event) diff --git a/lambda-functions/handler.py b/lambda-functions/handler.py new file mode 100644 index 0000000..9f71c10 --- /dev/null +++ b/lambda-functions/handler.py @@ -0,0 +1,29 @@ +import logging + +import sentry_sdk +from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration +from newsletter_sign_up import handle_newsletter_sign_up +from contact_form import handle_contact_form + + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + + +sentry_sdk.init( + dsn="https://5dd58ec394b556f352248ba56bc7876f@o202054.ingest.sentry.io" + + "/4506218347954176", + integrations=[AwsLambdaIntegration()], + traces_sample_rate=1.0, + profiles_sample_rate=1.0, +) + + +def lambda_handler(event, context): + logger.info('lambda_handler(): invoked. event={}'.format(event)) + try: + # try to create CRM lead + handle_contact_form(event, context) + except Exception as e: + sentry_sdk.capture_exception(e) + logger.exception('lambda_handler(): create_lead failed') diff --git a/lambda-functions/newsletter_sign_up.py b/lambda-functions/newsletter_sign_up.py index d04d084..e078568 100755 --- a/lambda-functions/newsletter_sign_up.py +++ b/lambda-functions/newsletter_sign_up.py @@ -50,12 +50,12 @@ def add_crisp_contact(event_body): logger.info('add_crisp_contact(): success') -def lambda_handler(event): +def handle_newsletter_sign_up(event): assert event.get('email') is not None - logger.info('lambda_handler(): invoked. event={}'.format(event)) + logger.info('handle_newsletter_sign_up(): add_crisp_contact invoked. event={}'.format(event)) try: add_crisp_contact(event) except Exception as e: sentry_sdk.capture_exception(e) - logger.exception('lambda_handler(): add_crisp_contact failed') + logger.exception('handle_newsletter_sign_up(): add_crisp_contact failed') diff --git a/run.py b/run.py index 957b8d4..ad47ef1 100755 --- a/run.py +++ b/run.py @@ -32,68 +32,61 @@ def deploy_lambda(): run(f'zip -qr ../{code_archive_name} .', cwd=package_deps_path) run( - f'zip -qg {code_archive_name} pipedrive.py contact_form.py ' - f'newsletter_sign_up.py', + f'zip -qg {code_archive_name} ./*.py', cwd=package_path, ) code_archive_path = f'fileb://{package_path}/{code_archive_name}' - functions = [ - 'LandingContactFormLambda', - 'LandingNewsletterSignUpLambda', - ] + function_name = resource_details( + settings.PROJECT_NAME, + 'LandingLambda', + )['PhysicalResourceId'] + aws( + f'lambda update-function-code ' + f'--function-name {function_name} ' + f'--zip-file {code_archive_path} ' + f'--publish', + ) - for function in functions: - function_name = resource_details( - settings.PROJECT_NAME, - function, - )['PhysicalResourceId'] - aws( - f'lambda update-function-code ' - f'--function-name {function_name} ' - f'--zip-file {code_archive_path} ' - f'--publish', + def get_function_config(): + return aws( + f'lambda get-function --function-name {function_name}', + )['Configuration'] + + while True: + function_config = get_function_config() + status = function_config['LastUpdateStatus'] + if status != 'InProgress': + break + time.sleep(1) + + if status == 'Failed': + status_reason = function_config['LastUpdateStatusReason'] + status_reason_code = function_config['LastUpdateStatusReasonCode'] + raise RuntimeError( + f'Failed to update lambda function. ' + f'Reason: {status_reason_code} | {status_reason}.', ) - def get_function_config(): - return aws( - f'lambda get-function --function-name {function_name}', - )['Configuration'] - - while True: - function_config = get_function_config() - status = function_config['LastUpdateStatus'] - if status != 'InProgress': - break - time.sleep(1) - - if status == 'Failed': - status_reason = function_config['LastUpdateStatusReason'] - status_reason_code = function_config['LastUpdateStatusReasonCode'] - raise RuntimeError( - f'Failed to update lambda function. ' - f'Reason: {status_reason_code} | {status_reason}.', - ) - - environment = { - 'Variables': { - 'CONTACT_FORM_FROM_EMAIL': settings.CONTACT_FORM_FROM_EMAIL, - 'CONTACT_FORM_TO_EMAIL': settings.CONTACT_FORM_TO_EMAIL, - 'PIPE_DRIVE_API_TOKEN': os.environ['PIPE_DRIVE_API_TOKEN'], - 'PIPE_DRIVE_LEAD_CUSTOM_DATA': - os.environ['PIPE_DRIVE_LEAD_CUSTOM_DATA'], - 'CRISP_WEBSITE_ID': settings.CRISP_WEBSITE_ID, - 'CRISP_TOKEN_ID': os.environ['CRISP_TOKEN_ID'], - 'CRISP_TOKEN_KEY': os.environ['CRISP_TOKEN_KEY'], - }, - } - aws( - f'lambda update-function-configuration ' - f'--function-name {function_name} ' - '--timeout 50 ' - f'--environment {quote(json.dumps(environment))}', - ) + environment = { + 'Variables': { + 'CONTACT_FORM_FROM_EMAIL': settings.CONTACT_FORM_FROM_EMAIL, + 'CONTACT_FORM_TO_EMAIL': settings.CONTACT_FORM_TO_EMAIL, + 'PIPE_DRIVE_API_TOKEN': os.environ['PIPE_DRIVE_API_TOKEN'], + 'PIPE_DRIVE_LEAD_CUSTOM_DATA': + os.environ['PIPE_DRIVE_LEAD_CUSTOM_DATA'], + 'CRISP_WEBSITE_ID': settings.CRISP_WEBSITE_ID, + 'CRISP_TOKEN_ID': os.environ['CRISP_TOKEN_ID'], + 'CRISP_TOKEN_KEY': os.environ['CRISP_TOKEN_KEY'], + }, + } + aws( + f'lambda update-function-configuration ' + f'--function-name {function_name} ' + '--timeout 50 ' + f'--environment {quote(json.dumps(environment))}', + ) if __name__ == '__main__':