diff --git a/infrastructure/DEPLOYMENT.md b/infrastructure/DEPLOYMENT.md index b5a5418..bf257a0 100644 --- a/infrastructure/DEPLOYMENT.md +++ b/infrastructure/DEPLOYMENT.md @@ -26,37 +26,54 @@ The example commands here will deploy a CloudFormation stack called `eoAPI-stagi npm --prefix infrastructure/aws install ``` -3. Install CDK and connect to your AWS account. This step is only necessary once per AWS account. The environment variable `CDK_EOAPI_STAGE` determines the name of the stack +3. Update settings + + Set environment variable or hard code in `infrastructure/aws/.env` file (e.g `CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1`). + + **Important**: + + - `CDK_EOAPI_DB_PGSTAC_VERSION` is a required env (see https://github.com/stac-utils/pgstac/tags for the latest version) + + - You can choose which functions to deploy by setting `CDK_EOAPI_FUNCTIONS` env (e.g `CDK_EOAPI_FUNCTIONS='["stac","raster","vector"]'`) + + +4. Install CDK and connect to your AWS account. This step is only necessary once per AWS account. The environment variable `CDK_EOAPI_STAGE` determines the name of the stack (e.g. eoAPI-staging or eoAPI-production) ```bash # Deploy the CDK toolkit stack into an AWS environment. - CDK_EOAPI_STAGE=staging CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 npm --prefix infrastructure/aws run cdk -- bootstrap + CDK_EOAPI_STAGE=staging \ + CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 \ + npm --prefix infrastructure/aws run cdk -- bootstrap # or to a specific region - AWS_DEFAULT_REGION=us-west-2 AWS_REGION=us-west-2 CDK_EOAPI_STAGE=staging CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 npm --prefix infrastructure/aws run cdk -- bootstrap + AWS_DEFAULT_REGION=us-west-2 \ + AWS_REGION=us-west-2 \ + CDK_EOAPI_STAGE=staging \ + CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 \ + npm --prefix infrastructure/aws run cdk -- bootstrap ``` -4. Update settings - - Set environment variable or hard code in `infrastructure/aws/.env` file (e.g `CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1`). - - **Important**: - - `CDK_EOAPI_DB_PGSTAC_VERSION` is a required env - - You can choose which functions to deploy by setting `CDK_EOAPI_FUNCTIONS` env (e.g `CDK_EOAPI_FUNCTIONS='["stac","raster","vector"]'`) - 5. Pre-Generate CFN template ```bash - CDK_EOAPI_STAGE=staging CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 npm --prefix infrastructure/aws run cdk -- synth # Synthesizes and prints the CloudFormation template for this stack + CDK_EOAPI_STAGE=staging \ + CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 \ + npm --prefix infrastructure/aws run cdk -- synth # Synthesizes and prints the CloudFormation template for this stack ``` 6. Deploy ```bash - CDK_EOAPI_STAGE=staging CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 npm --prefix infrastructure/aws run cdk -- deploy eoAPI-${CDK_EOAPI_STAGE} + CDK_EOAPI_STAGE=staging \ + CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 \ + npm --prefix infrastructure/aws run cdk -- deploy eoAPI-staging # Deploy in specific region - AWS_DEFAULT_REGION=eu-central-1 AWS_REGION=eu-central-1 CDK_EOAPI_STAGE=staging CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 npm --prefix infrastructure/aws run cdk -- deploy eoapi-${CDK_EOAPI_STAGE} --profile {my-aws-profile} + AWS_DEFAULT_REGION=eu-central-1 \ + AWS_REGION=eu-central-1 \ + CDK_EOAPI_STAGE=staging \ + CDK_EOAPI_DB_PGSTAC_VERSION=0.7.1 \ + npm --prefix infrastructure/aws run cdk -- deploy eoapi-staging --profile {my-aws-profile} ``` If you get an error saying that the max VPC's has been reached, this means that you have hit the limit for the amount of VPCs per unique AWS account and region combination. You can change the AWS region to a region that has less VPCs and deploy again to fix this. diff --git a/infrastructure/aws/cdk/config.py b/infrastructure/aws/cdk/config.py index 48b4a1a..8da73ba 100644 --- a/infrastructure/aws/cdk/config.py +++ b/infrastructure/aws/cdk/config.py @@ -110,7 +110,7 @@ class eoVectorSettings(pydantic.BaseSettings): env: Dict = {} timeout: int = 10 - memory: int = 256 + memory: int = 512 class Config: """model config""" diff --git a/infrastructure/aws/handlers/vector_handler.py b/infrastructure/aws/handlers/vector_handler.py index ea91abd..2c3d6fd 100644 --- a/infrastructure/aws/handlers/vector_handler.py +++ b/infrastructure/aws/handlers/vector_handler.py @@ -6,10 +6,46 @@ from eoapi.vector.app import app from mangum import Mangum +from tipg.db import connect_to_db, register_collection_catalog +from tipg.settings import PostgresSettings logging.getLogger("mangum.lifespan").setLevel(logging.ERROR) logging.getLogger("mangum.http").setLevel(logging.ERROR) +postgres_settings = PostgresSettings() + +try: + from importlib.resources import files as resources_files # type: ignore +except ImportError: + # Try backported to PY<39 `importlib_resources`. + from importlib_resources import files as resources_files # type: ignore + + +CUSTOM_SQL_DIRECTORY = resources_files("eoapi.vector") / "sql" +sql_files = list(CUSTOM_SQL_DIRECTORY.glob("*.sql")) # type: ignore + + +@app.on_event("startup") +async def startup_event() -> None: + """Connect to database on startup.""" + await connect_to_db( + app, + settings=postgres_settings, + # We enable both pgstac and public schemas (pgstac will be used by custom functions) + schemas=["pgstac", "public"], + user_sql_files=sql_files, + ) + await register_collection_catalog( + app, + # For the Tables' Catalog we only use the `public` schema + schemas=["public"], + # We exclude public functions + exclude_function_schemas=["public"], + # We allow non-spatial tables + spatial=False, + ) + + handler = Mangum(app, lifespan="off") if "AWS_EXECUTION_ENV" in os.environ: