From 8ab62f1a6b95fbab5cb4a01b03234b90d1667847 Mon Sep 17 00:00:00 2001 From: Edward Keeble Date: Tue, 21 Mar 2023 10:52:43 -0300 Subject: [PATCH] Updated README --- README.md | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 129 insertions(+) diff --git a/README.md b/README.md index e69de29..a186610 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,128 @@ +# CDK Bootstrapped DB Construct + +This CDK Construct enables provisioning an RDS Database (and optionally a DB Server) with a bootstrapping Lambda function for running migrations other setup requirements. + +## Usage + +```python +import os +from aws_cdk import ( + Duration, + aws_ec2 as ec2, + aws_rds as rds, + aws_lambda as lambda_, +) + +from constructs import Construct + +from cdk_bootstrapped_db.helpers import create_database_server +from cdk_bootstrapped_db.constructs import BootstrappedDb + + +class MyDatabase(Construct): + def __init__( + self, + scope: Construct, + id: str, + name: str, + secrets_prefix: str, + db_server: rds.DatabaseInstance, + vpc: ec2.IVpc, + **kwargs, + ): + super().__init__(scope, id, **kwargs) + + db_setup_handler = lambda_.Function( + self, + "RunMigrations", + handler="handler.handler", + runtime=lambda_.Runtime.PYTHON_3_8, + code=lambda_.Code.from_docker_build( + path=os.path.abspath("."), + file="Dockerfile", + ), + vpc=vpc, + ) + + self.db = BootstrappedDb( + self, + "MyDB", + db=db_server, + new_dbname="mydb", + new_username="mydbuser", + secrets_prefix=secrets_prefix, + handler=db_setup_handler, + ) + + self.db_connection_secret = self.db.secret +``` + +Any external constructs which then need to access the DB using the generated credentials can do so using the `db_connection_secret` property of the `MyDatabase` construct. + +### Creating an RDS Server + +This package comes with a helper function for setting up a new RDS server. The above example then becomes: + +```python +import os +from aws_cdk import ( + Duration, + aws_ec2 as ec2, + aws_rds as rds, + aws_lambda as lambda_, +) + +from constructs import Construct + +from cdk_bootstrapped_db.helpers import create_database_server +from cdk_bootstrapped_db.constructs import BootstrappedDb + + +class MyDatabase(Construct): + def __init__( + self, + scope: Construct, + id: str, + name: str, + secrets_prefix: str, + vpc: ec2.IVpc, + **kwargs, + ): + super().__init__(scope, id, **kwargs) + + self.db_server = create_database_server( + self, + "MyDBServer", + identifier="mydbserver", + vpc=vpc, + db_name="postgres", + db_version=rds.PostgresEngineVersion.VER_14, + instance_type=ec2.InstanceType.of( + ec2.InstanceClass.BURSTABLE4_GRAVITON, ec2.InstanceSize.NANO + ), + ) + + db_setup_handler = lambda_.Function( + self, + "RunMigrations", + handler="handler.handler", + runtime=lambda_.Runtime.PYTHON_3_8, + code=lambda_.Code.from_docker_build( + path=os.path.abspath("."), + file="Dockerfile", + ), + vpc=vpc, + ) + + self.db = BootstrappedDb( + self, + "MyDB", + db=self.db_server, + new_dbname="mydb", + new_username="mydbuser", + secrets_prefix=secrets_prefix, + handler=db_setup_handler, + ) + + self.db_connection_secret = self.db.secret +``` diff --git a/setup.py b/setup.py index 75dc5bb..db61975 100644 --- a/setup.py +++ b/setup.py @@ -42,6 +42,7 @@ "tests", ] ), + data_files=[("version", ["VERSION"])], zip_safe=False, include_package_data=True, install_requires=install_requires,