In theory, yes you can, but there's some challenges ahead if you go down this path.
To restore the backup from S3 to postgresql: You will have to bundle the pg_restore
or psql
binaries into your Lambda deployment package. Your Lambda's code will then have to either download the backup S3 file to your Lambda's /tmp
folder (watch out for Lambda's limit of 512 MB for the /tmp
folder), or stream the S3 file's content to pg_restore
/psql
via stdin. To invoke psql
or pg_restore
, you should use Python's subprocess module. Be careful with large backups not to load the whole backup file in memory: you could exceed Lambda's RAM limit.
To query the database, you'll want to include psycopg2 in your Lambda's deployment package. See https://github.com/jkehler/awslambda-psycopg2 for details on how to do that.
To send the data via email, you should look into using AWS SES.
Personally, I would probably use a Docker container to achieve that (using AWS ECS or Batch). This way, it will probably be easier to install the necessary binaries (pg_restore
/psql
, psycopg2). Also, you will avoid Lambda's inherent limitations (15 minutes execution time limit, max /tmp
size, RAM limit).