From c9eeb00c2d66bf923f3029743e1c9746f7752c5e Mon Sep 17 00:00:00 2001 From: Jamison French <50224594+jjfrench@users.noreply.github.com> Date: Mon, 21 Aug 2023 04:03:09 -0500 Subject: [PATCH] feat: custom domain names for apis (#63) * feat: custom domain names for apis * import the titiler app AFTER setting up the postgres env vars in the titiler-pgstac handler * add the missing ingestorDomainNameOptions parameter when calling the ingestor construct buildApiEndpoint method * fixes a titiler-pgstac handler bug introduced in #61 --------- Co-authored-by: emileten --- lib/ingestor-api/index.ts | 13 +++++++++++++ lib/stac-api/index.ts | 10 +++++++++- lib/titiler-pgstac-api/index.ts | 11 +++++++++-- lib/titiler-pgstac-api/runtime/src/handler.py | 5 +++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/ingestor-api/index.ts b/lib/ingestor-api/index.ts index b3dec43..c2de4d8 100644 --- a/lib/ingestor-api/index.ts +++ b/lib/ingestor-api/index.ts @@ -62,6 +62,7 @@ export class StacIngestor extends Construct { stage: props.stage, endpointConfiguration: props.apiEndpointConfiguration, policy: props.apiPolicy, + ingestorDomainNameOptions: props.ingestorDomainNameOptions, }); this.buildIngestor({ @@ -191,7 +192,9 @@ export class StacIngestor extends Construct { stage: string; policy?: iam.PolicyDocument; endpointConfiguration?: apigateway.EndpointConfiguration; + ingestorDomainNameOptions?: apigateway.DomainNameOptions; }): apigateway.LambdaRestApi { + return new apigateway.LambdaRestApi( this, `${Stack.of(this).stackName}-ingestor-api`, @@ -205,6 +208,11 @@ export class StacIngestor extends Construct { endpointConfiguration: props.endpointConfiguration, policy: props.policy, + + domainName: props.ingestorDomainNameOptions ? { + domainName: props.ingestorDomainNameOptions.domainName, + certificate: props.ingestorDomainNameOptions.certificate, + } : undefined, } ); } @@ -277,4 +285,9 @@ export interface StacIngestorProps { * API Policy Document, useful for creating private APIs. */ readonly apiPolicy?: iam.PolicyDocument; + + /** + * Custom Domain Name Options for Ingestor API + */ + readonly ingestorDomainNameOptions?: apigateway.DomainNameOptions; } diff --git a/lib/stac-api/index.ts b/lib/stac-api/index.ts index b21e870..a6e5212 100644 --- a/lib/stac-api/index.ts +++ b/lib/stac-api/index.ts @@ -10,7 +10,7 @@ import { PythonFunction, PythonFunctionProps, } from "@aws-cdk/aws-lambda-python-alpha"; -import { HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; +import { IDomainName, HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha"; import { Construct } from "constructs"; @@ -58,6 +58,9 @@ export class PgStacApiLambda extends Construct { this.stacApiLambdaFunction.connections.allowTo(props.db, ec2.Port.tcp(5432)); const stacApi = new HttpApi(this, `${Stack.of(this).stackName}-stac-api`, { + defaultDomainMapping: props.stacApiDomainName ? { + domainName: props.stacApiDomainName + } : undefined, defaultIntegration: new HttpLambdaIntegration("integration", this.stacApiLambdaFunction), }); @@ -102,6 +105,11 @@ export interface PgStacApiLambdaProps { * Customized environment variables to send to fastapi-pgstac runtime. */ readonly apiEnv?: Record; + + /** + * Custom Domain Name Options for STAC API, + */ + readonly stacApiDomainName?: IDomainName; } export interface ApiEntrypoint { diff --git a/lib/titiler-pgstac-api/index.ts b/lib/titiler-pgstac-api/index.ts index de242ce..a64cbb3 100644 --- a/lib/titiler-pgstac-api/index.ts +++ b/lib/titiler-pgstac-api/index.ts @@ -9,7 +9,7 @@ import { Duration, aws_logs, } from "aws-cdk-lib"; - import { HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; + import { IDomainName, HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha"; import { Construct } from "constructs"; @@ -67,6 +67,9 @@ import { this.titilerPgstacLambdaFunction.connections.allowTo(props.db, ec2.Port.tcp(5432), "allow connections from titiler"); const stacApi = new HttpApi(this, `${Stack.of(this).stackName}-titiler-pgstac-api`, { + defaultDomainMapping: props.titilerPgstacApiDomainName ? { + domainName: props.titilerPgstacApiDomainName + } : undefined, defaultIntegration: new HttpLambdaIntegration("integration", this.titilerPgstacLambdaFunction), }); @@ -111,4 +114,8 @@ import { */ readonly buckets?: string[]; - } \ No newline at end of file + /** + * Custom Domain Name Options for Titiler Pgstac API, + */ + readonly titilerPgstacApiDomainName?: IDomainName; + } diff --git a/lib/titiler-pgstac-api/runtime/src/handler.py b/lib/titiler-pgstac-api/runtime/src/handler.py index 34cf618..2b5a987 100644 --- a/lib/titiler-pgstac-api/runtime/src/handler.py +++ b/lib/titiler-pgstac-api/runtime/src/handler.py @@ -6,8 +6,6 @@ import os from mangum import Mangum from utils import get_secret_dict -from titiler.pgstac.main import app -from titiler.pgstac.db import connect_to_db pgstac_secret_arn = os.environ["PGSTAC_SECRET_ARN"] @@ -22,6 +20,9 @@ } ) +from titiler.pgstac.main import app # noqa: E402 +from titiler.pgstac.db import connect_to_db # noqa: E402 + @app.on_event("startup") async def startup_event() -> None: