Skip to content

Commit

Permalink
Move heartbeat to lambdas folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbahrai committed Nov 24, 2022
1 parent 51781ec commit f87d5a5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions heartbeat/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Docker image for the heartbeat hitting the lambda API.

FROM public.ecr.aws/lambda/python:3.9

ENV PYTHONDONTWRITEBYTECODE 1

# Install the function's dependencies
COPY heartbeat/requirements_for_heartbeat.txt ${LAMBDA_TASK_ROOT}
RUN python -m pip install -r requirements_for_heartbeat.txt

# Copy function code
COPY heartbeat/heartbeat.py ${LAMBDA_TASK_ROOT}

CMD [ "heartbeat.handler" ]
33 changes: 33 additions & 0 deletions heartbeat/heartbeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Code to keep the lambda API alive.
"""
import ast
import os
import uuid
from typing import List

from notifications_python_client.errors import HTTPError
from notifications_python_client.notifications import NotificationsAPIClient

API_KEY: str = os.getenv("heartbeat_api_key", "")
# As we can't pass in a list to env var, we pass a str and convert it.
BASE_URL: List[str] = ast.literal_eval(os.getenv("heartbeat_base_url")) # type: ignore
EMAIL_ADDRESS = "success@simulator.amazonses.com"
TEMPLATE_ID: uuid.UUID = os.getenv("heartbeat_template_id") # type: ignore


def handler(event, context):
if not BASE_URL:
print("Variable BASE_URL is missing")
if not API_KEY:
print("Variable API_KEY is missing")
if not TEMPLATE_ID:
print("Variable TEMPLATE_ID is missing")
for base_url in BASE_URL:
notifications_client = NotificationsAPIClient(API_KEY, base_url=base_url)
try:
notifications_client.send_email_notification(email_address=EMAIL_ADDRESS, template_id=TEMPLATE_ID)
print("Email has been sent by {}!".format(base_url))
except HTTPError as e:
print(f"Could not send heartbeat: status={e.status_code}, msg={e.message}")
raise
6 changes: 6 additions & 0 deletions heartbeat/requirements_for_heartbeat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# pyup: ignore file
# This file is autogenerated. Do not edit it manually.
# Run `make freeze-requirements` to update requirements.txt
# with package version changes made in requirements-app.txt

notifications-python-client==6.0.2

0 comments on commit f87d5a5

Please sign in to comment.