{"id":13626033,"url":"https://github.com/abiodunjames/aws-serverless-aurora","last_synced_at":"2026-02-05T10:26:55.374Z","repository":{"id":112494847,"uuid":"312578030","full_name":"abiodunjames/aws-serverless-aurora","owner":"abiodunjames","description":"Build a fully Serverless backend on AWS and how you can handle database schema migration using Amazon API Gateway, AWS Lambda, Amazon Aurora Serverless (MySQL) and Python CDK.","archived":false,"fork":false,"pushed_at":"2020-11-13T16:24:11.000Z","size":68,"stargazers_count":23,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-08T15:47:00.531Z","etag":null,"topics":["api-gateway","aurora-serverless","cdk","lambda","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abiodunjames.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-11-13T13:06:15.000Z","updated_at":"2024-11-08T02:31:12.000Z","dependencies_parsed_at":"2023-05-15T08:45:42.928Z","dependency_job_id":null,"html_url":"https://github.com/abiodunjames/aws-serverless-aurora","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiodunjames%2Faws-serverless-aurora","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiodunjames%2Faws-serverless-aurora/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiodunjames%2Faws-serverless-aurora/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiodunjames%2Faws-serverless-aurora/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abiodunjames","download_url":"https://codeload.github.com/abiodunjames/aws-serverless-aurora/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249235042,"owners_count":21235136,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-gateway","aurora-serverless","cdk","lambda","python"],"created_at":"2024-08-01T21:02:08.240Z","updated_at":"2026-02-05T10:26:55.330Z","avatar_url":"https://github.com/abiodunjames.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Serverless Aurora App with Database Migration\n\nThis project shows how to build a fully Serverless backend on AWS and how you can approach  database schema migration in [ Amazon Aurora Serverless](https://aws.amazon.com/rds/aurora/serverless/) (MySQL)  using [Amazon API Gateway](https://aws.amazon.com/api-gateway/), [AWS Lambda](https://aws.amazon.com/lambda/), and Python CDK. \n\n## How to Run\n\n1. **Clone the code repository**\n\n```\ngit clone git@github.com:abiodunjames/aws-serverless-aurora.git\n```\n\n\n\n2. **Create a virtualenv**\n\nTo create the virtualenv it assumes that there is a `python3` (or `python` for Windows) executable in your path with access to the `venv` package. If for any reason the automatic creation of the virtualenv fails, you can create the virtualenv manually.\n\nTo manually create a virtualenv on MacOS and Linux:\n\n```\n$ python3 -m venv .venv\n```\n\nAfter the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.\n\n```\n$ source .venv/bin/activate\n```\n\nIf you are a Windows platform, you would activate the virtualenv like this:\n\n```\n% .venv\\Scripts\\activate.bat\n```\n\n3. **Once the virtualenv is activated, you can install the required dependencies.**\n\n```\n$ pip install -r requirements.txt\n```\n\nAt this point you can now synthesize the CloudFormation template for this code.\n\n```\n$ cdk synth\n```\n\nTo add additional dependencies, for example other CDK libraries, just add them to your `setup.py` file and rerun the `pip install -r requirements.txt` command.\n\n4. **Now you deploy the application** \n\n```\ncdk deploy\n```\n\nOn successful deployment, you can browse the endpoint at:\n```\ncurl https://xxxxxx.execute-api.xxxxxx.amazonaws.com/prod/posts \n```\n\n```\ncurl https://hkgpg3z1xd.execute-api.eu-west-1.amazonaws.com/prod/posts\n\n{\"statusCode\": 200, \"body\": [[{\"longValue\": 1}, {\"stringValue\": \"344d117ea9f1\"}, {\"stringValue\": \"A serverless blogpost with Aurora\"}, {\"stringValue\": \"serverless-aurora\"}, {\"stringValue\": \"This is the description of the post\"}, {\"longValue\": 0}, {\"isNull\": true}, {\"isNull\": true}]]}%     \n\n```\n##  Solution\n\n![](./docs/aws-serverless-aurora.png)\n\n\n\n#### Database Schema Migration \n\nThe code leverages custom resources with a lambda function to run database migration schema after provisioning an aurora instannce.\n\nThe SQL scripts are packaged into lambda layer, available to the executing function in the \"/opt\" directory.\n\n\n#### Config\n\nThe `config.py`  contains route definition and how http endpoints map to each lambda function. It's a dictionary object that looks like this:\n\n```python\nroutes = {\n    \"posts\": [\n        {\"method\": \"GET\", \"function\": \"get_posts\"},\n        {\"method\": \"POST\", \"function\": \"create_posts\"},\n    ],\n}\n```\n\nThe idea behind this is to make the relationship between paths, methods and functions configurable.\n\n#### Functions\n\nLambda functions are defined in in this directory.  All functions should use `handler` as nme. \n\n```\ndef handler(event, context):\n\t\t.......\n```\n\n#### Scripts\n\nSQL scripts are located in the `scripts/schema` directory.  It contains the schema of your database.  You may version migration scripts. For example:\n\n```\nv1.sql\nv2.sql\nv3.sql\n...\n```\n### References\n * [Using Aurora Serverless and the Data API](https://github.com/aws-samples/aws-aurora-serverless-data-api-sam)\n * [AWS CDK Examples (Custom Resource)](https://github.com/aws-samples/aws-cdk-examples/tree/master/python/custom-resource)\n\n## Known Issues\n\nCustom resource functions are not run on subsequent deploys if no modifications were made to the custom resource. \n\nAs you develop, you might need to modify your SQL scripts during development and expect the lambda function to apply the new changes during the next deployment. Unfortunately, this does not work as you may have expected.   My thought on this is similar to what [Tom](https://github.com/tommedema) wrote [here](https://github.com/serverless/serverless/issues/4483).\n\n\u003e Cloudformation creates an internal state, and then if the resource is already created once, it won't be created again unless you previously deleted it. If you then change the resource parameters, it will be run again with the Update event.RequestType.\n\nHowever, there is a workaround suggested in this [thread](https://github.com/serverless/serverless/issues/4483 ) to fix this temporarily.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabiodunjames%2Faws-serverless-aurora","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabiodunjames%2Faws-serverless-aurora","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabiodunjames%2Faws-serverless-aurora/lists"}