From 4ede0a5ab42c4288ffc8d868ee42857e0281a7fc Mon Sep 17 00:00:00 2001 From: emileten Date: Wed, 21 Feb 2024 17:18:02 +0300 Subject: [PATCH] lint and make lambda functions more configurable --- .../cdk/eoapi_template/pgStacInfra.py | 7 ++---- integration_tests/cdk/eoapi_template/vpc.py | 2 +- lib/database/index.ts | 9 +++---- lib/ingestor-api/index.ts | 25 ++++++++----------- lib/stac-api/index.ts | 9 +++---- lib/tipg-api/index.ts | 9 +++---- lib/titiler-pgstac-api/index.ts | 9 +++---- tox.ini | 4 +-- 8 files changed, 32 insertions(+), 42 deletions(-) diff --git a/integration_tests/cdk/eoapi_template/pgStacInfra.py b/integration_tests/cdk/eoapi_template/pgStacInfra.py index e585ae0..cbb5491 100644 --- a/integration_tests/cdk/eoapi_template/pgStacInfra.py +++ b/integration_tests/cdk/eoapi_template/pgStacInfra.py @@ -1,7 +1,7 @@ from aws_cdk import ( Stack, aws_ec2, - aws_rds, + aws_rds ) from constructs import Construct from eoapi_cdk import ( @@ -39,10 +39,7 @@ def __init__( subnet_type=aws_ec2.SubnetType.PUBLIC, ), allocated_storage=app_config.db_allocated_storage, - instance_type=aws_ec2.InstanceType(app_config.db_instance_type), - bootstrapper_lambda_function_options={ - "allow_public_subnet": True, - } + instance_type=aws_ec2.InstanceType(app_config.db_instance_type) ) pgstac_db.db.connections.allow_default_port_from_any_ipv4() diff --git a/integration_tests/cdk/eoapi_template/vpc.py b/integration_tests/cdk/eoapi_template/vpc.py index 600f189..d17967f 100644 --- a/integration_tests/cdk/eoapi_template/vpc.py +++ b/integration_tests/cdk/eoapi_template/vpc.py @@ -32,7 +32,7 @@ def __init__(self, scope: Construct, app_config: AppConfig, **kwargs) -> None: "CloudWatchEndpoint", service=aws_ec2.InterfaceVpcEndpointAwsService.CLOUDWATCH_LOGS, ) - + self.vpc.add_gateway_endpoint( "S3", service=aws_ec2.GatewayVpcEndpointAwsService.S3 ) diff --git a/lib/database/index.ts b/lib/database/index.ts index b930e32..2e64efe 100644 --- a/lib/database/index.ts +++ b/lib/database/index.ts @@ -60,7 +60,7 @@ export class PgStacDatabase extends Construct { }); const handler = new aws_lambda.Function(this, "lambda", { - // defaults for configurable properties + // defaults runtime: aws_lambda.Runtime.PYTHON_3_11, handler: "handler.handler", memorySize: 128, @@ -70,11 +70,10 @@ export class PgStacDatabase extends Construct { file: "bootstrapper_runtime/Dockerfile", buildArgs: {PGSTAC_VERSION: DEFAULT_PGSTAC_VERSION, PYTHON_VERSION: "3.11"} }), - // overwrites defaults with user-provided configurable properties - ...props.bootstrapperLambdaFunctionOptions, - // Non configurable properties that are going to be overwritten even if provided by the user vpc: hasVpc(this.db) ? this.db.vpc : props.vpc, allowPublicSubnet: true, + // overwrites defaults with user-provided configurable properties, + ...props.bootstrapperLambdaFunctionOptions, }); this.pgstacSecret = new secretsmanager.Secret(this, "bootstrappersecret", { @@ -204,7 +203,7 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps { } /** - * Optional settings for the bootstrapper lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - defined in the construct. */ diff --git a/lib/ingestor-api/index.ts b/lib/ingestor-api/index.ts index c36baad..e39f7ce 100644 --- a/lib/ingestor-api/index.ts +++ b/lib/ingestor-api/index.ts @@ -115,7 +115,7 @@ export class StacIngestor extends Construct { }): lambda.Function { const handler = new lambda.Function(this, "api-handler", { - // defaults for configurable properties + // defaults runtime: lambda.Runtime.PYTHON_3_11, handler: "src.handler.handler", memorySize: 2048, @@ -125,14 +125,13 @@ export class StacIngestor extends Construct { file: "runtime/Dockerfile", buildArgs: { PYTHON_VERSION: '3.11' }, }), - // overwrites defaults with user-provided configurable properties - ...props.lambdaFunctionOptions, - // Non configurable properties that are going to be overwritten even if provided by the user + allowPublicSubnet: true, vpc: props.dbVpc, vpcSubnets: props.subnetSelection, - allowPublicSubnet: true, environment: { DB_SECRET_ARN: props.dbSecret.secretArn, ...props.env }, - role: this.handlerRole + role: this.handlerRole, + // overwrites defaults with user-provided configurable properties + ...props.lambdaFunctionOptions, }); // Allow handler to read DB secret @@ -165,7 +164,7 @@ export class StacIngestor extends Construct { const handler = new lambda.Function(this, "stac-ingestor",{ - // defaults for configurable properties + // defaults runtime: lambda.Runtime.PYTHON_3_11, handler: "src.ingestor.handler", memorySize: 2048, @@ -175,15 +174,13 @@ export class StacIngestor extends Construct { file: "runtime/Dockerfile", buildArgs: { PYTHON_VERSION: '3.11' }, }), - // overwrites defaults with user-provided configurable properties - ...props.lambdaFunctionOptions, - - // Non configurable properties that are going to be overwritten even if provided by the user vpc: props.dbVpc, vpcSubnets: props.subnetSelection, allowPublicSubnet: true, environment: { DB_SECRET_ARN: props.dbSecret.secretArn, ...props.env }, - role: this.handlerRole + role: this.handlerRole, + // overwrites defaults with user-provided configurable properties + ...props.lambdaFunctionOptions, }); // Allow handler to read DB secret @@ -322,14 +319,14 @@ export interface StacIngestorProps { readonly ingestorDomainNameOptions?: apigateway.DomainNameOptions; /** - * Optional settings for the lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - default settings are defined in the construct. */ readonly apiLambdaFunctionOptions?: CustomLambdaFunctionProps; /** - * Optional settings for the lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - default settings are defined in the construct. */ diff --git a/lib/stac-api/index.ts b/lib/stac-api/index.ts index bcfe434..77c4bc2 100644 --- a/lib/stac-api/index.ts +++ b/lib/stac-api/index.ts @@ -23,7 +23,7 @@ export class PgStacApiLambda extends Construct { console.log(props) console.log(props.lambdaFunctionOptions); this.stacApiLambdaFunction = new lambda.Function(this, "lambda", { - // defaults for configurable properties + // defaults runtime: lambda.Runtime.PYTHON_3_11, handler: "src.handler.handler", memorySize: 8192, @@ -33,9 +33,6 @@ export class PgStacApiLambda extends Construct { file: "runtime/Dockerfile", buildArgs: { PYTHON_VERSION: '3.11' }, }), - // overwrites defaults with user-provided configurable properties - ...props.lambdaFunctionOptions, - // Non configurable properties that are going to be overwritten even if provided by the user vpc: props.vpc, vpcSubnets: props.subnetSelection, allowPublicSubnet: true, @@ -45,6 +42,8 @@ export class PgStacApiLambda extends Construct { DB_MAX_CONN_SIZE: "1", ...props.apiEnv, }, + // overwrites defaults with user-provided configurable properties + ...props.lambdaFunctionOptions }); props.dbSecret.grantRead(this.stacApiLambdaFunction); @@ -99,7 +98,7 @@ export interface PgStacApiLambdaProps { readonly stacApiDomainName?: IDomainName; /** - * Optional settings for the lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - defined in the construct. */ diff --git a/lib/tipg-api/index.ts b/lib/tipg-api/index.ts index d50518b..d40a3df 100644 --- a/lib/tipg-api/index.ts +++ b/lib/tipg-api/index.ts @@ -21,7 +21,7 @@ import { super(scope, id); this.tiPgLambdaFunction = new lambda.Function(this, "lambda", { - // defaults for configurable properties + // defaults runtime: lambda.Runtime.PYTHON_3_11, handler: "handler.handler", memorySize: 1024, @@ -31,9 +31,6 @@ import { file: "runtime/Dockerfile", buildArgs: { PYTHON_VERSION: '3.11' }, }), - // overwrites defaults with user-provided configurable properties - ...props.lambdaFunctionOptions, - // Non configurable properties that are going to be overwritten even if provided by the user vpc: props.vpc, vpcSubnets: props.subnetSelection, allowPublicSubnet: true, @@ -43,6 +40,8 @@ import { DB_MAX_CONN_SIZE: "1", ...props.apiEnv, }, + // overwrites defaults with user-provided configurable properties + ...props.lambdaFunctionOptions }); props.dbSecret.grantRead(this.tiPgLambdaFunction); @@ -103,7 +102,7 @@ import { /** - * Optional settings for the lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - defined in the construct. */ diff --git a/lib/titiler-pgstac-api/index.ts b/lib/titiler-pgstac-api/index.ts index 7ac8fe9..726744d 100644 --- a/lib/titiler-pgstac-api/index.ts +++ b/lib/titiler-pgstac-api/index.ts @@ -39,7 +39,7 @@ import { CustomLambdaFunctionProps } from "../utils"; super(scope, id); this.titilerPgstacLambdaFunction = new lambda.Function(this, "lambda", { - // defaults for configurable properties + // defaults runtime: lambda.Runtime.PYTHON_3_11, handler: "handler.handler", memorySize: 3008, @@ -49,14 +49,13 @@ import { CustomLambdaFunctionProps } from "../utils"; file: "runtime/Dockerfile", buildArgs: { PYTHON_VERSION: '3.11' } }), - // overwrites defaults with user-provided configurable properties - ...props.lambdaFunctionOptions, - // Non configurable properties that are going to be overwritten even if provided by the user vpc: props.vpc, vpcSubnets: props.subnetSelection, allowPublicSubnet: true, // if user provided environment variables, merge them with the defaults. environment: props.apiEnv ? { ...defaultTitilerPgstacEnv, ...props.apiEnv, "PGSTAC_SECRET_ARN": props.dbSecret.secretArn } : defaultTitilerPgstacEnv, + // overwrites defaults with user-provided configurable properties + ...props.lambdaFunctionOptions, }); // grant access to buckets using addToRolePolicy @@ -132,7 +131,7 @@ import { CustomLambdaFunctionProps } from "../utils"; readonly titilerPgstacApiDomainName?: IDomainName; /** - * Optional settings for the lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here. + * Can be used to override the default lambda function properties. * * @default - defined in the construct. */ diff --git a/tox.ini b/tox.ini index 3b42230..79db331 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ exclude = __pycache__ .git .tox - venv* + *venv* toxenv* devenv* cdk.out @@ -38,7 +38,7 @@ exclude = __pycache__ .git .tox - venv* + *venv* toxenv* devenv* cdk.out