|
| 1 | +import { |
| 2 | + Stack, |
| 3 | + aws_ec2 as ec2, |
| 4 | + aws_rds as rds, |
| 5 | + aws_lambda as lambda, |
| 6 | + aws_secretsmanager as secretsmanager, |
| 7 | + CfnOutput, |
| 8 | + Duration, |
| 9 | + } from "aws-cdk-lib"; |
| 10 | + import { |
| 11 | + PythonFunction, |
| 12 | + PythonFunctionProps, |
| 13 | + } from "@aws-cdk/aws-lambda-python-alpha"; |
| 14 | + import { IDomainName, HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; |
| 15 | + import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha"; |
| 16 | + import { Construct } from "constructs"; |
| 17 | + |
| 18 | + export class TiPgApiLambda extends Construct { |
| 19 | + readonly url: string; |
| 20 | + public tiPgLambdaFunction: PythonFunction; |
| 21 | + |
| 22 | + constructor(scope: Construct, id: string, props: TiPgApiLambdaProps) { |
| 23 | + super(scope, id); |
| 24 | + |
| 25 | + const apiCode = props.apiCode || { |
| 26 | + entry: `${__dirname}/runtime`, |
| 27 | + index: "src/handler.py", |
| 28 | + handler: "handler", |
| 29 | + }; |
| 30 | + |
| 31 | + this.tiPgLambdaFunction = new PythonFunction(this, "tipg-api", { |
| 32 | + ...apiCode, |
| 33 | + runtime: lambda.Runtime.PYTHON_3_10, |
| 34 | + architecture: lambda.Architecture.X86_64, |
| 35 | + environment: { |
| 36 | + PGSTAC_SECRET_ARN: props.dbSecret.secretArn, |
| 37 | + DB_MIN_CONN_SIZE: "1", |
| 38 | + DB_MAX_CONN_SIZE: "1", |
| 39 | + ...props.apiEnv, |
| 40 | + }, |
| 41 | + vpc: props.vpc, |
| 42 | + vpcSubnets: props.subnetSelection, |
| 43 | + allowPublicSubnet: true, |
| 44 | + memorySize: 1024, |
| 45 | + timeout: Duration.seconds(30), |
| 46 | + }); |
| 47 | + |
| 48 | + props.dbSecret.grantRead(this.tiPgLambdaFunction); |
| 49 | + this.tiPgLambdaFunction.connections.allowTo(props.db, ec2.Port.tcp(5432), "allow connections from tipg"); |
| 50 | + |
| 51 | + const tipgApi = new HttpApi(this, `${Stack.of(this).stackName}-tipg-api`, { |
| 52 | + defaultDomainMapping: props.tipgApiDomainName ? { |
| 53 | + domainName: props.tipgApiDomainName |
| 54 | + } : undefined, |
| 55 | + defaultIntegration: new HttpLambdaIntegration("integration", this.tiPgLambdaFunction), |
| 56 | + }); |
| 57 | + |
| 58 | + this.url = tipgApi.url!; |
| 59 | + |
| 60 | + new CfnOutput(this, "tipg-api-output", { |
| 61 | + exportName: `${Stack.of(this).stackName}-tip-url`, |
| 62 | + value: this.url, |
| 63 | + }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + export interface TiPgApiLambdaProps { |
| 68 | + |
| 69 | + /** |
| 70 | + * VPC into which the lambda should be deployed. |
| 71 | + */ |
| 72 | + readonly vpc: ec2.IVpc; |
| 73 | + |
| 74 | + /** |
| 75 | + * RDS Instance with installed pgSTAC. |
| 76 | + */ |
| 77 | + readonly db: rds.IDatabaseInstance; |
| 78 | + |
| 79 | + /** |
| 80 | + * Subnet into which the lambda should be deployed. |
| 81 | + */ |
| 82 | + readonly subnetSelection: ec2.SubnetSelection; |
| 83 | + |
| 84 | + /** |
| 85 | + * Secret containing connection information for pgSTAC database. |
| 86 | + */ |
| 87 | + readonly dbSecret: secretsmanager.ISecret; |
| 88 | + |
| 89 | + /** |
| 90 | + * Custom code to run for fastapi-pgstac. |
| 91 | + * |
| 92 | + * @default - simplified version of fastapi-pgstac |
| 93 | + */ |
| 94 | + readonly apiCode?: TiPgApiEntrypoint; |
| 95 | + |
| 96 | + /** |
| 97 | + * Customized environment variables to send to titiler-pgstac runtime. |
| 98 | + */ |
| 99 | + readonly apiEnv?: Record<string, string>; |
| 100 | + |
| 101 | + /** |
| 102 | + * Custom Domain Name for tipg API. If defined, will create the |
| 103 | + * domain name and integrate it with the tipg API. |
| 104 | + * |
| 105 | + * @default - undefined |
| 106 | + */ |
| 107 | + readonly tipgApiDomainName?: IDomainName; |
| 108 | + } |
| 109 | + |
| 110 | + export interface TiPgApiEntrypoint { |
| 111 | + /** |
| 112 | + * Path to the source of the function or the location for dependencies. |
| 113 | + */ |
| 114 | + readonly entry: PythonFunctionProps["entry"]; |
| 115 | + /** |
| 116 | + * The path (relative to entry) to the index file containing the exported handler. |
| 117 | + */ |
| 118 | + readonly index: PythonFunctionProps["index"]; |
| 119 | + /** |
| 120 | + * The name of the exported handler in the index file. |
| 121 | + */ |
| 122 | + readonly handler: PythonFunctionProps["handler"]; |
| 123 | + } |
0 commit comments