An open API service indexing awesome lists of open source software.

https://github.com/briancaffey/cdk-django

A CDK library that provides high-level constructs for hosting Django applications on AWS
https://github.com/briancaffey/cdk-django

aws cdk cdk-construct-library containers devops django docker ecs fargate github-actions infrastructure-as-code poetry projen terraform typescript

Last synced: 17 days ago
JSON representation

A CDK library that provides high-level constructs for hosting Django applications on AWS

Awesome Lists containing this project

README

        

# `cdk-django`

## About this construct library

`cdk-django` is a library for deploying Django applications to AWS using ECS Fargate.

`cdk-django` aims to demonstrate best-practices for building web applications in AWS cloud. Executing the code in this project will build cloud resources such as networks, servers and databases that power a web application that can run securely on the public internet.

You don't need to use this library directly in your code. It is recommended that you use this repo as a guide. It includes common patterns that are used when working with Infrastructure as Code.

### Companion application

This project has a companion repo that contains a Django (Python) web application backend and a Nuxt.js frontend site (server-rendered Vue.js written in TypeScript). This project can be found here:

[https://github.com/briancaffey/django-step-by-step/](https://github.com/briancaffey/django-step-by-step/)

### Related projects

This project is has been written with the two other main Infrastructure as Code tools: Terraform and Pulumi. You can find these repos here:

- [terraform-aws-django](https://github.com/briancaffey/terraform-aws-django)
- [pulumi-aws-django](https://github.com/briancaffey/pulumi-aws-django)

## Project structure

There are two main constructs the build resources for the different stacks:

- `base`
- `app`

The `base` stack deploys long-lived resources that shouldn't need to be updated frequently, these include:

- VPC
- ElastiCache
- S3
- Security Groups
- Load balancer
- RDS

The `app` stack deploys resources primarily for ECS services that run the application processes, these include:

- ECS cluster for the environment
- web-facing services (for running gunicorn and for running the frontend UI app)
- celery worker for asynchronous task processing
- celery beat for scheduled tasks
- management_command for running migrations and other "pre-update" tasks (collectstatic, loading fixtures, etc.)
- All backend environment variables are configured here (shared between all backend services)
- Route 53 record for the environment (e.g. `.example.com`)
- IAM resources (this might be able to be moved to the base stack)

### Local Examples

It is best to deploy cloud infrastructure with automated pipelines that execute Infrastructure as Code. For testing and development you can deploy locally.

The `Makefile` in this repo documents the commands to create and destroy infrastructure for the `base` and `app` stacks. For example:

```Makefile
# base stack
ecs-base-synth:
cdk synth --app='lib/examples/ecs/index.js' -e ExampleEcsBaseStack

ecs-base-diff:
cdk diff --app='./lib/examples/ecs/index.js' -e ExampleEcsBaseStack

ecs-base-deploy:
cdk deploy --app='./lib/examples/ecs/index.js' -e ExampleEcsBaseStack

ecs-base-deploy-approve:
cdk deploy --app='./lib/examples/ecs/index.js' --require-approval never -e ExampleEcsBaseStack

ecs-base-destroy:
yes | cdk destroy --app='./lib/examples/ecs/index.js' -e ExampleEcsBaseStack
```

## Companion application

To see how `terraform-aws-django` can be used, have a look at [https://github.com/briancaffey/django-step-by-step](https://github.com/briancaffey/django-step-by-step).

This companion repo includes two main components: a Django application (backend) and a Nuxt.js application (frontend)

### Django backend application features

- email-based authentication flow (confirmation email + forgot password)
- microblog app (users can write posts with text and images, like posts)
- chat app (a simple OpenAI API wrapper)

### Nuxt.js frontend client features

- Vue 3, Nuxt v3.15
- SSR
- shadcn
- tailwindcss
- pinia
- composables

This construct library focuses on security, best practices, scalability, flexibility and cost-efficiency.

## Maintaining this repo

Make sure you are on the most recent version of CDK:

```
npm i -g aws-cdk
```

This project is managed by [`projen`](https://github.com/projen/projen). To update the application, run the following:

```
npx projen upgrade
```

Update [CDK version](https://github.com/aws/aws-cdk/releases) in `.projenrc.ts` and then run:

```
npx projen
```

Run `npx projen watch` in one terminal.

In another terminal, export AWS credentials and environment variables and then run commands in the `Makefile`.

Here are the environment variables you will need to export:

```
export DOMAIN_NAME=example.com
export CERTIFICATE_ARN=arn:aws:acm:us-east-1:111111111111:certificate/11111111-1111-1111-1111-111111111111
export AWS_REGION=us-east-1
export AWS_ACCESS_KEY_ID=abc
export AWS_SESSION_TOKEN=123
export COMPANY_NAME=abc
export AWS_ACCOUNT_ID=123456789
```

## Release process

This project uses `projen` to manage the release process. There is a GitHub Action called `release` that will make a release and publish it to npm. Before running release, make sure you run `yarn build` to build the documentation, and run `yarn eslint` to fix any linting errors.

## Issues

- [https://github.com/projen/projen/issues/3950](https://github.com/projen/projen/issues/3950)