|
| 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 | +} from "aws-cdk-lib"; |
| 9 | +import { |
| 10 | + PythonFunction, |
| 11 | + PythonFunctionProps, |
| 12 | +} from "@aws-cdk/aws-lambda-python-alpha"; |
| 13 | +import { HttpApi } from "@aws-cdk/aws-apigatewayv2-alpha"; |
| 14 | +import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha"; |
| 15 | +import { Construct } from "constructs"; |
| 16 | + |
| 17 | +export class PgStacApiLambda extends Construct { |
| 18 | + constructor(scope: Construct, id: string, props: PgStacApiLambdaProps) { |
| 19 | + super(scope, id); |
| 20 | + |
| 21 | + const apiCode = props.apiCode || { |
| 22 | + entry: `${__dirname}/runtime`, |
| 23 | + index: "src/handler.py", |
| 24 | + handler: "handler", |
| 25 | + }; |
| 26 | + |
| 27 | + const handler = new PythonFunction(this, "stac-api", { |
| 28 | + ...apiCode, |
| 29 | + /** |
| 30 | + * NOTE: Unable to use Py3.9, due to issues with hashes: |
| 31 | + * |
| 32 | + * ERROR: Hashes are required in --require-hashes mode, but they are missing |
| 33 | + * from some requirements. Here is a list of those requirements along with the |
| 34 | + * hashes their downloaded archives actually had. Add lines like these to your |
| 35 | + * requirements files to prevent tampering. (If you did not enable |
| 36 | + * --require-hashes manually, note that it turns on automatically when any |
| 37 | + * package has a hash.) |
| 38 | + * anyio==3.6.1 --hash=sha256:cb29b9c70620506a9a8f87a309591713446953302d7d995344d0d7c6c0c9a7be |
| 39 | + * */ |
| 40 | + runtime: lambda.Runtime.PYTHON_3_8, |
| 41 | + architecture: lambda.Architecture.X86_64, |
| 42 | + environment: { |
| 43 | + PGSTAC_SECRET_ARN: props.dbSecret.secretArn, |
| 44 | + DB_MIN_CONN_SIZE: "0", |
| 45 | + DB_MAX_CONN_SIZE: "1", |
| 46 | + ...props.apiEnv, |
| 47 | + }, |
| 48 | + vpc: props.vpc, |
| 49 | + vpcSubnets: props.subnetSelection, |
| 50 | + allowPublicSubnet: true, |
| 51 | + memorySize: 8192, |
| 52 | + }); |
| 53 | + |
| 54 | + props.dbSecret.grantRead(handler); |
| 55 | + handler.connections.allowTo(props.db, ec2.Port.tcp(5432)); |
| 56 | + |
| 57 | + const stacApi = new HttpApi(this, "api", { |
| 58 | + defaultIntegration: new HttpLambdaIntegration("integration", handler), |
| 59 | + }); |
| 60 | + |
| 61 | + new CfnOutput(this, "stac-api-output", { |
| 62 | + exportName: `${Stack.of(this).stackName}-url`, |
| 63 | + value: stacApi.url!, |
| 64 | + }); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export interface PgStacApiLambdaProps { |
| 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?: ApiEntrypoint; |
| 95 | + |
| 96 | + /** |
| 97 | + * Customized environment variables to send to fastapi-pgstac runtime. |
| 98 | + */ |
| 99 | + readonly apiEnv?: Record<string, string>; |
| 100 | +} |
| 101 | + |
| 102 | +export interface ApiEntrypoint { |
| 103 | + /** |
| 104 | + * Path to the source of the function or the location for dependencies. |
| 105 | + */ |
| 106 | + readonly entry: PythonFunctionProps["entry"]; |
| 107 | + /** |
| 108 | + * The path (relative to entry) to the index file containing the exported handler. |
| 109 | + */ |
| 110 | + readonly index: PythonFunctionProps["index"]; |
| 111 | + /** |
| 112 | + * The name of the exported handler in the index file. |
| 113 | + */ |
| 114 | + readonly handler: PythonFunctionProps["handler"]; |
| 115 | +} |
0 commit comments