Skip to content

Commit 34448b5

Browse files
committed
no more custom runtimes
1 parent 70d9f53 commit 34448b5

File tree

13 files changed

+86
-245
lines changed

13 files changed

+86
-245
lines changed

lib/stac-api/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aw
1212
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
1313
import { Construct } from "constructs";
1414
import { CustomLambdaFunctionProps } from "../utils";
15+
import * as path from 'path';
1516

1617
export class PgStacApiLambda extends Construct {
1718
readonly url: string;
@@ -23,12 +24,12 @@ export class PgStacApiLambda extends Construct {
2324
this.stacApiLambdaFunction = new lambda.Function(this, "lambda", {
2425
// defaults
2526
runtime: lambda.Runtime.PYTHON_3_11,
26-
handler: "src.handler.handler",
27+
handler: "handler.handler",
2728
memorySize: 8192,
2829
logRetention: aws_logs.RetentionDays.ONE_WEEK,
2930
timeout: Duration.seconds(30),
30-
code: lambda.Code.fromDockerBuild(__dirname, {
31-
file: "runtime/Dockerfile",
31+
code: lambda.Code.fromDockerBuild(path.join(__dirname, '..'), {
32+
file: "stac-api/runtime/Dockerfile",
3233
buildArgs: { PYTHON_VERSION: '3.11' },
3334
}),
3435
vpc: props.vpc,

lib/stac-api/runtime/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
44
WORKDIR /tmp
55
RUN python -m pip install pip -U
66

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

1010
RUN mkdir -p /asset/src
11-
COPY runtime/src/*.py /asset/src/
11+
COPY stac-api/runtime/src/*.py /asset/
12+
COPY utils/utils.py /asset/
1213

1314
CMD ["echo", "hello world"]

lib/stac-api/runtime/src/app.py

-75
This file was deleted.

lib/stac-api/runtime/src/config.py

-64
This file was deleted.

lib/stac-api/runtime/src/handler.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import os
77

88
from mangum import Mangum
9+
from utils import get_secret_dict
910

10-
from .config import get_secret_dict
11+
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
1112

12-
pgstac_secret_arn = os.environ["PGSTAC_SECRET_ARN"]
13-
14-
secret = get_secret_dict(pgstac_secret_arn)
1513
os.environ.update(
1614
{
1715
"postgres_host_reader": secret["host"],
@@ -23,7 +21,25 @@
2321
}
2422
)
2523

26-
from .app import app
24+
from stac_fastapi.pgstac.app import app
25+
from stac_fastapi.pgstac.db import close_db_connection, connect_to_db
26+
27+
28+
@app.on_event("startup")
29+
async def startup_event():
30+
"""Connect to database on startup."""
31+
print("Setting up DB connection...")
32+
await connect_to_db(app)
33+
print("DB connection setup.")
34+
35+
36+
@app.on_event("shutdown")
37+
async def shutdown_event():
38+
"""Close database connection."""
39+
print("Closing up DB connection...")
40+
await close_db_connection(app)
41+
print("DB connection closed.")
42+
2743

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

lib/tipg-api/index.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import {
2-
Stack,
3-
aws_ec2 as ec2,
4-
aws_lambda as lambda,
5-
aws_logs as logs,
6-
aws_rds as rds,
7-
aws_secretsmanager as secretsmanager,
8-
CfnOutput,
9-
Duration,
10-
} from "aws-cdk-lib";
11-
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
12-
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
13-
import { Construct } from "constructs";
14-
import { CustomLambdaFunctionProps } from "../utils";
2+
Stack,
3+
aws_ec2 as ec2,
4+
aws_lambda as lambda,
5+
aws_logs as logs,
6+
aws_rds as rds,
7+
aws_secretsmanager as secretsmanager,
8+
CfnOutput,
9+
Duration,
10+
} from "aws-cdk-lib";
11+
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
12+
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
13+
import { Construct } from "constructs";
14+
import { CustomLambdaFunctionProps } from "../utils";
15+
import * as path from 'path';
1516

1617
export class TiPgApiLambda extends Construct {
1718
readonly url: string;
@@ -27,9 +28,9 @@ import {
2728
memorySize: 1024,
2829
logRetention: logs.RetentionDays.ONE_WEEK,
2930
timeout: Duration.seconds(30),
30-
code: lambda.Code.fromDockerBuild(__dirname, {
31-
file: "runtime/Dockerfile",
32-
buildArgs: { PYTHON_VERSION: '3.11' },
31+
code: lambda.Code.fromDockerBuild(path.join(__dirname, '..'), {
32+
file: "tipg-api/runtime/Dockerfile",
33+
buildArgs: { PYTHON_VERSION: '3.11' },
3334
}),
3435
vpc: props.vpc,
3536
vpcSubnets: props.subnetSelection,

lib/tipg-api/runtime/Dockerfile

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM --platform=linux/amd64 public.ecr.aws/lambda/python:${PYTHON_VERSION}
44
WORKDIR /tmp
55
RUN python -m pip install pip -U
66

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

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

16-
COPY runtime/src/*.py /asset/
16+
COPY tipg-api/runtime/src/*.py /asset/
17+
COPY utils/utils.py /asset/
1718

1819
CMD ["echo", "hello world"]

lib/tipg-api/runtime/src/handler.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@
66
import os
77

88
from mangum import Mangum
9-
from utils import load_pgstac_secret
9+
from utils import get_secret_dict
1010

11-
load_pgstac_secret(os.environ["PGSTAC_SECRET_ARN"]) # required for the below imports
11+
secret = get_secret_dict(secret_arn_env_var="PGSTAC_SECRET_ARN")
12+
os.environ.update(
13+
{
14+
"postgres_host": secret["host"],
15+
"postgres_dbname": secret["dbname"],
16+
"postgres_user": secret["username"],
17+
"postgres_pass": secret["password"],
18+
"postgres_port": str(secret["port"]),
19+
}
20+
)
1221

1322
from tipg.collections import register_collection_catalog # noqa: E402
1423
from tipg.database import connect_to_db # noqa: E402

lib/tipg-api/runtime/src/utils.py

-42
This file was deleted.

lib/titiler-pgstac-api/index.ts

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import {
2-
Stack,
3-
aws_iam as iam,
4-
aws_ec2 as ec2,
5-
aws_rds as rds,
6-
aws_lambda as lambda,
7-
aws_secretsmanager as secretsmanager,
8-
CfnOutput,
9-
Duration,
10-
aws_logs
11-
} from "aws-cdk-lib";
12-
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
13-
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
14-
import { Construct } from "constructs";
2+
Stack,
3+
aws_iam as iam,
4+
aws_ec2 as ec2,
5+
aws_rds as rds,
6+
aws_lambda as lambda,
7+
aws_secretsmanager as secretsmanager,
8+
CfnOutput,
9+
Duration,
10+
aws_logs
11+
} from "aws-cdk-lib";
12+
import { IDomainName, HttpApi, ParameterMapping, MappingValue} from "@aws-cdk/aws-apigatewayv2-alpha";
13+
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha";
14+
import { Construct } from "constructs";
1515
import { CustomLambdaFunctionProps } from "../utils";
16-
16+
import * as path from 'path';
1717

1818
// default settings that can be overridden by the user-provided environment.
1919
let defaultTitilerPgstacEnv :{ [key: string]: any } = {
@@ -45,8 +45,8 @@ import { CustomLambdaFunctionProps } from "../utils";
4545
memorySize: 3008,
4646
logRetention: aws_logs.RetentionDays.ONE_WEEK,
4747
timeout: Duration.seconds(30),
48-
code: lambda.Code.fromDockerBuild(__dirname, {
49-
file: "runtime/Dockerfile",
48+
code: lambda.Code.fromDockerBuild(path.join(__dirname, '..'), {
49+
file: "titiler-pgstac-api/runtime/Dockerfile",
5050
buildArgs: { PYTHON_VERSION: '3.11' }
5151
}),
5252
vpc: props.vpc,

0 commit comments

Comments
 (0)