Skip to content

Commit

Permalink
Unite contact form and newsletter signup into a single sevrerless fun…
Browse files Browse the repository at this point in the history
…ction.
  • Loading branch information
eugenchio committed Jan 13, 2025
1 parent 2e5f97c commit f382093
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 60 deletions.
6 changes: 3 additions & 3 deletions lambda-functions/contact_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
29 changes: 29 additions & 0 deletions lambda-functions/handler.py
Original file line number Diff line number Diff line change
@@ -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://[email protected]"
+ "/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')
6 changes: 3 additions & 3 deletions lambda-functions/newsletter_sign_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
101 changes: 47 additions & 54 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit f382093

Please sign in to comment.