Skip to content

Commit

Permalink
Add custom domain for API
Browse files Browse the repository at this point in the history
Separate settings into deployment and application.
  • Loading branch information
zacdezgeo committed Aug 6, 2024
1 parent 498432e commit 400ac16
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ db.env
*.duckdb
.pgdata
space2stats_api/space2stats_env
aws.env
aws*.env
cdk.out
lambda_layer
137 changes: 48 additions & 89 deletions notebooks/space2stats_api_demo.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions space2stats_api/cdk/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from aws_cdk import App, Environment
from aws_stack import Space2StatsStack
from settings import Settings
from settings import DeploymentSettings


settings = Settings(_env_file="./aws.env")
settings = DeploymentSettings(_env_file="aws_deployment.env")

env = Environment(
account=settings.CDK_DEFAULT_ACCOUNT,
Expand Down
17 changes: 11 additions & 6 deletions space2stats_api/cdk/aws_stack.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from aws_cdk import (
Stack,
aws_apigateway as apigateway,
aws_lambda as _lambda
aws_lambda as _lambda,
aws_certificatemanager as acm
)
from aws_cdk.aws_lambda_python_alpha import PythonFunction
from aws_cdk import Duration
from constructs import Construct
from settings import Settings
from settings import AppSettings, DeploymentSettings

class Space2StatsStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)

settings = Settings(_env_file="./aws.env")
app_settings = AppSettings(_env_file="./aws_app.env")
deployment_settings = DeploymentSettings(_env_file="./aws_deployment.env")

lambda_function = PythonFunction(
self, "Space2StatsFunction",
Expand All @@ -21,11 +23,14 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
index="app/main.py",
timeout=Duration.seconds(120),
handler="handler",
environment=settings.model_dump()
environment=app_settings.model_dump()
)

certificate = acm.Certificate.from_certificate_arn(self, "certificate", deployment_settings.CDK_CERTIFICATE_ARN)

apigateway.LambdaRestApi(
self, "Space2StatsAPI",
self, "Space2Stats",
handler=lambda_function,
proxy=True
proxy=True,
domain_name=apigateway.DomainNameOptions(domain_name=deployment_settings.CDK_DOMAIN_NAME, certificate=certificate)
)
8 changes: 6 additions & 2 deletions space2stats_api/cdk/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
class AppSettings(BaseSettings):
DB_HOST: str
DB_PORT: str
DB_NAME: str
DB_USER: str
DB_PASSWORD: str
DB_TABLE_NAME: str

class DeploymentSettings(BaseSettings):
CDK_DEFAULT_ACCOUNT: str
CDK_DEFAULT_REGION: str
CDK_DEFAULT_REGION: str
CDK_CERTIFICATE_ARN: str
CDK_DOMAIN_NAME: str
11 changes: 9 additions & 2 deletions space2stats_api/src/app/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
from fastapi import FastAPI
from mangum import Mangum
from fastapi.middleware.cors import CORSMiddleware

from .routers import api


app = FastAPI()

app.include_router(api.router)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(api.router)

@app.get("/")
def read_root():
Expand Down

0 comments on commit 400ac16

Please sign in to comment.