DEV Community

Cover image for Getting started with AWS serverless - Databases

Getting started with AWS serverless - Databases

Pierre Chollet on March 01, 2023

In the last article, I covered the basics of creating Lambda functions on AWS, using the CDK. In this article, I will cover how to store data in a ...
Collapse
 
recursivecodes profile image
Todd Sharp • Edited

Just a small suggestion - when creating an API, it's better to include details as to why validation failed instead of just returning a generic error message. At the very least, this code:

if (userId === undefined || noteId === undefined) {
  return {
    statusCode: 400,
    body: 'bad request',
  };
}
Enter fullscreen mode Exit fullscreen mode

Would be vastly improved by this simple change:

if (userId === undefined || noteId === undefined) {
  return {
    statusCode: 400,
    body: 'You must pass a userId or noteId to getNote',
  };
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
pchol22 profile image
Pierre Chollet

Indeed! I tried to keep it simple in these examples. To go even further, a better typing should be used instead of hard coding the input and output types. I plan to cover the use of contracts one day

Collapse
 
kkob03 profile image
kob.xrd

Shouldn't the handler function return a promise?

Collapse
 
pchol22 profile image
Pierre Chollet

Yes, but it does here -> async functions that contain an "await" statement automatically return a promise, even if it ins't stated explicitly.

If you write the code and check the return type, you will see that it's a promise

Collapse
 
mac10046 profile image
Abdeali

How did you deploy the code in required lambda using cdk code.. would like to know more on those points as well. Thanks

Collapse
 
pchol22 profile image
Pierre Chollet

I covered the subject with more details in the previous article of the series. Basically the Nodejsfunction construct allows you to write code in a separate file and will handle deployment for you !

Collapse
 
adelegauvrit profile image
Adèle Gauvrit

I wished someone had taught me how to partionned a DynamoDB when I started

Collapse
 
pchol22 profile image
Pierre Chollet

Partitioning a DB is easier said than done 😩

Collapse
 
alexandreperni4 profile image
Alexandre Pernin

classic use case, it's super valuable to have these code examples, thanks for sharing!

Collapse
 
pchol22 profile image
Pierre Chollet

Thank you! I’m glad you found this useful 🤗

Collapse
 
eichgi profile image
Hiram

Excellent content. I tried the homework, DELETE was actually the same than GET. Update and creation might rely on the existence of noteId to use or create a new uuid 😎