Skip to content

Commit

Permalink
lint and make lambda functions more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
emileten committed Feb 21, 2024
1 parent 97b5a32 commit 4ede0a5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 42 deletions.
7 changes: 2 additions & 5 deletions integration_tests/cdk/eoapi_template/pgStacInfra.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from aws_cdk import (
Stack,
aws_ec2,
aws_rds,
aws_rds
)
from constructs import Construct
from eoapi_cdk import (
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/cdk/eoapi_template/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
9 changes: 4 additions & 5 deletions lib/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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", {
Expand Down Expand Up @@ -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.
*/
Expand Down
25 changes: 11 additions & 14 deletions lib/ingestor-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 4 additions & 5 deletions lib/stac-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 4 additions & 5 deletions lib/tipg-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 4 additions & 5 deletions lib/titiler-pgstac-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exclude =
__pycache__
.git
.tox
venv*
*venv*
toxenv*
devenv*
cdk.out
Expand All @@ -38,7 +38,7 @@ exclude =
__pycache__
.git
.tox
venv*
*venv*
toxenv*
devenv*
cdk.out
Expand Down

0 comments on commit 4ede0a5

Please sign in to comment.