diff --git a/lib/bootstrapper/index.ts b/lib/bootstrapper/index.ts index 958b837..29fb1a7 100644 --- a/lib/bootstrapper/index.ts +++ b/lib/bootstrapper/index.ts @@ -12,6 +12,8 @@ import { import { Construct } from "constructs"; import { CustomLambdaFunctionOptions } from "../utils"; +const DEFAULT_PGSTAC_VERSION = "0.6.13"; + function hasVpc( instance: aws_rds.DatabaseInstance | aws_rds.IDatabaseInstance ): instance is aws_rds.DatabaseInstance { @@ -27,6 +29,15 @@ export class BootstrapPgStac extends Construct { constructor(scope: Construct, id: string, props: BootstrapPgStacProps) { super(scope, id); + // if `lambdaAssetCode` is provided, `pgstacVersion` must be provided, otherwise throw an error + if (props.lambdaAssetCode && !props.pgstacVersion) { + throw new Error( + "If `lambdaAssetCode` is provided, `pgstacVersion` must be provided as well." + ); + } + + const pgstacVersion = props.pgstacVersion ?? DEFAULT_PGSTAC_VERSION; + const handler = new aws_lambda.Function(this, "lambda", { ...props.lambdaFunctionOptions ?? { runtime: aws_lambda.Runtime.PYTHON_3_8, @@ -37,6 +48,7 @@ export class BootstrapPgStac extends Construct { }, code: props.lambdaAssetCode ?? aws_lambda.Code.fromDockerBuild(__dirname, { file: "runtime/Dockerfile", + buildArgs: {PGSTAC_VERSION: pgstacVersion, PYTHON_VERSION: "3.8"} }), vpc: hasVpc(props.database) ? props.database.vpc : props.vpc, }); @@ -75,6 +87,7 @@ export class BootstrapPgStac extends Construct { new CustomResource(this, "bootstrapper", { serviceToken: handler.functionArn, properties: { + pgstac_version: pgstacVersion, conn_secret_arn: props.dbSecret.secretArn, new_user_secret_arn: this.secret.secretArn, }, @@ -142,4 +155,13 @@ export interface BootstrapPgStacProps { * @default default runtime defined in this repository */ readonly lambdaAssetCode?: aws_lambda.AssetCode; + + /** + * pgSTAC database version to be installed. If a custom `lambdaAssetCode` + * is provided, must be provided and match the version of pgSTAC installed + * in the lambda function. + * + * @default 0.6.13 + */ + readonly pgstacVersion?: string; } diff --git a/lib/bootstrapper/runtime/Dockerfile b/lib/bootstrapper/runtime/Dockerfile index f9e7ace..d8d01de 100644 --- a/lib/bootstrapper/runtime/Dockerfile +++ b/lib/bootstrapper/runtime/Dockerfile @@ -1,8 +1,13 @@ -FROM lambci/lambda:build-python3.8 +ARG PYTHON_VERSION +FROM lambci/lambda:build-python${PYTHON_VERSION} + +ARG PGSTAC_VERSION +RUN echo "PGSTAC_VERSION: ${PGSTAC_VERSION}" +RUN echo "PYTHON_VERSION: ${PYTHON_VERSION}" WORKDIR /tmp -RUN pip install httpx psycopg[binary,pool] pypgstac==0.6.13 -t /asset +RUN pip install httpx psycopg[binary,pool] pypgstac==${PGSTAC_VERSION} -t /asset COPY runtime/handler.py /asset/handler.py diff --git a/lib/ingestor-api/runtime/Dockerfile b/lib/ingestor-api/runtime/Dockerfile index 604f813..2c2b496 100644 --- a/lib/ingestor-api/runtime/Dockerfile +++ b/lib/ingestor-api/runtime/Dockerfile @@ -1,6 +1,10 @@ -FROM public.ecr.aws/lambda/python:3.10 +ARG PYTHON_VERSION +FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} WORKDIR /tmp + +RUN yum install -y git + RUN python -m pip install pip -U COPY runtime/requirements.txt requirements.txt diff --git a/lib/stac-api/index.ts b/lib/stac-api/index.ts index 6bcc1af..2c798df 100644 --- a/lib/stac-api/index.ts +++ b/lib/stac-api/index.ts @@ -22,7 +22,7 @@ export class PgStacApiLambda extends Construct { this.stacApiLambdaFunction = new lambda.Function(this, "lambda", { ...props.lambdaFunctionOptions ?? { - runtime: lambda.Runtime.PYTHON_3_8, + runtime: lambda.Runtime.PYTHON_3_9, handler: "handler.handler", memorySize: 8192, logRetention: aws_logs.RetentionDays.ONE_WEEK, @@ -30,7 +30,7 @@ export class PgStacApiLambda extends Construct { }, code: props.lambdaAssetCode ?? lambda.Code.fromDockerBuild(__dirname, { file: "runtime/Dockerfile", - buildArgs: { PYTHON_VERSION: '3.8' }, + buildArgs: { PYTHON_VERSION: '3.9' }, }), vpc: props.vpc, vpcSubnets: props.subnetSelection, diff --git a/lib/stac-api/runtime/Dockerfile b/lib/stac-api/runtime/Dockerfile index 604f813..746c954 100644 --- a/lib/stac-api/runtime/Dockerfile +++ b/lib/stac-api/runtime/Dockerfile @@ -1,4 +1,5 @@ -FROM public.ecr.aws/lambda/python:3.10 +ARG PYTHON_VERSION +FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} WORKDIR /tmp RUN python -m pip install pip -U diff --git a/lib/tipg-api/runtime/Dockerfile b/lib/tipg-api/runtime/Dockerfile index 2a1ef11..25f5bc2 100644 --- a/lib/tipg-api/runtime/Dockerfile +++ b/lib/tipg-api/runtime/Dockerfile @@ -1,10 +1,10 @@ -FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.10 +ARG PYTHON_VERSION +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 -RUN python -m pip install -r requirements.txt "mangum>=0.14,<0.15" -t /asset --no-binary pydantic +RUN python -m pip install tipg==0.3.1 "mangum>=0.14,<0.15" -t /asset --no-binary pydantic # Reduce package size and remove useless files RUN cd /asset && find . -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//'); cp $f $n; done; diff --git a/lib/tipg-api/runtime/requirements.txt b/lib/tipg-api/runtime/requirements.txt deleted file mode 100644 index fde7382..0000000 --- a/lib/tipg-api/runtime/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -tipg==0.3.1 -mangum==0.15.1 diff --git a/lib/tipg-api/runtime/src/handler.py b/lib/tipg-api/runtime/src/handler.py index 40e358a..cdc480d 100644 --- a/lib/tipg-api/runtime/src/handler.py +++ b/lib/tipg-api/runtime/src/handler.py @@ -5,7 +5,7 @@ import asyncio import os from mangum import Mangum -from src.utils import load_pgstac_secret +from utils import load_pgstac_secret load_pgstac_secret(os.environ["PGSTAC_SECRET_ARN"]) # required for the below imports diff --git a/lib/titiler-pgstac-api/index.ts b/lib/titiler-pgstac-api/index.ts index 7d26ead..190565c 100644 --- a/lib/titiler-pgstac-api/index.ts +++ b/lib/titiler-pgstac-api/index.ts @@ -48,6 +48,7 @@ import { CustomLambdaFunctionOptions } from "../utils"; }, code: props.lambdaAssetCode ?? lambda.Code.fromDockerBuild(__dirname, { file: "runtime/Dockerfile", + buildArgs: { PYTHON_VERSION: '3.10' } }), vpc: props.vpc, vpcSubnets: props.subnetSelection, diff --git a/lib/titiler-pgstac-api/runtime/Dockerfile b/lib/titiler-pgstac-api/runtime/Dockerfile index 1fba5ce..e5cf74e 100644 --- a/lib/titiler-pgstac-api/runtime/Dockerfile +++ b/lib/titiler-pgstac-api/runtime/Dockerfile @@ -1,4 +1,5 @@ -FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.10 +ARG PYTHON_VERSION +FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION} WORKDIR /tmp RUN python -m pip install pip -U diff --git a/lib/titiler-pgstac-api/runtime/src/handler.py b/lib/titiler-pgstac-api/runtime/src/handler.py index 23e8ed2..2b5a987 100644 --- a/lib/titiler-pgstac-api/runtime/src/handler.py +++ b/lib/titiler-pgstac-api/runtime/src/handler.py @@ -5,7 +5,7 @@ import asyncio import os from mangum import Mangum -from src.utils import get_secret_dict +from utils import get_secret_dict pgstac_secret_arn = os.environ["PGSTAC_SECRET_ARN"]