Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ verify_ssl = true
name = "pypi"

[packages]
sentry-sdk = "*"

[dev-packages]
bandit = "*"
black = "*"
coverage = "*"
coveralls = "*"
mypy = "*"
pyflakes = "==2.4.0"
pylama = {extras = ["all"], version = "*"}
pytest = "*"

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ A template repository for creating Python lambda functions.
- Dependabot alerts
- Dependabot security updates
- Secret scanning
7. Create a Sentry project for the app if needed (we want this for most apps):
- Send initial exceptions to Sentry project for dev, stage, and prod environments to create them.
- Create an alert for the prod environment only, with notifications sent to the appropriate team(s).
- If *not* using Sentry, delete Sentry configuration from my_function.py and test_my_function_.py, and remove sentry_sdk from project dependencies.

# my_function

Expand All @@ -29,6 +33,7 @@ Description of the function/functions.

## Required ENV

- `SENTRY_DSN` = If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development.
- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform.

## Running locally
Expand All @@ -44,7 +49,7 @@ Description of the function/functions.
- Run the default handler for the container:

```bash
docker run -p 9000:8080 my_function:latest
docker run -e WORKSPACE=dev -p 9000:8080 my_function:latest
```

- Post to the container:
Expand Down
22 changes: 22 additions & 0 deletions lambdas/my_function.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import json
import logging
import os

import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

env = os.getenv("WORKSPACE")
if sentry_dsn := os.getenv("SENTRY_DSN"):
sentry = sentry_sdk.init(
dsn=sentry_dsn,
environment=env,
integrations=[
AwsLambdaIntegration(),
],
traces_sample_rate=1.0,
)
logger.info("Sentry DSN found, exceptions will be sent to Sentry with env=%s", env)
else:
logger.info("No Sentry DSN found, exceptions will not be sent to Sentry")


def lambda_handler(event: dict, context: object) -> str: # noqa
if not os.getenv("WORKSPACE"):
raise RuntimeError("Required env variable WORKSPACE is not set")

logger.debug(json.dumps(event))

return "You have successfully called this lambda!"
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ max_line_length = 90
[pylama:isort]
profile = black

[pylama:pydocstyle]
convention = pep257

[tool:pytest]
log_level = DEBUG
5 changes: 0 additions & 5 deletions tests/test_function.py

This file was deleted.

34 changes: 34 additions & 0 deletions tests/test_my_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from importlib import reload

import pytest

from lambdas import my_function


def test_my_function_configures_sentry_if_dsn_present(caplog, monkeypatch):
monkeypatch.setenv("SENTRY_DSN", "https://1234567890@00000.ingest.sentry.io/123456")
reload(my_function)
assert (
"Sentry DSN found, exceptions will be sent to Sentry with env=test"
in caplog.text
)


def test_my_function_doesnt_configure_sentry_if_dsn_not_present(caplog, monkeypatch):
monkeypatch.delenv("SENTRY_DSN", raising=False)
reload(my_function)
assert "No Sentry DSN found, exceptions will not be sent to Sentry" in caplog.text


def test_lambda_handler_missing_workspace_env_raises_error(monkeypatch):
monkeypatch.delenv("WORKSPACE", raising=False)
with pytest.raises(RuntimeError) as error:
my_function.lambda_handler({}, {})
assert "Required env variable WORKSPACE is not set" in str(error)


def test_my_function():
assert (
my_function.lambda_handler({}, {})
== "You have successfully called this lambda!"
)