|
| 1 | +# demo-aurora-eventual-s3-data-load |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +This project implements a robust, event-driven architecture that seamlessly ingests `CSV` files uploaded to an _Amazon |
| 6 | +S3_ bucket into an _Aurora MySQL_ database. It leverages the powerful `LOAD` command of the _MySQL_ engine to |
| 7 | +efficiently load the data into the target database. |
| 8 | + |
| 9 | +Key Features: |
| 10 | + |
| 11 | +1. **Event-Driven Architecture**: The system is designed to react to events triggered by `CSV` file uploads to the |
| 12 | + designated _S3_ bucket. This ensures real-time data ingestion and minimizes manual intervention. |
| 13 | + |
| 14 | +2. **Reliable Data Ingestion**: The `LOAD` command is utilized to efficiently and reliably load the `CSV` data into the |
| 15 | + _Aurora MySQL_ database, ensuring data integrity and consistency. |
| 16 | + |
| 17 | +3. **Error Handling and Notifications**: In the event of errors or warnings during the data ingestion process, the |
| 18 | + system captures and stores the error details in an _SQS Dead Letter Queue_ (DLQ). This facilitates subsequent |
| 19 | + analysis and troubleshooting. Additionally, an alarm is triggered to notify the relevant stakeholders, enabling |
| 20 | + prompt resolution of issues. |
| 21 | + |
| 22 | +4. **Monitoring and Observability**: The system incorporates monitoring capabilities, allowing you to track the |
| 23 | + transaction, health, and potential issues in the data ingestion pipeline. |
| 24 | + |
| 25 | +## Architecture |
| 26 | + |
| 27 | +The architecture consists of the following components: |
| 28 | + |
| 29 | +1. An _S3_ bucket that will store the `CSV` file and notify the subsequent components. |
| 30 | +2. An _SQS_ queue to store the event notification from the _S3_ bucket. |
| 31 | +3. An _SQS_ queue to store any failed attempts of processing the files (_Dead-letter queue_). |
| 32 | +4. A _Lambda_ function deployed in the same _VPC_ as the database. This function will consume the message from the _SQS_ |
| 33 | + queue and trigger the database `LOAD` process, providing the data file details. |
| 34 | +5. An _Aurora MySQL_ database where the data is persisted and the new data will be loaded. The database is responsible |
| 35 | + to fetch the data file from the _S3_ bucket through the use of the `LOAD` command. |
| 36 | +6. A _CloudWatch Alarm_ that will monitor the metric `NumberOfMessagesReceived` of the _DLQ_. |
| 37 | +7. An _SNS_ topic that will be triggered from the alarm created in case of new messages arriving in the _DLQ_. The |
| 38 | + stakeholders can subscribe to this topic directly using their e-mail addresses or mobile phone numbers. It's also |
| 39 | + possible to create a custom integration that will allow the architecture to notify the related stakeholders using |
| 40 | + third-party components. |
| 41 | +8. _CloudWatch Logs_ will capture and store all the logs produced by the _Lambda_ functions for further analysis. |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Deployment Guide |
| 46 | + |
| 47 | +### Prerequisites |
| 48 | + |
| 49 | +- AWS CLI installed and configured with appropriate IAM permissions |
| 50 | +- NodeJS installed (version 22.4 or later) |
| 51 | +- AWS CDK installed (version 2.160 or later) |
| 52 | + |
| 53 | +### CDK Toolkit |
| 54 | + |
| 55 | +The `cdk.json` file tells the CDK Toolkit how to execute your app. |
| 56 | + |
| 57 | +To start working with the project, first you will need to install all dependencies as well as the cdk module (if not |
| 58 | +installed already). In the project directory, run: |
| 59 | + |
| 60 | +```bash |
| 61 | +$ npm install |
| 62 | +``` |
| 63 | + |
| 64 | +### Deploying the solution |
| 65 | + |
| 66 | +To deploy the solution, we will need to request cdk to deploy the stacks: |
| 67 | + |
| 68 | +```shell |
| 69 | +$ cdk deploy --all |
| 70 | +``` |
| 71 | + |
| 72 | +> **Note**: After the deployment is done, some output data is presented on the terminal, providing information regarding |
| 73 | +> the deployed solution: |
| 74 | +> - **DataBucketName**: S3 bucket where the data files will be uploaded. |
| 75 | +> - **DataLoadQueueName**: Queue responsible for storing the events sent from S3. |
| 76 | +> - **DLQName**: Dead-letter queue responsible for storing failed events. |
| 77 | +> - **NotificationTopicName**: SNS topic responsible for notifying the stakeholders regarding failed processes. |
| 78 | +> - **FunctionLogGroupName**: CloudWatch Log Group responsible for storing the Lambda's function logs. |
| 79 | +
|
| 80 | +```shell |
| 81 | +Outputs: |
| 82 | +DemoAuroraEventualDataLoadStack.BastionHostSecurityGroupId = sg-XXXXXXXXXXX |
| 83 | +DemoAuroraEventualDataLoadStack.DLQName = demo-data-load-dlq |
| 84 | +DemoAuroraEventualDataLoadStack.DataBucketName = data-bucket-XXXXXXXXXXX |
| 85 | +DemoAuroraEventualDataLoadStack.DataLoadQueueName = demo-data-load |
| 86 | +DemoAuroraEventualDataLoadStack.DatabaseSecretName = demo-aurora-eventual-load-database-secret |
| 87 | +DemoAuroraEventualDataLoadStack.FunctionLogGroupName = /aws/lambda/demo-aurora-eventual-data-load-function |
| 88 | +DemoAuroraEventualDataLoadStack.NotificationTopicName = demo-aurora-eventual-load-notification |
| 89 | +DemoAuroraEventualDataLoadStack.VpcId = vpc-XXXXXXXXXXX |
| 90 | +``` |
| 91 | + |
| 92 | +## Testing the solution |
| 93 | + |
| 94 | +1. Head to _AWS_ console and then to _S3_ |
| 95 | +2. Select the bucket provided from the deployment command and click on `Upload` |
| 96 | +3. Select one of the files present on this repo in the _/data_ directory. |
| 97 | + 1. `db-data.csv` will load successfully into the database and will generate enough logs for you to check the number |
| 98 | + of rows loaded. |
| 99 | + 2. `db-data-with-error.csv` will produce some errors and will deliver the message to the _DLQ_, which will trigger |
| 100 | + the alarm and send a message to the alarm topic. This will also generate enough logs for better understand the |
| 101 | + errors encountered. |
| 102 | +4. You can check the logs produced by the solution using the _CloudWatch Log Group_ provided after the deployment |
| 103 | + process. |
| 104 | +5. Whenever you want to test the failure scenario, after uploading the data file with errors, you will be able to see |
| 105 | + the failure logs on the _CloudWatch Log Group_ and the alarm in "In Alarm" state on _CloudWatch Alarms_ section. |
| 106 | +6. **(OPTIONAL)** You can subscribe your e-mail address to the _SNS Notification Topic_ and validate the e-mail sent on |
| 107 | + the failure event. |
| 108 | +7. **(OPTIONAL)** You can access the database by deploying an _EC2_ bastion host or _CloudShell_ session inside the |
| 109 | + created _VPC_ (see the `VpcId` output). You will need to install the _MySQL_ client and use the database credentials |
| 110 | + through _Secrets Manager_ (see the `DatabaseSecretName` output). |
| 111 | + |
| 112 | +## Cleanup |
| 113 | + |
| 114 | +To destroy the provisioned infrastructure, you can simply run the following command: |
| 115 | + |
| 116 | +```shell |
| 117 | +$ cdk destroy --all |
| 118 | +``` |
0 commit comments