This is a serverless API developed using the AWS CDK and structured following best practices.
The application is organized into logical units, such as API, database, monitoring resources and deployment pipeline. These logical units are implemented as constructs, including the infrastructure and the runtime code. The constructs are later group in stacks, which define the deployment models.
The runtime code is written in Python and defines basic CRUD operations to manage music albums as an example.
In this project you can also find how to apply linting and formatting rules to your code and how to implement continuos integration using GitHub Actions and continuos deployment using CodePipeline.
This is a simple service that uses AWS API Gateway HTTP API. It's integrated with Lambda functions on the backend and uses IAM authentication. A DynamoDB table is used as the database.
Each logical unit has a directory that includes the related infrastructure, runtime and configuration code. This way, any developer can easily find the code related to a specific logical unit.
.
├── .github # GitHub actions for continuos integration
|
├──api
| |── cdk.ts # CDK construct with API resources and Lambdas
| |── api_resource # Create a folder for each API resource. In this case the resource name is Item
| | |── api_resource_method # Create a folder for each API method associated with that particular resource (parent folder)
| | | |── lambda_handler # Code for Lambda handler
| | | |── requirements.txt # Python packages that needs to be bundle with this Lambda
| |
| |── ...
|
|──database
| |── cdk.ts # CDK construct with DynamoDB table resource
|
├── monitoring
| |── cdk.ts # CDK construct with Monitoring dashboard and alarms
|
├── pipeline
| |── cdk.ts # CDK stack with deployment pipeline
|
|── tests
| |── integration # Integration tests
| |── unit # Unit tests
|
|── app.ts # Main CDK application (Constructs are imported here and deployed within Stacks)
|
|── other linters and formatters files (.eslintrc.yml, .flake8, .pylintrc)
|
└── ...
You can fork this repository and clone it to your local environment.
Then, if you want to deploy the application to the development environment, please follow the next steps:
npm install
npm run cdk deploy Python-Serverless-API-Stack Python-Serverless-API-Table-Stack Python-Serverless-API-Monitoring-Stack
To deploy the application through the deployment pipeline, follow these steps:
- Create AWS CodeStart GitHub connection
- Create SSM parameter for the connection ARN and assign the following name
Github-Connection
- Create SSM parameter for the connection ARN and assign the following name
- Modify the
app.ts
file and enter your Github user and the repo name
new CodePipelineStack(app, 'DeploymentPipelineStack', {
repoOwner: <github_user>,
repoName: <repo_name>
})
npm install
npm run cdk deploy Python-Serverless-API-Pipeline-Stack
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Run unit tests - python -m pytest tests/unit
Run integration tests - python -m pytest tests/integration
cdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template