This script fetches Redash query results and sends them via email as CSV attachment.
- You only need to run this once for this repository on your machine:
- Follow the steps described in our Notion docs for setting up
pyenvon MacOS for development here, make sure you follow step 3 (Setting up repositories with pyenv) with Python 3.12. - Confirm that this worked by checking that this command outputs a correct venv path
echo $VIRTUAL_ENV - If the command above doesn't yield any output, then something is not configured correctly, so you will need to get help from another tech team member.
- Follow the steps described in our Notion docs for setting up
- Run
pip install -r requirements.txt - Ensure you have a development AWS credential set up, preferably with
aws-vault. - Additional setup required for new triggers:
- Each query id needs a corresponding secrets manager entry. Add it to the
redash-emailersecret:- key:
{query number}_REDASH_QUERY_KEY - value: {Redash query API key}
- key:
- The Redash query needs to set to a recurring schedule so that the values are refreshed. This repo only grabs the last results, but it doesn't refresh the data on-demand.
- Each query id needs a corresponding secrets manager entry. Add it to the
- See the test event in the AWS console.
- You can also test the script manually using Query 13032 and your email address. Query 13032 does not return any sensitive data:
python redash_emailer.py --query_id 13032 --to '<your-email-here>' --from '<your-email-here>'- Run
python redash_emailer.py -hto get input options.
You can set the recipient with either --to on the command line, or TO_ADDRESS in settings.py. In either case, if the recipient value is not an email address (containing @), it will be assumed to be the name of a column in the Redash query results, and that column must contain email addresses. This allows more complex workflows in which the query itself determines who receives which records.