|
| 1 | +import logging |
| 2 | + |
| 3 | +from lorrystream.carabas.aws import RDSPostgreSQLDMSKinesisPipe |
| 4 | +from lorrystream.util.common import setup_logging |
| 5 | + |
| 6 | +logger = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +def main(): |
| 10 | + """ |
| 11 | + A recipe to deploy a data relay stack to Amazon AWS. |
| 12 | +
|
| 13 | + Pipeline: |
| 14 | + - RDS PostgreSQL -> DMS -> Kinesis Stream -> Python Lambda via OCI -> CrateDB |
| 15 | +
|
| 16 | + Ingredients: |
| 17 | + - DMS, RDS PostgreSQL, Kinesis |
| 18 | + - Lambda function, shipped per OCI image |
| 19 | + - CrateDB Cloud |
| 20 | +
|
| 21 | + Prerequisites: Register an OCI repository. |
| 22 | + """ |
| 23 | + |
| 24 | + # Build and publish OCI image that includes the AWS Lambda function. |
| 25 | + """ |
| 26 | + python_image = LambdaPythonImage( |
| 27 | + name="cratedb-kinesis-lambda", |
| 28 | + entrypoint_file=Path("./lorrystream/process/kinesis_cratedb_lambda.py"), |
| 29 | + entrypoint_handler="kinesis_cratedb_lambda.handler", |
| 30 | + ) |
| 31 | + python_image.publish() |
| 32 | + """ |
| 33 | + |
| 34 | + # Define an AWS CloudFormation software stack. |
| 35 | + stack = RDSPostgreSQLDMSKinesisPipe( |
| 36 | + project="testdrive-dms-postgresql", |
| 37 | + stage="dev", |
| 38 | + region="eu-central-1", |
| 39 | + description="RDS PostgreSQL > DMS -> Kinesis Stream -> Python Lambda via OCI -> CrateDB", |
| 40 | + db_username="dynapipe", |
| 41 | + db_password="secret11", # noqa: S106 |
| 42 | + environment={ |
| 43 | + "SINK_SQLALCHEMY_URL": "crate://admin:dZ..qB@example.eks1.eu-west-1.aws.cratedb.net:4200/?ssl=true", |
| 44 | + "SINK_TABLE": "transactions", |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + # Add components to the stack. |
| 49 | + """ |
| 50 | + stack.table().processor( |
| 51 | + LambdaFactory( |
| 52 | + name="DynamoDBCrateDBProcessor", |
| 53 | + oci_uri=python_image.uri, |
| 54 | + handler=python_image.entrypoint_handler, |
| 55 | + ) |
| 56 | + ).connect() |
| 57 | + """ |
| 58 | + stack.vpc().database().stream().dms() # .table() |
| 59 | + |
| 60 | + # Deploy stack. |
| 61 | + stack.deploy() |
| 62 | + logger.info(f"Deployed stack: {stack}") |
| 63 | + |
| 64 | + # Refresh the OCI image. |
| 65 | + # TODO: Detect when changed. |
| 66 | + stack.deploy_processor_image() |
| 67 | + |
| 68 | + PublicDbEndpoint = stack.get_output_value(stack._bsm, "PublicDbEndpoint") |
| 69 | + PublicDbPort = stack.get_output_value(stack._bsm, "PublicDbPort") |
| 70 | + psql_command = ( |
| 71 | + f'psql "postgresql://{stack.db_username}:{stack.db_password}@{PublicDbEndpoint}:{PublicDbPort}/postgres"' |
| 72 | + ) |
| 73 | + print(psql_command) |
| 74 | + |
| 75 | + print("Stream ARN:", stack.get_output_value(stack._bsm, "StreamArn")) |
| 76 | + |
| 77 | + """ |
| 78 | + aws dms describe-replications |
| 79 | + aws dms start-replication \ |
| 80 | + --start-replication-type=start-replication \ |
| 81 | + --replication-config-arn arn:aws:dms:eu-central-1:931394475905:replication-config:LB2JAGY7XFB7PA7HEX3MI36CUA |
| 82 | +
|
| 83 | + aws logs describe-log-groups |
| 84 | + aws logs start-live-tail --log-group-identifiers \ |
| 85 | + arn:aws:logs:eu-central-1:931394475905:log-group:/aws/rds/instance/testdrive-dms-postgresql-dev-db/postgresql \ |
| 86 | + arn:aws:logs:eu-central-1:931394475905:log-group:dms-serverless-replication-LB2JAGY7XFB7PA7HEX3MI36CUA |
| 87 | +
|
| 88 | + aws cloudformation continue-update-rollback --stack-name testdrive-dms-postgresql-dev |
| 89 | + aws cloudformation delete-stack --stack-name testdrive-dms-postgresql-dev |
| 90 | + """ |
| 91 | + """ |
| 92 | + - https://docs.aws.amazon.com/dms/latest/APIReference/API_StartReplication.html#DMS-StartReplication-request-StartReplicationType |
| 93 | + - https://docs.aws.amazon.com/cli/latest/reference/dms/start-replication-task.html |
| 94 | +
|
| 95 | + Possible values: |
| 96 | +
|
| 97 | + - start-replication |
| 98 | + - resume-processing |
| 99 | + - reload-target |
| 100 | + """ |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + setup_logging() |
| 105 | + main() |
0 commit comments