|
| 1 | +import { |
| 2 | + aws_ec2, |
| 3 | + aws_rds, |
| 4 | + aws_lambda, |
| 5 | + aws_logs, |
| 6 | + aws_secretsmanager, |
| 7 | + CustomResource, |
| 8 | + Duration, |
| 9 | + Stack, |
| 10 | + RemovalPolicy, |
| 11 | +} from "aws-cdk-lib"; |
| 12 | +import { Construct } from "constructs"; |
| 13 | + |
| 14 | +function hasVpc( |
| 15 | + instance: aws_rds.DatabaseInstance | aws_rds.IDatabaseInstance |
| 16 | +): instance is aws_rds.DatabaseInstance { |
| 17 | + return (instance as aws_rds.DatabaseInstance).vpc !== undefined; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Bootstraps a database instance, installing pgSTAC onto the database. |
| 22 | + */ |
| 23 | +export class BootstrapPgStac extends Construct { |
| 24 | + secret: aws_secretsmanager.ISecret; |
| 25 | + |
| 26 | + constructor(scope: Construct, id: string, props: BootstrapPgStacProps) { |
| 27 | + super(scope, id); |
| 28 | + |
| 29 | + const handler = new aws_lambda.Function(this, "lambda", { |
| 30 | + handler: "handler.handler", |
| 31 | + runtime: aws_lambda.Runtime.PYTHON_3_8, |
| 32 | + code: aws_lambda.Code.fromDockerBuild(__dirname, { |
| 33 | + file: "runtime/Dockerfile", |
| 34 | + buildArgs: { PGSTAC_VERSION: props.pgstacVersion }, |
| 35 | + }), |
| 36 | + timeout: Duration.minutes(2), |
| 37 | + vpc: hasVpc(props.database) ? props.database.vpc : props.vpc, |
| 38 | + logRetention: aws_logs.RetentionDays.ONE_WEEK, |
| 39 | + }); |
| 40 | + |
| 41 | + this.secret = new aws_secretsmanager.Secret(this, "secret", { |
| 42 | + secretName: [ |
| 43 | + props.secretsPrefix || "pgstac", |
| 44 | + id, |
| 45 | + this.node.addr.slice(-8), |
| 46 | + ].join("/"), |
| 47 | + generateSecretString: { |
| 48 | + secretStringTemplate: JSON.stringify({ |
| 49 | + dbname: props.pgstacDbName || "pgstac", |
| 50 | + engine: "postgres", |
| 51 | + port: 5432, |
| 52 | + host: props.database.instanceEndpoint.hostname, |
| 53 | + username: props.pgstacUsername || "pgstac_user", |
| 54 | + }), |
| 55 | + generateStringKey: "password", |
| 56 | + excludePunctuation: true, |
| 57 | + }, |
| 58 | + description: `PgSTAC database bootstrapped by ${ |
| 59 | + Stack.of(this).stackName |
| 60 | + }`, |
| 61 | + }); |
| 62 | + |
| 63 | + // Allow lambda to... |
| 64 | + // read new user secret |
| 65 | + this.secret.grantRead(handler); |
| 66 | + // read database secret |
| 67 | + props.dbSecret.grantRead(handler); |
| 68 | + // connect to database |
| 69 | + props.database.connections.allowFrom(handler, aws_ec2.Port.tcp(5432)); |
| 70 | + |
| 71 | + // this.connections = props.database.connections; |
| 72 | + new CustomResource(this, "bootstrapper", { |
| 73 | + serviceToken: handler.functionArn, |
| 74 | + properties: { |
| 75 | + // By setting pgstac_version in the properties assures |
| 76 | + // that Create/Update events will be passed to the service token |
| 77 | + pgstac_version: props.pgstacVersion, |
| 78 | + conn_secret_arn: props.dbSecret.secretArn, |
| 79 | + new_user_secret_arn: this.secret.secretArn, |
| 80 | + }, |
| 81 | + removalPolicy: RemovalPolicy.RETAIN, // This retains the custom resource (which doesn't really exist), not the database |
| 82 | + }); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export interface BootstrapPgStacProps { |
| 87 | + /** |
| 88 | + * VPC in which the database resides. |
| 89 | + * |
| 90 | + * Note - Must be explicitely set if the `database` only conforms to the |
| 91 | + * `aws_rds.IDatabaseInstace` interface (ie it is a reference to a database instance |
| 92 | + * rather than a database instance.) |
| 93 | + * |
| 94 | + * @default - `vpc` property of the `database` instance provided. |
| 95 | + */ |
| 96 | + readonly vpc?: aws_ec2.IVpc; |
| 97 | + |
| 98 | + /** |
| 99 | + * Database onto which pgSTAC should be installed. |
| 100 | + */ |
| 101 | + readonly database: aws_rds.DatabaseInstance | aws_rds.IDatabaseInstance; |
| 102 | + |
| 103 | + /** |
| 104 | + * Secret containing valid connection details for the database instance. Secret must |
| 105 | + * conform to the format of CDK's `DatabaseInstance` (i.e. a JSON object containing a |
| 106 | + * `username`, `password`, `host`, `port`, and optionally a `dbname`). If a `dbname` |
| 107 | + * property is not specified within the secret, the bootstrapper will attempt to |
| 108 | + * connect to a database with the name of `"postgres"`. |
| 109 | + */ |
| 110 | + readonly dbSecret: aws_secretsmanager.ISecret; |
| 111 | + |
| 112 | + /** |
| 113 | + * Name of database that is to be created and onto which pgSTAC will be installed. |
| 114 | + * |
| 115 | + * @default - "pgstac" |
| 116 | + */ |
| 117 | + readonly pgstacDbName?: string; |
| 118 | + |
| 119 | + /** |
| 120 | + * Name of user that will be generated for connecting to the pgSTAC database. |
| 121 | + * |
| 122 | + * @default - "pgstac_user" |
| 123 | + */ |
| 124 | + readonly pgstacUsername?: string; |
| 125 | + |
| 126 | + /** |
| 127 | + * pgSTAC version to be installed. |
| 128 | + */ |
| 129 | + readonly pgstacVersion: string; |
| 130 | + |
| 131 | + /** |
| 132 | + * Prefix to assign to the generated `secrets_manager.Secret` |
| 133 | + * |
| 134 | + * @default - "pgstac" |
| 135 | + */ |
| 136 | + readonly secretsPrefix: string; |
| 137 | +} |
0 commit comments