Skip to content

Commit

Permalink
Merge pull request #103 from developmentseed/fixVectorLifespan
Browse files Browse the repository at this point in the history
fix lifespan config for eoapi.vector and update docs
  • Loading branch information
vincentsarago authored Jun 22, 2023
2 parents 0798309 + c2d13ae commit 9d0b226
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
45 changes: 31 additions & 14 deletions infrastructure/DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion infrastructure/aws/cdk/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class eoVectorSettings(pydantic.BaseSettings):
env: Dict = {}

timeout: int = 10
memory: int = 256
memory: int = 512

class Config:
"""model config"""
Expand Down
36 changes: 36 additions & 0 deletions infrastructure/aws/handlers/vector_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 9d0b226

Please sign in to comment.