🎁

12 Days of Baddass Courses sale! Get instant access to the entire egghead library of courses and lessons for 58% off.

Runs out in:
15 : 06 : 07 : 44
Join egghead, unlock knowledge.

Want more egghead? It's 58% off for a limited time only!

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock All Content for 40% Off
1×
Become a member
to unlock all features
Autoplay

    Deploy an AWS Lambda to retrieve a record from DynamoDB with the Serverless Framework

    Nik GrafNik Graf
    awsAWS
    javascriptJavaScript
    nodeNode.js

    To retrieve a previously created item from a DynamoDB table we have to setup the IAM permissions for the Lambda to get an item and further implement the code using the DynamoDB DocumentClient API.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: To retrieve an item from the DynamoDB table, we create a new function, getTodo, and we attach an HTTP event. This time, our path actually should include a path parameter. Using curly braces, we can make it happen.

    The event object then passed to the function will include this parameter, d. Next up, we also need to give the function permissions to actually retrieve an item from DynamoDB. Therefore, we add DynamoDB getItem.

    Be aware that with this configuration, every function we deploy in this stack will have the same permissions. To lock them down, you need to create custom confirmation EM permissions. Since this isn't that convenient in serverless.yml, we're going to skip it for now.

    Our configuration is complete, and we can move on to the code part. We create the file, get JS, and fill in our function.

    As previously mentioned, we can access the key ID and get it from the path parameters inside the event. Then we get the result. In case we found an item, we're going to return a response with the status code 200. In case no item was found, we return a 404 with an error message.

    Everything is in place, and we can redeploy with our new function.

    Once the deploy was successful, we can retrieve the to-do we previously created.

    In the case we provide an ID from an entry that doesn't exist, we get a 404, as expected. Keep in mind, as an alternative, we can still invoke the function using SLS invoke. This comes in especially handy in case you added logging statements for debugging.