Skip to content

Commit bb4bff8

Browse files
fix: ensure db bootstrapper runs on each deploy (#124)
* 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]>
1 parent 94f7c02 commit bb4bff8

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

integration_tests/cdk/app.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,33 @@ def __init__(
8181
instance_type=aws_ec2.InstanceType(app_config.db_instance_type),
8282
add_pgbouncer=True,
8383
removal_policy=RemovalPolicy.DESTROY,
84+
pgstac_version="0.9.2",
8485
)
8586

8687
assert pgstac_db.security_group
8788

88-
# make sure we can get the secret value!
89-
assert pgstac_db.pgstac_secret.secret_value_from_json("host").to_string()
90-
9189
pgstac_db.security_group.add_ingress_rule(
9290
aws_ec2.Peer.any_ipv4(), aws_ec2.Port.tcp(5432)
9391
)
9492

95-
PgStacApiLambda(
93+
stac_api = PgStacApiLambda(
9694
self,
9795
"pgstac-api",
9896
db=pgstac_db.connection_target,
9997
db_secret=pgstac_db.pgstac_secret,
10098
api_env={
10199
"NAME": app_config.build_service_name("STAC API"),
102100
"description": f"{app_config.stage} STAC API",
101+
# test that we can use the pgbouncer secret in downstream resources
102+
"POSTGRES_HOST": pgstac_db.pgstac_secret.secret_value_from_json(
103+
"host"
104+
).to_string(),
103105
},
104106
)
105107

108+
# make sure stac_api does not try to build before the secret has been boostrapped
109+
stac_api.node.add_dependency(pgstac_db.secret_bootstrapper)
110+
106111
TitilerPgstacApiLambda(
107112
self,
108113
"titiler-pgstac-api",

lib/database/index.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export class PgStacDatabase extends Construct {
3939

4040
public readonly connectionTarget: rds.IDatabaseInstance | ec2.Instance;
4141
public readonly securityGroup?: ec2.SecurityGroup;
42+
public readonly secretBootstrapper?: CustomResource;
4243

4344
constructor(scope: Construct, id: string, props: PgStacDatabaseProps) {
4445
super(scope, id);
@@ -79,7 +80,7 @@ export class PgStacDatabase extends Construct {
7980
code: aws_lambda.Code.fromDockerBuild(__dirname, {
8081
file: "bootstrapper_runtime/Dockerfile",
8182
buildArgs: {
82-
PYTHON_VERSION: "3.11"
83+
PYTHON_VERSION: "3.11",
8384
},
8485
}),
8586
vpc: hasVpc(this.db) ? this.db.vpc : props.vpc,
@@ -130,16 +131,20 @@ export class PgStacDatabase extends Construct {
130131

131132
// if props.lambdaFunctionOptions doesn't have 'code' defined, update pgstac_version (needed for default runtime)
132133
if (!props.bootstrapperLambdaFunctionOptions?.code) {
133-
customResourceProperties["pgstac_version"] = props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
134+
customResourceProperties["pgstac_version"] =
135+
props.pgstacVersion || DEFAULT_PGSTAC_VERSION;
134136
}
135-
// this.connections = props.database.connections;
137+
138+
// add timestamp to properties to ensure the Lambda gets re-executed on each deploy
139+
customResourceProperties["timestamp"] = new Date().toISOString();
140+
136141
const bootstrapper = new CustomResource(this, "bootstrapper", {
137142
serviceToken: handler.functionArn,
138143
properties: customResourceProperties,
139144
removalPolicy: RemovalPolicy.RETAIN, // This retains the custom resource (which doesn't really exist), not the database
140145
});
141146

142-
// PgBouncer: connection pooler
147+
// PgBouncer: connection poolercustomresource trigger on redeploy
143148
const addPgbouncer = props.addPgbouncer ?? true;
144149
if (addPgbouncer) {
145150
this._pgBouncerServer = new PgBouncer(this, "pgbouncer", {
@@ -172,6 +177,7 @@ export class PgStacDatabase extends Construct {
172177
this.pgstacSecret = this._pgBouncerServer.pgbouncerSecret;
173178
this.connectionTarget = this._pgBouncerServer.instance;
174179
this.securityGroup = this._pgBouncerServer.securityGroup;
180+
this.secretBootstrapper = this._pgBouncerServer.secretUpdateComplete;
175181
} else {
176182
this.connectionTarget = this.db;
177183
}
@@ -226,10 +232,10 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
226232
readonly pgstacDbName?: string;
227233

228234
/**
229-
* Version of pgstac to install on the database
230-
*
231-
* @default 0.8.5
232-
*/
235+
* Version of pgstac to install on the database
236+
*
237+
* @default 0.8.5
238+
*/
233239
readonly pgstacVersion?: string;
234240

235241
/**

lib/ingestor-api/runtime/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ cachetools==5.3.0
33
fastapi>=0.75.1
44
orjson>=3.6.8
55
psycopg[binary,pool]>=3.0.15
6-
pydantic_ssm_settings>=0.2.0
6+
pydantic_ssm_settings>=0.2.0,<1.0
77
pydantic>=1.9.0
88
pypgstac==0.8.5
99
requests>=2.27.1

0 commit comments

Comments
 (0)