Skip to content

Commit

Permalink
fix: ensure db bootstrapper runs on each deploy (#124)
Browse files Browse the repository at this point in the history
* fix: ensure db bootstrapper runs on each deploy

fix: advertise secretBootstrapper so other resources can use it as a
dependency

* set upper version for pydantic_ssm_settings

---------

Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
hrodmn and vincentsarago authored Feb 6, 2025
1 parent 94f7c02 commit bb4bff8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
13 changes: 9 additions & 4 deletions integration_tests/cdk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,33 @@ def __init__(
instance_type=aws_ec2.InstanceType(app_config.db_instance_type),
add_pgbouncer=True,
removal_policy=RemovalPolicy.DESTROY,
pgstac_version="0.9.2",
)

assert pgstac_db.security_group

# make sure we can get the secret value!
assert pgstac_db.pgstac_secret.secret_value_from_json("host").to_string()

pgstac_db.security_group.add_ingress_rule(
aws_ec2.Peer.any_ipv4(), aws_ec2.Port.tcp(5432)
)

PgStacApiLambda(
stac_api = PgStacApiLambda(
self,
"pgstac-api",
db=pgstac_db.connection_target,
db_secret=pgstac_db.pgstac_secret,
api_env={
"NAME": app_config.build_service_name("STAC API"),
"description": f"{app_config.stage} STAC API",
# test that we can use the pgbouncer secret in downstream resources
"POSTGRES_HOST": pgstac_db.pgstac_secret.secret_value_from_json(
"host"
).to_string(),
},
)

# make sure stac_api does not try to build before the secret has been boostrapped
stac_api.node.add_dependency(pgstac_db.secret_bootstrapper)

TitilerPgstacApiLambda(
self,
"titiler-pgstac-api",
Expand Down
22 changes: 14 additions & 8 deletions lib/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class PgStacDatabase extends Construct {

public readonly connectionTarget: rds.IDatabaseInstance | ec2.Instance;
public readonly securityGroup?: ec2.SecurityGroup;
public readonly secretBootstrapper?: CustomResource;

constructor(scope: Construct, id: string, props: PgStacDatabaseProps) {
super(scope, id);
Expand Down Expand Up @@ -79,7 +80,7 @@ export class PgStacDatabase extends Construct {
code: aws_lambda.Code.fromDockerBuild(__dirname, {
file: "bootstrapper_runtime/Dockerfile",
buildArgs: {
PYTHON_VERSION: "3.11"
PYTHON_VERSION: "3.11",
},
}),
vpc: hasVpc(this.db) ? this.db.vpc : props.vpc,
Expand Down Expand Up @@ -130,16 +131,20 @@ export class PgStacDatabase extends Construct {

// if props.lambdaFunctionOptions doesn't have 'code' defined, update pgstac_version (needed for default runtime)
if (!props.bootstrapperLambdaFunctionOptions?.code) {
customResourceProperties["pgstac_version"] = props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
customResourceProperties["pgstac_version"] =
props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
}
// this.connections = props.database.connections;

// add timestamp to properties to ensure the Lambda gets re-executed on each deploy
customResourceProperties["timestamp"] = new Date().toISOString();

const bootstrapper = new CustomResource(this, "bootstrapper", {
serviceToken: handler.functionArn,
properties: customResourceProperties,
removalPolicy: RemovalPolicy.RETAIN, // This retains the custom resource (which doesn't really exist), not the database
});

// PgBouncer: connection pooler
// PgBouncer: connection poolercustomresource trigger on redeploy
const addPgbouncer = props.addPgbouncer ?? true;
if (addPgbouncer) {
this._pgBouncerServer = new PgBouncer(this, "pgbouncer", {
Expand Down Expand Up @@ -172,6 +177,7 @@ export class PgStacDatabase extends Construct {
this.pgstacSecret = this._pgBouncerServer.pgbouncerSecret;
this.connectionTarget = this._pgBouncerServer.instance;
this.securityGroup = this._pgBouncerServer.securityGroup;
this.secretBootstrapper = this._pgBouncerServer.secretUpdateComplete;
} else {
this.connectionTarget = this.db;
}
Expand Down Expand Up @@ -226,10 +232,10 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
readonly pgstacDbName?: string;

/**
* Version of pgstac to install on the database
*
* @default 0.8.5
*/
* Version of pgstac to install on the database
*
* @default 0.8.5
*/
readonly pgstacVersion?: string;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/ingestor-api/runtime/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cachetools==5.3.0
fastapi>=0.75.1
orjson>=3.6.8
psycopg[binary,pool]>=3.0.15
pydantic_ssm_settings>=0.2.0
pydantic_ssm_settings>=0.2.0,<1.0
pydantic>=1.9.0
pypgstac==0.8.5
requests>=2.27.1
Expand Down

0 comments on commit bb4bff8

Please sign in to comment.