Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/database/bootstrapper_runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
RUN echo "PYTHON_VERSION: ${PYTHON_VERSION}"

WORKDIR /tmp

RUN pip install httpx psycopg[binary,pool] pypgstac -t /asset
ARG PGSTAC_VERSION
RUN pip install httpx psycopg[binary,pool] pypgstac==${PGSTAC_VERSION} -t /asset

COPY bootstrapper_runtime/handler.py /asset/handler.py

Expand Down
8 changes: 4 additions & 4 deletions lib/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CustomLambdaFunctionProps } from "../utils";
import { PgBouncer } from "./PgBouncer";

const instanceSizes: Record<string, number> = require("./instance-memory.json");
const DEFAULT_PGSTAC_VERSION = "0.8.5";
const DEFAULT_PGSTAC_VERSION = "0.9.5";

let defaultPgSTACCustomOptions: { [key: string]: any } = {
context: "FALSE",
Expand Down Expand Up @@ -69,7 +69,7 @@ export class PgStacDatabase extends Construct {
parameterGroup,
...props,
});

const pgstac_version = props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
const handler = new aws_lambda.Function(this, "lambda", {
// defaults
runtime: aws_lambda.Runtime.PYTHON_3_11,
Expand All @@ -81,6 +81,7 @@ export class PgStacDatabase extends Construct {
file: "bootstrapper_runtime/Dockerfile",
buildArgs: {
PYTHON_VERSION: "3.11",
PGSTAC_VERSION: pgstac_version,
},
}),
vpc: hasVpc(this.db) ? this.db.vpc : props.vpc,
Expand Down Expand Up @@ -131,8 +132,7 @@ export class PgStacDatabase extends Construct {

// if props.lambdaFunctionOptions doesn't have 'code' defined, update pgstac_version (needed for default runtime)
if (!props.bootstrapperLambdaFunctionOptions?.code) {
customResourceProperties["pgstac_version"] =
props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
customResourceProperties["pgstac_version"] = pgstac_version;
}

// add timestamp to properties to ensure the Lambda gets re-executed on each deploy
Expand Down
7 changes: 4 additions & 3 deletions lib/stac-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aw
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { Construct } from "constructs";
import { CustomLambdaFunctionProps } from "../utils";
import * as path from 'path';

export class PgStacApiLambda extends Construct {
readonly url: string;
Expand All @@ -23,12 +24,12 @@ export class PgStacApiLambda extends Construct {
this.stacApiLambdaFunction = new lambda.Function(this, "lambda", {
// defaults
runtime: lambda.Runtime.PYTHON_3_11,
handler: "src.handler.handler",
handler: "handler.handler",
memorySize: 8192,
logRetention: aws_logs.RetentionDays.ONE_WEEK,
timeout: Duration.seconds(30),
code: lambda.Code.fromDockerBuild(__dirname, {
file: "runtime/Dockerfile",
code: lambda.Code.fromDockerBuild(path.join(__dirname, '..'), {
file: "stac-api/runtime/Dockerfile",
buildArgs: { PYTHON_VERSION: '3.11' },
}),
vpc: props.vpc,
Expand Down
5 changes: 3 additions & 2 deletions lib/stac-api/runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
WORKDIR /tmp
RUN python -m pip install pip -U

COPY runtime/requirements.txt requirements.txt
COPY stac-api/runtime/requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt "mangum>=0.14,<0.15" -t /asset --no-binary pydantic

RUN mkdir -p /asset/src
COPY runtime/src/*.py /asset/src/
COPY stac-api/runtime/src/*.py /asset/
COPY utils/utils.py /asset/

CMD ["echo", "hello world"]
9 changes: 2 additions & 7 deletions lib/stac-api/runtime/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
stac-fastapi.api==2.4.8
stac-fastapi.extensions==2.4.8
stac-fastapi.pgstac==2.4.8
stac-fastapi.types==2.4.8
# https://github.com/stac-utils/stac-fastapi/pull/466
pygeoif==0.7
starlette_cramjam
stac-fastapi-pgstac>=5.0,<5.1
starlette-cramjam>=0.4,<0.5
58 changes: 0 additions & 58 deletions lib/stac-api/runtime/src/app.py

This file was deleted.

97 changes: 0 additions & 97 deletions lib/stac-api/runtime/src/config.py

This file was deleted.

31 changes: 30 additions & 1 deletion lib/stac-api/runtime/src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,37 @@
import os

from mangum import Mangum
from stac_fastapi.pgstac.app import app
from stac_fastapi.pgstac.config import PostgresSettings
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db
from utils import get_secret_dict

secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
postgres_settings = PostgresSettings(
postgres_host_reader=secret["host"],
postgres_host_writer=secret["host"],
postgres_dbname=secret["dbname"],
postgres_user=secret["username"],
postgres_pass=secret["password"],
postgres_port=int(secret["port"]),
)


@app.on_event("startup")
async def startup_event():
"""Connect to database on startup."""
print("Setting up DB connection...")
await connect_to_db(app, postgres_settings=postgres_settings)
print("DB connection setup.")


@app.on_event("shutdown")
async def shutdown_event():
"""Close database connection."""
print("Closing up DB connection...")
await close_db_connection(app)
print("DB connection closed.")

from .app import app

handler = Mangum(app, lifespan="off")

Expand Down
33 changes: 17 additions & 16 deletions lib/tipg-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {
Stack,
aws_ec2 as ec2,
aws_lambda as lambda,
aws_logs as logs,
aws_rds as rds,
aws_secretsmanager as secretsmanager,
CfnOutput,
Duration,
} from "aws-cdk-lib";
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { Construct } from "constructs";
import { CustomLambdaFunctionProps } from "../utils";
Stack,
aws_ec2 as ec2,
aws_lambda as lambda,
aws_logs as logs,
aws_rds as rds,
aws_secretsmanager as secretsmanager,
CfnOutput,
Duration,
} from "aws-cdk-lib";
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
import { Construct } from "constructs";
import { CustomLambdaFunctionProps } from "../utils";
import * as path from 'path';

export class TiPgApiLambda extends Construct {
readonly url: string;
Expand All @@ -27,9 +28,9 @@ import {
memorySize: 1024,
logRetention: logs.RetentionDays.ONE_WEEK,
timeout: Duration.seconds(30),
code: lambda.Code.fromDockerBuild(__dirname, {
file: "runtime/Dockerfile",
buildArgs: { PYTHON_VERSION: '3.11' },
code: lambda.Code.fromDockerBuild(path.join(__dirname, '..'), {
file: "tipg-api/runtime/Dockerfile",
buildArgs: { PYTHON_VERSION: '3.11' },
}),
vpc: props.vpc,
vpcSubnets: props.subnetSelection,
Expand Down
5 changes: 3 additions & 2 deletions lib/tipg-api/runtime/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
WORKDIR /tmp
RUN python -m pip install pip -U

COPY runtime/requirements.txt requirements.txt
COPY tipg-api/runtime/requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt "mangum>=0.14,<0.15" -t /asset --no-binary pydantic

# Reduce package size and remove useless files
Expand All @@ -13,6 +13,7 @@ RUN cd /asset && find . -type d -a -name '__pycache__' -print0 | xargs -0 rm -rf
RUN cd /asset && find . -type f -a -name '*.py' -print0 | xargs -0 rm -f
RUN find /asset -type d -a -name 'tests' -print0 | xargs -0 rm -rf

COPY runtime/src/*.py /asset/
COPY tipg-api/runtime/src/*.py /asset/
COPY utils/utils.py /asset/

CMD ["echo", "hello world"]
2 changes: 1 addition & 1 deletion lib/tipg-api/runtime/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tipg==0.6.3
tipg>=1.0,<1.1
13 changes: 11 additions & 2 deletions lib/tipg-api/runtime/src/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
import os

from mangum import Mangum
from utils import load_pgstac_secret
from utils import get_secret_dict

load_pgstac_secret(os.environ["PGSTAC_SECRET_ARN"]) # required for the below imports
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
os.environ.update(
{
"postgres_host": secret["host"],
"postgres_dbname": secret["dbname"],
"postgres_user": secret["username"],
"postgres_pass": secret["password"],
"postgres_port": str(secret["port"]),
}
)

from tipg.collections import register_collection_catalog # noqa: E402
from tipg.database import connect_to_db # noqa: E402
Expand Down
Loading
Loading