Let’s run the Django App in production on AWS.

AWS Setup

1

Update Credentials

To run on AWS, you need one of the following:

  1. The ~/.aws/credentials file with your AWS credentials
  2. or AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY environment variables

To create the credentials file, install the aws cli and run aws configure

2

Update region and subnets

Add 2 subnets to the workspace/settings.py file (required for ECS services)

workspace/settings.py
ws_settings = WorkspaceSettings(
    ...
    # -*- AWS settings
    # Add your Subnet IDs here
    subnet_ids=["subnet-xyz", "subnet-xyz"],
    ...
)

Please check that the subnets belong to the selected aws_region

Update Secrets

1

RDS database password

Update the RDS database password in workspace/secrets/prd_db_secrets.yml

workspace/secrets/prd_db_secrets.yml
# Secrets used by RDS Database
MASTER_USERNAME: app
MASTER_USER_PASSWORD: "app9999!!"
2

App secrets

Add any other secrets used by your app to workspace/secrets/prd_app_secrets.yml

workspace/secrets/prd_app_secrets.yml
SECRET_KEY: "django-insecure-...."
# OPENAI_API_KEY: "sk-***"

Create AWS resources

Create AWS resources using:

This will create:

  1. ECS Cluster for the application.
  2. ECS Task Definitions and Services that run the application on the ECS cluster.
  3. LoadBalancer to route traffic to the application.
  4. Security Groups that control incoming and outgoing traffic.
  5. Secrets for managing application and database secrets.
  6. RDS Database for Knowledge Base and Storage.

Press Enter to confirm and grab a cup of coffee while the resources spin up.

  • The RDS database takes about 5 minutes to activate.
  • These resources are defined in the workspace/prd_resources.py file.
  • Use the ECS console to view services and logs.
  • Use the RDS console to view the database instance.

Production Django app

Open the LoadBalancer DNS to view your Django App running on AWS.

Production Django Admin

Open the LoadBalancer DNS + /admin page to view the Django admin site.

Create an admin user by SSH-ing into the production container:

ECS_CLUSTER=django-prd
TASK_ARN=$(aws ecs list-tasks --cluster django-prd --query "taskArns[0]" --output text)
CONTAINER_NAME=django-prd

aws ecs execute-command --cluster $ECS_CLUSTER \
    --task $TASK_ARN \
    --container $CONTAINER_NAME \
    --interactive \
    --command "python manage.py createsuperuser"

Log in to the admin panel:

Update Production

Follow this guide to update your production application. You'll need to:

  1. Create a new image
  2. Update the ECS Task Definition and Services.

Delete AWS resources

Play around and then delete AWS resources using:

or delete individual resource groups using:

Next

Congratulations on running your Django App on AWS. Next Steps: