Skip to content

Commit 988f932

Browse files
authored
Merge pull request #195 from ryansurf/feat-aws-lambda
Feat aws lambda
2 parents 3c85de3 + 58f3b07 commit 988f932

14 files changed

Lines changed: 268 additions & 203 deletions

.gitignore

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,23 @@ cython_debug/
162162
.idea/
163163

164164
*.cache.sqlite
165-
*.pyc
165+
*.pyc
166+
167+
# Terraform
168+
**/.terraform/
169+
*.tfstate
170+
*.tfstate.backup
171+
*.tfplan
172+
*.tfvars
173+
!*.tfvars.example
174+
175+
# Lambda build artifacts
176+
terraform/lambda.zip
177+
package/
178+
requirements.txt
179+
180+
# macOS
181+
.DS_Store
182+
183+
# IDE
184+
.vscode/

Dockerfile.lambda

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM public.ecr.aws/lambda/python:3.12
2+
3+
WORKDIR /var/task
4+
5+
COPY pyproject.toml poetry.lock ./
6+
COPY .env.example .env
7+
8+
RUN pip install poetry && \
9+
poetry config virtualenvs.create false && \
10+
poetry install --no-interaction --no-ansi --without dev --no-root
11+
12+
COPY src/ ./src/
13+
COPY help.txt ./
14+
15+
CMD ["src/handler.handler"]

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Inspired by [wttr.in](https://github.com/chubin/wttr.in) · [Documentation](http
3434
- [MongoDB](#mongodb)
3535
- [GPT Surf Report](#-gpt-surf-report)
3636
- [Tech Stack](#-tech-stack)
37+
- [Architecture](#-architecture)
3738
- [Contributing](#-contributing)
3839
- [Contributors](#-contributors)
3940

@@ -332,6 +333,10 @@ GPT_PROMPT="What are some good places to eat around this surf spot?"
332333

333334
---
334335

336+
## 📐 Architecture
337+
338+
[Architecture](docs/architecture.png)
339+
335340
## 📈 Contributing
336341

337342
Thank you for considering contributing to cli-surf!

docs/architecture.png

112 KB
Loading

makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,38 @@ clean:
6565

6666
.PHONY: all
6767
all: format lint test
68+
69+
.PHONY: lambda-zip
70+
lambda-zip:
71+
poetry export -f requirements.txt --output requirements.txt --without-hashes
72+
pip install -r requirements.txt -t ./package
73+
cp -r src/ ./package/src/
74+
cd package && zip -r ../terraform/lambda.zip . && cd ..
75+
76+
ECR_REGION ?= us-west-1
77+
ECR_ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query Account --output text)
78+
ECR_REPO = $(ECR_ACCOUNT_ID).dkr.ecr.$(ECR_REGION).amazonaws.com/cli-surf
79+
80+
.PHONY: ecr-login
81+
ecr-login:
82+
aws ecr get-login-password --region $(ECR_REGION) | docker login --username AWS --password-stdin $(ECR_ACCOUNT_ID).dkr.ecr.$(ECR_REGION).amazonaws.com
83+
84+
.PHONY: docker-build
85+
docker-build:
86+
docker build --platform linux/amd64 --provenance=false -f Dockerfile.lambda -t cli-surf:latest .
87+
88+
.PHONY: docker-push
89+
docker-push: ecr-login docker-build
90+
docker tag cli-surf:latest $(ECR_REPO):latest
91+
docker push $(ECR_REPO):latest
92+
93+
.PHONY: update-lambda
94+
update-lambda:
95+
$(eval DIGEST := $(shell aws ecr describe-images --repository-name cli-surf --region $(ECR_REGION) --query 'sort_by(imageDetails, &imagePushedAt)[-1].imageDigest' --output text))
96+
aws lambda update-function-code --function-name cli-surf --region $(ECR_REGION) --image-uri $(ECR_REPO)@$(DIGEST)
97+
98+
.PHONY: deploy
99+
deploy: docker-push update-lambda
100+
terraform -chdir=terraform apply
101+
102+

poetry.lock

Lines changed: 16 additions & 199 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ streamlit-folium = "^0.22.0"
4343
matplotlib = ">=3.9.0"
4444
seaborn = "^0.13.2"
4545
numpy = "^1.26.4"
46-
pymongo = {extras = ["gssapi", "snappy", "srv", "tls"], version = "^4.15.4"}
46+
pymongo = {extras = ["srv", "tls"], version = "^4.15.4"}
4747
fastapi = "^0.135.2"
4848
uvicorn = "^0.42.0"
4949
httpx = "^0.28.1"
5050
debugpy = "^1.8.20"
5151
cachetools = "^7.0.5"
52+
mangum = "^0.21.0"
5253

5354
[tool.poetry.group.dev.dependencies]
5455
# command: `poetry add --group dev <package-name>`
@@ -66,8 +67,11 @@ build-backend = "poetry.core.masonry.api"
6667
[tool.pytest.ini_options]
6768
testpaths = 'tests'
6869
pythonpath = "."
69-
addopts = '-p no:warnings' # disable pytest warnings
70+
addopts = '-p no:warnings -m "not integration"' # disable pytest warnings; skip integration tests by default
7071
log_format = '%(name)s %(levelname)s: %(message)s'
72+
markers = [
73+
"integration: tests that hit live external services (run with -m integration)",
74+
]
7175

7276
# coverage settings
7377
[tool.coverage.run]

src/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838

3939
def _create_openmeteo_client() -> openmeteo_requests.Client:
4040
"""Creates a cached, retry-enabled Open-Meteo API client."""
41-
cache_session = requests_cache.CachedSession(".cache", expire_after=3600)
41+
cache_session = requests_cache.CachedSession(
42+
"/tmp/.cache", expire_after=3600
43+
)
4244
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
4345
return openmeteo_requests.Client(session=retry_session)
4446

src/handler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from mangum import Mangum
2+
3+
from src.server import app
4+
5+
handler = Mangum(app)

terraform/.terraform.lock.hcl

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)