How to use PostgreSQL in your serverless app

Link to chapter — https://serverless-stack.com/examples/how-to-use-postgresql-in-your-serverless-app.html

Trying to use Postgres RDS with SST. Added the following but now it won’t deploy with npx sst deploy. Get the following error.

npm i pg

var Native = require('pg-native')
  ╵                      ~~~~~~~~~~~

There was a problem transpiling the Lambda handler.

Is SST meant to be used with RDS? If so, does Webpack or ESBuild need to be installed in order to exclude pg-native?

Yeah I recall something like this was brought up in: Slack

You’ll likely need to exclude it from bundling in esbuild: Function | Serverless Stack (SST)

I followed this example and I started getting billed more than I expected, about $25 for two weeks. It all seems to be coming from the “$0.045 per NAT Gateway Hour” line item on my bill, which I believe is related to the VPC needed for the Aurora DB cluster.

I’m doing sporadic development right now with no real users. While it’s not a lot of money, is there a simple way to turn off the VPCs when I’m not using them? If possible, I’d rather not lose the data in the Aurora table in the process.

Yeah we are going to update this example to use the new RDS construct: Update PostgresQL example to use RDS construct by Manitej66 · Pull Request #655 · AnomalyInnovations/serverless-stack-com · GitHub

It removes the need to use the NAT Gateway.

That’s awesome! You all are the best :slight_smile: I’ve subscribed to the pull request.

After these instructions were updated to use the new RDS construct, I ran into one small problem. I was getting a “TypeError: sst3.RDS is not a constructor” error message.

In case anyone else runs into this problem, all it took was updating the CLI to fix the problem. Everything else went smoothly.

I’m running through the tutorial, and at the “Deploying to Production” step I’m getting the following error when I run npx sst deploy --stage prod

 prod-tiplikeido-my-stack failed: Properties validation failed for resource ClusterMigrationFunctionC400958E with message:
#/Handler: expected maxLength: 128, actual: 134

I think this is referring to trying to create a Lambda function but the name is too long. I’m not seing this problem earlier, because I think “prod” stage name is longer than my “dev” stage name, and I’m guessing the one extra character is tipping it over the limit.

Any suggestions for workarounds or how to debug this deeper?

EDIT - Using sst build --stage prod I’m able to see that I think this is being caused by a long path to my handler. I’m not sure why Dev worked, but this looks to be unrelated to this tutorial (which is really good BTW!).

This example no longer works, returning:

ExampleStack Cluster/Cluster AWS::RDS::DBCluster CREATE_FAILED Resource handler returned message: "The engine mode serverless you requested is currently unavailable. (Service: Rds, Status Code: 400, Request ID: 1234)" (RequestToken: 1234, HandlerErrorCode: InvalidRequest)

After reading your “Moving away from CDK” blog post, I understand that fixing this issue won’t be worth anyone’s time.

I’ll have a look at the Ion-based setup.
Hope this comment spared someone time before going to deep debugging it :wink: