-
Notifications
You must be signed in to change notification settings - Fork 7
Update lambda stack #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gerhc
wants to merge
2
commits into
master
Choose a base branch
from
update-lambda-stack
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copy this file to .env and fill in the values. | ||
| # .env is gitignored — never commit real secrets. | ||
|
|
||
| FORM_SHARED_SECRET= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| -include .env | ||
| export | ||
|
|
||
| DEV_OVERRIDES = Stage=dev FromEmail=hello@tryolabs.com DestinationEmail=german@tryolabs.com FormSharedSecret=$(FORM_SHARED_SECRET) | ||
| PROD_OVERRIDES = Stage=prod FromEmail=hello@tryolabs.com DestinationEmail=hello@tryolabs.com FormSharedSecret=$(FORM_SHARED_SECRET) | ||
|
|
||
| .PHONY: build deploy-dev deploy-prod logs-dev logs-prod test lint | ||
|
|
||
| build: | ||
| sam build --use-container | ||
|
|
||
| deploy-dev: build | ||
| sam deploy --config-env dev --parameter-overrides "$(DEV_OVERRIDES)" | ||
|
|
||
| deploy-prod: build | ||
| sam deploy --config-env prod --parameter-overrides "$(PROD_OVERRIDES)" | ||
|
|
||
| logs-dev: | ||
| sam logs --stack-name lambda-mailer-v2-dev --region us-west-2 --tail | ||
|
|
||
| logs-prod: | ||
| sam logs --stack-name lambda-mailer-v2-prod --region us-west-2 --tail | ||
|
|
||
| test: | ||
| uv run pytest | ||
|
|
||
| lint: | ||
| uv run ruff check . | ||
| uv run ruff format --check . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,116 @@ | ||
| # Tryolabs Lambda mailer | ||
| # Tryolabs Lambda Mailer | ||
|
|
||
| Uses [Zappa](https://github.com/Miserlou/Zappa) to deploy a serverless endpoint that receives a JSON and sends an email to a destination address. Made to support contact forms in our static website. | ||
| Serverless contact form endpoint deployed to AWS Lambda via [AWS SAM](https://aws.amazon.com/serverless/sam/). Receives a JSON payload and sends an email using Amazon SES. | ||
|
|
||
| Built with [FastAPI](https://fastapi.tiangolo.com/) + [Mangum](https://mangum.fastapiexpert.com/) (ASGI→Lambda adapter). | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [uv](https://docs.astral.sh/uv/) (Python package manager) | ||
| - [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) | ||
| - [Docker](https://www.docker.com/) (required for `sam build --use-container`) | ||
| - Valid AWS credentials configured (`~/.aws/credentials` or environment variables) | ||
| - A verified SES sender email address in your AWS account | ||
|
|
||
| ## Setup | ||
|
|
||
| _Before you begin, make sure you have a valid AWS account and your [AWS credentials file](https://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs) is properly installed._ | ||
| **1. Install dependencies:** | ||
|
|
||
| ```bash | ||
| uv sync --all-extras | ||
| ``` | ||
|
|
||
| **2. Configure secrets:** | ||
|
|
||
| ```bash | ||
| cp .env.example .env | ||
| # Edit .env and fill in FORM_SHARED_SECRET with a strong random value: | ||
| openssl rand -hex 32 | ||
| ``` | ||
|
|
||
| `.env` is gitignored — never commit it. | ||
|
|
||
| ## Deployment | ||
|
|
||
| All deploy commands are in the `Makefile` and read secrets from `.env` automatically. | ||
|
|
||
| Deploy to **dev**: | ||
|
|
||
| ```bash | ||
| make deploy-dev | ||
| ``` | ||
|
|
||
| Make sure you are in a virtualenv, and run: | ||
| Deploy to **production**: | ||
|
|
||
| ```bash | ||
| make deploy-prod | ||
| ``` | ||
| $ pip install -r requirements.txt | ||
| $ zappa deploy dev | ||
|
|
||
| ### Environment variables / parameters | ||
|
|
||
| | Parameter | Source | Description | | ||
| |---|---|---| | ||
| | `FromEmail` | `samconfig.toml` | Verified SES sender address | | ||
| | `DestinationEmail` | `samconfig.toml` | Address to receive form submissions | | ||
| | `FormSharedSecret` | `.env` | Shared secret — Next.js sends this in `X-Form-Secret` header; requests without it are rejected with 403 | | ||
|
|
||
| Other deploy config (stack name, region) is in `samconfig.toml`. | ||
|
|
||
| ## Development | ||
|
|
||
| Run the API locally (requires SAM CLI): | ||
|
|
||
| ```bash | ||
| sam local start-api --env-vars env.json | ||
| ``` | ||
|
|
||
| To redeploy: | ||
| Or directly with uvicorn: | ||
|
|
||
| ```bash | ||
| uv run uvicorn api:app --reload | ||
| ``` | ||
|
|
||
| $ zappa update dev | ||
| ### Linting & formatting | ||
|
|
||
| To deploy the production version: | ||
| ```bash | ||
| make lint | ||
| # or individually: | ||
| uv run ruff check . | ||
| uv run ruff format . | ||
| ``` | ||
|
|
||
| $ zappa deploy prod | ||
| ### Type checking | ||
|
|
||
| ```bash | ||
| uv run mypy api.py handler.py | ||
| ``` | ||
|
|
||
| ### Environment variables | ||
| In the `zappa_settings.json` file, you can edit the environment variables for `dev` and `prod`: | ||
| ### Testing | ||
|
|
||
| ```javascript | ||
| { | ||
| "dev": { | ||
| ... | ||
| "environment_variables": { | ||
| "FROM_EMAIL": "alan@tryolabs.com", | ||
| "DESTINATION_EMAIL": "alan@tryolabs.com" | ||
| } | ||
| }, | ||
| ... | ||
| } | ||
| ```bash | ||
| make test | ||
| # or: | ||
| uv run pytest | ||
| ``` | ||
|
|
||
| ## Testing | ||
| ### Logs | ||
|
|
||
| With [httpie](https://httpie.org/), invoke the endpoint0 with the `message.json` test file as payload: | ||
| ```bash | ||
| make logs-dev # tail dev Lambda logs | ||
| make logs-prod # tail prod Lambda logs | ||
| ``` | ||
|
|
||
| $ http <endpoint> < message.json | ||
| ## Manual testing | ||
|
|
||
| With [httpie](https://httpie.org/), invoke the endpoint with the `message.json` test file: | ||
|
|
||
| ```bash | ||
| http POST <endpoint-url> X-Form-Secret:<secret> < message.json | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| Copyright (c) 2016 [Tryolabs](https://tryolabs.com). | ||
|
|
||
| Released under the MIT License (See LICENSE). | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
README suggests running
uv run uvicorn api:app --reload, butuvicornisn’t declared in the project’s dependencies/dev dependencies. Either adduvicorn(and any needed extras) to the dev dependency set, or update the docs to use an installed runner.