This is really problematic advice. In practice it is not remotely scaleable to have every lambda have it’s own Postgres connection. Of course it works great in these tiny examples of 20 requests at a time, but try making 2,000 requests at a time and it’s a different story. We ended up migrating all our lambda functions that connect to Postgres into Fargate containers and it scaled to hundreds of times what the lambda version could handle.
I’d be interested to know if anybody has tried using https://aws.amazon.com/rds/proxy/ as that didn’t exist when we were trialing lambda.
On an unrelated note. I think all your examples have SQL Injection vulnerabilities, because you are using string formatting, instead of passing the query and parameters separately to the database engine. I could be wrong though as I’m not super familiar with the programming language you are using.