Creating branded herokuapp.com-like domains using Cloudflare, based on the app name (eg my-app-prod.example.com).
- Set records for domains which don't exist
- Update records which are set incorrectly
- Delete records which aren't referenced any more in Heroku
- Enable / refresh ACM when an app has its domain updated.
Install the dependencies listed in requirements.txt, ideally into a virtual environment.
Set some environment variables:
CLOUDFLARE_API_TOKEN: A Cloudflare API token, with access to the zone you wish to edit. "DNS Edit" is required for those zones.HEROKU_API_KEY: API key from HerokuCLOUDFLARE_ZONE_ID: The Cloudflare zone id of the domain to automatically create
Optionally:
APP_NAME: A regex of app names to act on. Any not matching this will be skipped.HEROKU_TEAMS: A comma separated list of Heroku teams to operate on. By default will use all apps the account has access to.ALLOWED_CNAME_TARGETS: A comma-separated list of regexes which match CNAMEs. If these CNAMEs are found in place of the correct Heroku CNAME, they won't be overridden.LOG_LEVEL: Log level to use (default "INFO").DRY_RUN: Whether to perform actions, or just say they happened (Eithertrueorfalse(default)).
These can also be set in a .env file.
Then, simply run the main.py. To have the application loop for you, specify an interval in seconds with $INTERVAL.
In some hosting environments, it may not be possible to run the container as a background job (eg Cloud Run).
To account for this, the default container wraps the command in webhook.
To trigger the hook, send a GET request to /hooks/trigger. The webhook is protected by a token, which can be set using $WEBHOOK_TOKEN, and should be sent in the X-Webhook-Token header.
To run in a loop instead of a webhook, set $INTERVAL in the container.
Alternatively, it's possible to run on Heroku using its scheduler. Just add a job for /app/main.py, and stop the web processes.