@@ -8,51 +8,61 @@ import {
88 CfnOutput ,
99 Duration ,
1010 aws_logs ,
11+ BundlingOptions
1112 } from "aws-cdk-lib" ;
13+ import { Runtime } from 'aws-cdk-lib/aws-lambda' ;
14+ import { PythonFunction } from "@aws-cdk/aws-lambda-python-alpha" ;
1215 import { IDomainName , HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha" ;
1316 import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha" ;
1417 import { Construct } from "constructs" ;
1518
19+
20+ // default settings that can be overridden by the user-provided environment.
21+ let defaultTitilerPgstacEnv :{ [ key : string ] : any } = {
22+ "CPL_VSIL_CURL_ALLOWED_EXTENSIONS" : ".tif,.TIF,.tiff" ,
23+ "GDAL_CACHEMAX" : "200" ,
24+ "GDAL_DISABLE_READDIR_ON_OPEN" : "EMPTY_DIR" ,
25+ "GDAL_INGESTED_BYTES_AT_OPEN" : "32768" ,
26+ "GDAL_HTTP_MERGE_CONSECUTIVE_RANGES" : "YES" ,
27+ "GDAL_HTTP_MULTIPLEX" : "YES" ,
28+ "GDAL_HTTP_VERSION" : "2" ,
29+ "PYTHONWARNINGS" : "ignore" ,
30+ "VSI_CACHE" : "TRUE" ,
31+ "VSI_CACHE_SIZE" : "5000000" ,
32+ "DB_MIN_CONN_SIZE" : "1" ,
33+ "DB_MAX_CONN_SIZE" : "1"
34+ }
35+
1636 export class TitilerPgstacApiLambda extends Construct {
1737 readonly url : string ;
1838 public titilerPgstacLambdaFunction : lambda . Function ;
1939
2040 constructor ( scope : Construct , id : string , props : TitilerPgStacApiLambdaProps ) {
2141 super ( scope , id ) ;
22-
23- const titilerPgstacEnv = {
24- "CPL_VSIL_CURL_ALLOWED_EXTENSIONS" : ".tif,.TIF,.tiff" ,
25- "GDAL_CACHEMAX" : "200" ,
26- "GDAL_DISABLE_READDIR_ON_OPEN" : "EMPTY_DIR" ,
27- "GDAL_INGESTED_BYTES_AT_OPEN" : "32768" ,
28- "GDAL_HTTP_MERGE_CONSECUTIVE_RANGES" : "YES" ,
29- "GDAL_HTTP_MULTIPLEX" : "YES" ,
30- "GDAL_HTTP_VERSION" : "2" ,
31- "PYTHONWARNINGS" : "ignore" ,
32- "VSI_CACHE" : "TRUE" ,
33- "VSI_CACHE_SIZE" : "5000000" ,
34- "DB_MIN_CONN_SIZE" : "1" ,
35- "DB_MAX_CONN_SIZE" : "1" ,
36- "PGSTAC_SECRET_ARN" : props . dbSecret . secretArn ,
37- }
38-
39-
40- this . titilerPgstacLambdaFunction = new lambda . Function ( this , "lambda" , {
41- handler : "handler.handler" ,
42+
43+
44+ // if user provided environment variables, merge them with the defaults.
45+ const apiEnv = props . apiEnv ? { ...defaultTitilerPgstacEnv , ...props . apiEnv , "PGSTAC_SECRET_ARN" : props . dbSecret . secretArn } : defaultTitilerPgstacEnv ;
46+
47+ const pythonLambdaOptions : TitilerPgstacPythonLambdaOptions = props . pythonLambdaOptions ?? {
4248 runtime : lambda . Runtime . PYTHON_3_10 ,
43- code : lambda . Code . fromDockerBuild ( __dirname , {
44- file : "runtime/Dockerfile" ,
45- buildArgs : { PYTHON_VERSION : '3.10' } ,
46- } ) ,
47- timeout : Duration . seconds ( 30 ) ,
49+ entry : `${ __dirname } /runtime` ,
50+ index : "src/handler.py" ,
51+ handler : "handler" ,
52+ memorySize : 3008 ,
53+ architecture : lambda . Architecture . X86_64
54+ }
55+
56+ this . titilerPgstacLambdaFunction = new PythonFunction ( this , "titiler-pgstac-api" , {
57+ ...pythonLambdaOptions ,
58+ environment : apiEnv ,
4859 vpc : props . vpc ,
4960 vpcSubnets : props . subnetSelection ,
5061 allowPublicSubnet : true ,
51- memorySize : 3008 ,
5262 logRetention : aws_logs . RetentionDays . ONE_WEEK ,
53- environment : titilerPgstacEnv ,
54- } ) ;
55-
63+ timeout : Duration . seconds ( 30 )
64+ } )
65+
5666 // grant access to buckets using addToRolePolicy
5767 if ( props . buckets ) {
5868 props . buckets . forEach ( bucket => {
@@ -105,8 +115,9 @@ import {
105115 readonly dbSecret : secretsmanager . ISecret ;
106116
107117 /**
108- * Customized environment variables to send to titiler-pgstac runtime.
109- */
118+ * Customized environment variables to send to titiler-pgstac runtime. These will be merged with `defaultTitilerPgstacEnv`.
119+ * The database secret arn is automatically added to the environment variables at deployment.
120+ /*/
110121 readonly apiEnv ?: Record < string , string > ;
111122
112123 /**
@@ -116,6 +127,57 @@ import {
116127
117128 /**
118129 * Custom Domain Name Options for Titiler Pgstac API,
130+ *
131+ * @default - undefined.
119132 */
120133 readonly titilerPgstacApiDomainName ?: IDomainName ;
134+
135+ /**
136+ * Optional settings for the titiler-pgstac python lambda function.
137+ *
138+ * @default - defined in the construct.
139+ */
140+ readonly pythonLambdaOptions ?: TitilerPgstacPythonLambdaOptions ;
141+
142+ }
143+
144+
145+ export interface TitilerPgstacPythonLambdaOptions {
146+
147+ /**
148+ * Path to the source of the function or the location for dependencies.
149+ */
150+ readonly entry : string ;
151+ /**
152+ * The runtime environment. Only runtimes of the Python family are
153+ * supported.
154+ */
155+ readonly runtime : Runtime ;
156+
157+ /**
158+ * The path (relative to entry) to the index file containing the exported handler.
159+ *
160+ */
161+ readonly index : string ;
162+ /**
163+ * The name of the exported handler in the index file.
164+ */
165+ readonly handler : string ;
166+
167+ /**
168+ * Bundling options to use for this function. Use this to specify custom bundling options like
169+ * the bundling Docker image, asset hash type, custom hash, architecture, etc.
170+ */
171+ readonly bundling ?: BundlingOptions ;
172+
173+ /**
174+ * The amount of memory, in MB, that is allocated to your Lambda function.
175+ */
176+ readonly memorySize : number ;
177+
178+ /**
179+ * The system architectures compatible with this lambda function.
180+ */
181+ readonly architecture : lambda . Architecture ;
182+
121183 }
0 commit comments