-
Notifications
You must be signed in to change notification settings - Fork 1
Feat: use PostgreSQL for the database backend #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lalver1
wants to merge
9
commits into
main
Choose a base branch
from
feat/django-postgres-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
354f0f4
feat(compose): add postgres and pgweb services
lalver1 9d70d3f
refactor(settings): use postgres backend
lalver1 ed38802
feat(commands): add management command for database setup
lalver1 7288a6b
chore(commands): remove redundant database setup commands
lalver1 efaa9a6
feat(compose): ensure postgres server is ready
lalver1 bfc4663
feat(database): add Aurora Serverless storage and configuration
lalver1 332fb4a
fix(compose): remove pgweb container_name to avoid conflict
lalver1 fc0c42e
fix(database): Aurora Serverless setup and configuration
lalver1 721edc3
refactor(tests): use CBV testing approach for DistrictView test
lalver1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2,660 changes: 2,660 additions & 0 deletions
2,660
appcontainer/certs/aws_global_postgres_ca_bundle.pem
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
set -ex | ||
|
||
# Ensure databases, users, migrations, and superuser are set up | ||
should_reset=${REMOTE_CONTAINERS:-false} | ||
if [[ $should_reset == "true" ]]; then | ||
# running in a devcontainer, reset the DB | ||
python manage.py ensure_db --reset | ||
else | ||
python manage.py ensure_db | ||
fi | ||
|
||
# Load data fixtures (if any) | ||
valid_fixtures=$(echo "$DJANGO_DB_FIXTURES" | grep -e fixtures\.json$ || test $? = 1) | ||
|
||
if [[ -n "$valid_fixtures" ]]; then | ||
python manage.py loaddata $DJANGO_DB_FIXTURES | ||
else | ||
echo "No JSON fixtures to load" | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,14 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
|
||
# | ||
# S3 bucket name is injected by Copilot as an environment variable | ||
# since it was created via copilot storage init --name pems-db, the variable is 'PEMSDB_NAME' | ||
S3_BUCKET_NAME="$PEMSDB_NAME" | ||
S3_FIXTURE_PATH="fixtures.json" | ||
LOCAL_FIXTURE_PATH="fixtures.json" | ||
|
||
echo "Downloading $S3_FIXTURE_PATH from bucket $S3_BUCKET_NAME" | ||
aws s3 cp "s3://${S3_BUCKET_NAME}/${S3_FIXTURE_PATH}" "${LOCAL_FIXTURE_PATH}" | ||
aws s3 cp "s3://${S3_BUCKET_NAME}/${S3_FIXTURE_PATH}" "${DJANGO_DB_FIXTURES}" | ||
echo "Download complete" | ||
|
||
# initialize Django | ||
|
||
bin/init.sh | ||
|
||
# effectively reset database by loading downloaded fixtures into the database | ||
echo "Loading data from ${LOCAL_FIXTURE_PATH}" | ||
python manage.py loaddata "${LOCAL_FIXTURE_PATH}" | ||
echo "Data loading complete" | ||
|
||
# start the web server | ||
|
||
nginx | ||
|
||
# start the application server | ||
|
||
python -m gunicorn -c $GUNICORN_CONF pems.wsgi | ||
bin/setup.sh | ||
bin/start.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
Parameters: | ||
App: | ||
Type: String | ||
Description: Your application's name. | ||
Env: | ||
Type: String | ||
Description: The environment name your service, job, or workflow is being deployed to. | ||
Name: | ||
Type: String | ||
Description: Your workload's name. | ||
# Customize your Aurora Serverless cluster by setting the default value of the following parameters. | ||
postgreswebDBName: | ||
thekaveman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Type: String | ||
Description: The name of the initial database to be created in the Aurora Serverless v2 cluster. | ||
Default: postgres | ||
# Cannot have special characters | ||
# Naming constraints: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.Constraints | ||
Mappings: | ||
postgreswebEnvScalingConfigurationMap: | ||
dev: | ||
"DBMinCapacity": 0.5 # AllowedValues: from 0.5 through 128 | ||
"DBMaxCapacity": 8 # AllowedValues: from 0.5 through 128 | ||
|
||
All: | ||
"DBMinCapacity": 0.5 # AllowedValues: from 0.5 through 128 | ||
"DBMaxCapacity": 8 # AllowedValues: from 0.5 through 128 | ||
|
||
Resources: | ||
postgreswebDBSubnetGroup: | ||
Type: "AWS::RDS::DBSubnetGroup" | ||
Properties: | ||
DBSubnetGroupDescription: Group of Copilot private subnets for Aurora Serverless v2 cluster. | ||
SubnetIds: | ||
!Split [",", { "Fn::ImportValue": !Sub "${App}-${Env}-PrivateSubnets" }] | ||
postgreswebSecurityGroup: | ||
Metadata: | ||
"aws:copilot:description": "A security group for your workload to access the Aurora Serverless v2 cluster postgresweb" | ||
Type: "AWS::EC2::SecurityGroup" | ||
Properties: | ||
GroupDescription: !Sub "The Security Group for ${Name} to access Aurora Serverless v2 cluster postgresweb." | ||
VpcId: | ||
Fn::ImportValue: !Sub "${App}-${Env}-VpcId" | ||
Tags: | ||
- Key: Name | ||
Value: !Sub "copilot-${App}-${Env}-${Name}-Aurora" | ||
postgreswebDBClusterSecurityGroup: | ||
Metadata: | ||
"aws:copilot:description": "A security group for your Aurora Serverless v2 cluster postgresweb" | ||
Type: AWS::EC2::SecurityGroup | ||
Properties: | ||
GroupDescription: The Security Group for the Aurora Serverless v2 cluster. | ||
SecurityGroupIngress: | ||
- ToPort: 5432 | ||
FromPort: 5432 | ||
IpProtocol: tcp | ||
Description: !Sub "From the Aurora Security Group of the workload ${Name}." | ||
SourceSecurityGroupId: !Ref postgreswebSecurityGroup | ||
VpcId: | ||
Fn::ImportValue: !Sub "${App}-${Env}-VpcId" | ||
Tags: | ||
- Key: Name | ||
Value: !Sub "copilot-${App}-${Env}-${Name}-Aurora" | ||
postgreswebAuroraSecret: | ||
Metadata: | ||
"aws:copilot:description": "A Secrets Manager secret to store your DB credentials" | ||
Type: AWS::SecretsManager::Secret | ||
Properties: | ||
Description: !Sub Aurora main user secret for ${AWS::StackName} | ||
GenerateSecretString: | ||
SecretStringTemplate: '{"username": "postgres"}' | ||
GenerateStringKey: "password" | ||
ExcludePunctuation: true | ||
IncludeSpace: false | ||
PasswordLength: 16 | ||
postgreswebDBClusterParameterGroup: | ||
Metadata: | ||
"aws:copilot:description": "A DB parameter group for engine configuration values" | ||
Type: "AWS::RDS::DBClusterParameterGroup" | ||
Properties: | ||
Description: !Ref "AWS::StackName" | ||
Family: "aurora-postgresql16" | ||
Parameters: | ||
client_encoding: "UTF8" | ||
postgreswebDBCluster: | ||
Metadata: | ||
"aws:copilot:description": "The postgresweb Aurora Serverless v2 database cluster" | ||
Type: "AWS::RDS::DBCluster" | ||
Properties: | ||
MasterUsername: | ||
!Join [ | ||
"", | ||
[ | ||
"{{resolve:secretsmanager:", | ||
!Ref postgreswebAuroraSecret, | ||
":SecretString:username}}", | ||
], | ||
] | ||
MasterUserPassword: | ||
!Join [ | ||
"", | ||
[ | ||
"{{resolve:secretsmanager:", | ||
!Ref postgreswebAuroraSecret, | ||
":SecretString:password}}", | ||
], | ||
] | ||
DatabaseName: !Ref postgreswebDBName | ||
Engine: "aurora-postgresql" | ||
EngineVersion: "16.2" | ||
EnableHttpEndpoint: true # enable the Data API feature | ||
DBClusterParameterGroupName: !Ref postgreswebDBClusterParameterGroup | ||
DBSubnetGroupName: !Ref postgreswebDBSubnetGroup | ||
Port: 5432 | ||
VpcSecurityGroupIds: | ||
- !Ref postgreswebDBClusterSecurityGroup | ||
ServerlessV2ScalingConfiguration: | ||
# Replace "All" below with "!Ref Env" to set different autoscaling limits per environment. | ||
MinCapacity: | ||
!FindInMap [postgreswebEnvScalingConfigurationMap, All, DBMinCapacity] | ||
MaxCapacity: | ||
!FindInMap [postgreswebEnvScalingConfigurationMap, All, DBMaxCapacity] | ||
postgreswebDBWriterInstance: | ||
Metadata: | ||
"aws:copilot:description": "The postgresweb Aurora Serverless v2 writer instance" | ||
Type: "AWS::RDS::DBInstance" | ||
Properties: | ||
DBClusterIdentifier: !Ref postgreswebDBCluster | ||
DBInstanceClass: db.serverless | ||
Engine: "aurora-postgresql" | ||
PromotionTier: 1 | ||
AvailabilityZone: !Select | ||
- 0 | ||
- !GetAZs | ||
Ref: AWS::Region | ||
|
||
postgreswebSecretAuroraClusterAttachment: | ||
Type: AWS::SecretsManager::SecretTargetAttachment | ||
Properties: | ||
SecretId: !Ref postgreswebAuroraSecret | ||
TargetId: !Ref postgreswebDBCluster | ||
TargetType: AWS::RDS::DBCluster | ||
Outputs: | ||
postgreswebSecret: # injected as POSTGRESWEB_SECRET environment variable by Copilot. | ||
Description: "The JSON secret that holds the database username and password. Fields are 'host', 'port', 'dbname', 'username', 'password', 'dbClusterIdentifier' and 'engine'" | ||
Value: !Ref postgreswebAuroraSecret | ||
postgreswebSecurityGroup: | ||
Description: "The security group to attach to the workload." | ||
Value: !Ref postgreswebSecurityGroup |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting a small change I made to how the cert bundle is added to the CDRC image:
This ultimately was because of a change related to the Buildkit caching, where I wasn't copying the entire source directory in anymore during Docker build. That caused some cascading issues with the runtime directory being different from e.g.
/cdt/app
, and one of the problems was this cert bundle couldn't be found by Django.This solves it by not making Django have to construct the path, instead it happens at Docker build time and is baked into the image as an env var.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! If it's ok, I'll add this small change to #132 which is about implementing Buildkit caching.