Skip to main content

Helper functions to run Django and Flask applications in AWS Copilot/ECS.

Project description

DBT Copilot Python

A set of utility functions for running Django & Flask apps in AWS ECS via AWS Copilot.

Using dbt-copilot-python

Supported Python versions

3.9, 3.10, 3.11, 3.12 and 3.13.

Installation

pip install dbt-copilot-python

With celery

If you are using celery, you should also install an appropriate version of celery using your project's package manager, or use our built-in extra.

# Poetry
poetry add celery

# Pip
pip install celery
pip freeze | grep "^celery==" >> requirements.txt

# Pipenv
pipenv install celery

OR

# Poetry
poetry add dbt-copilot-python[celery]

# Pip
pip install dbt-copilot-python[celery]
pip freeze | grep "^celery==" >> requirements.txt

# Pipenv
pipenv install dbt-copilot-python[celery]

Usage

ALLOWED_HOSTS setting

Add the ECS container IP to ALLOWED_HOSTS setting so that the Application Load Balancer (ALB) healthcheck will succeed:

from dbt_copilot_python.network import setup_allowed_hosts

ALLOWED_HOSTS = [...]

ALLOWED_HOSTS = setup_allowed_hosts(ALLOWED_HOSTS)

Celery health check

Install the library with celery.

Add the health check in your application's Celery config file...

from dbt_copilot_python.celery_health_check import healthcheck

celery_app = Celery("application_name")
...

celery_app = healthcheck.setup(celery_app)

Add the health check to the Celery worker service in docker-compose.yml...

healthcheck:
  test: [ "CMD-SHELL", "python -m dbt_copilot_python.celery_health_check.healthcheck" ]
  interval: 10s
  timeout: 5s
  retries: 2
  start_period: 5s

In your *-deploy codebase, add the health check to the Celery worker service in copilot/celery-worker/manifest.yml...

healthcheck:
  command: [ "CMD-SHELL", "launcher bash -c 'python -m dbt_copilot_python.celery_health_check.healthcheck'" ]
  interval: 10s
  timeout: 5s
  retries: 2
  start_period: 10s

DATABASES setting

To configure the DATABASES setting from an RDS JSON object stored in AWS Secrets Manager, there are two options.

  1. Configure the DATABASES setting to use a database URL (recommended):

    Note: This is dependent on the dj-database-url package which can be installed via pip install dj-database-url.

    import dj_database_url
    
    from dbt_copilot_python.database import database_url_from_env
    
    DATABASES = {
        "default": dj_database_url.config(
            default=database_url_from_env("DATABASE_ENV_VAR_KEY")
        )
    }
    
  2. Configure the DATABASES setting to use a dictionary containing the settings:

    from dbt-copilot-python.database import database_from_env
    
    DATABASES = database_from_env("DATABASE_ENV_VAR_KEY")
    

Contributing to dbt-copilot-python

Requirements

Install dependencies & pre-commit hooks

poetry install && poetry run pre-commit install

Run the tests

poetry run pytest

Publishing

To publish the Python package dbt-copilot-python, you will need an API token.

  1. Acquire API token from Passman.
    • Request access from the SRE team.
    • Note: You will need access to the platform group in Passman.
  2. Run poetry config pypi-token.pypi <token> to add the token to your Poetry configuration.

Update the version, as the same version cannot be published to PyPi.

poetry version patch

More options for the version command can be found in the Poetry documentation. For example, for a minor version bump: poetry version minor.

Update the CHANGELOG.md with details of the changes you've made.

Build the Python package.

poetry build

Publish the Python package.

Note: Make sure your Pull Request (PR) is approved and contains the version upgrade in pyproject.toml before publishing the package.

poetry publish

Check the PyPi Release history to make sure the package has been updated.

For an optional manual check, install the package locally and test everything works as expected.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbt_copilot_python-2.0.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbt_copilot_python-2.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file dbt_copilot_python-2.0.0.tar.gz.

File metadata

  • Download URL: dbt_copilot_python-2.0.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.11.5 Darwin/24.5.0

File hashes

Hashes for dbt_copilot_python-2.0.0.tar.gz
Algorithm Hash digest
SHA256 3eba8713705094d422d6e65ca1f30ac79ba71be1977c29f028bd03ce89ebefed
MD5 467de2b1b467e505de03e731806371cb
BLAKE2b-256 a474bc7dc3e8a1b704eaca81910c0d3ab383db0f6f4dda40cba91b368037cd9a

See more details on using hashes here.

File details

Details for the file dbt_copilot_python-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dbt_copilot_python-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6497aec1b0ae95fed3f3a34a75f57abe7b897e0220b291ab5898d3f2b546e858
MD5 968080d7d24d074e46c3457cb3f7d61a
BLAKE2b-256 572a81a546ba64a7f2b98db879fda38e630906945a7a61b677fd401d0212c443

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page