Challenge 1: Create a simple app in Lambda + DynamoDB + Serverless Framework
Tags: aws, tutorial, serverless, challenge
Published: 2021-04-20 16:42:23

*This article was also presented as [a talk last July 17, 2021 for ULAP.org](https://www.youtube.com/watch?v=SzFO1WXz140&t=4s)*

## About the challenge

A few of my developer friends and colleagues at work asked me how they can learn more about AWS. I told them that the best way to learn is:

- To do hands-on experience
- Learn something that has close parallels to what you're doing
- Solve a well-defined problem that you have to research the answer for instead of handing it to you.

Hence, the serverless challenge was born. The challenge is best suited for people who have some experience in development but have not necessarily touched up on the latest trend of serverless computing. 

## Situation

You are the dev lead (aka 'the only dev') of a chain of restaurants. To increase sales, your boss wants you to create a simple loyalty application. He has heard of the Serverless Framework and how it allows your team to use serverless technologies like Lambda to deploy your application. Your boss is particularly sensitive to cost. Since Lambda only charges you when the code is run (when someone uses your app), your boss is thrilled to introduce the technology.

## Specifications

Create 3 Lambda functions for each endpoint:
- POST /loyalty-cards: create a loyalty card
- GET /loyalty-cards/10: display the loyalty card you just created using the id
- GET /loyalty-cards: display all loyalty cards

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwwh1i7x5jrhk15ijos0.png)

You must follow the guidelines below:
- The Lambda function must be deployed using the Serverless Framework (SF). SF is a tool that makes deploying Lambda functions easier. You no longer have to package dependencies and upload code to Lambda manually. All you need to do is run "serverless deploy".
- Your data must persist, and your database must be DynamoDB. DynamoDB is a NoSQL database service by AWS. Unlike traditional relational databases, DynamoDB does not maintain a schema. Except for the primary key attributes (which have to be unique for each record), you can add different attributes for different items in the table.
    - NodeJS - https://aws.amazon.com/sdk-for-javascript/


## Resources

The resources here will certainly help you get started with this challenge. But it won't be the only ones you'll use. You will have to research some answers.

#### Set up your AWS CLI

- Create your own AWS account (if you don't have access to one). See the [AWS Tutorial](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) on how to do this.
- Setup AWS CLI in your local:  https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html

#### Serverless in AWS

- Hello World in Lambda - your first Lambda function deployed using Serverless Framework
    - NodeJS: https://www.serverless.com/framework/docs/providers/aws/examples/hello-world/node/
    - Python: https://www.serverless.com/framework/docs/providers/aws/examples/hello-world/python/
- Creating Rest APIs
    - NodeJS: https://www.serverless.com/blog/node-rest-api-with-serverless-lambda-and-dynamodb
    - Python: https://www.serverless.com/examples/aws-python-rest-api-with-dynamodb/

## Useful Hints

1. Use `serverless` to initialize a project
2. Use `serverless deploy` to deploy your project. A link to API Gateway will be displayed after deployment. Use that link to test in Postman
3. At this point, the only way to test your code is to keep deploying using `serverless deploy` and using APIGW to test.
4. To debug, leave print messages in your code and find the log group for the lambda function. It is under cloudwatch > logs > log groups (https://console.aws.amazon.com/cloudwatch/home?region=us-east-1#logsV2:log-groups)
5. If you are wondering why we have to test by deploying your function every time and test via APIGW and if there's a better way, you are right. The better way is to use serverless offline and serverless invoke. You can find them at the [third challenge](https://dev.to/awscommunity-asean/challenge-3-using-offline-tools-to-speed-up-dev-in-serverless-2hp8)
6. If you really are a bit lost, you can see the [Github repository](https://github.com/jamby1100/serverless-challenge-loyalty-card-api) of the code I did as I made this article.

## Show off your work!

Comment a screencap of your work below. Or better yet, create a blog post here in dev.to explaining how you did it. 

If you have any questions or are stuck somewhere, comment below or send me a pm, and I'd be happy to help you.

Photo by <a href="https://unsplash.com/@avirichards?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Avi Richards</a> on <a href="https://unsplash.com/s/photos/computer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>