API endpoint to update(PUT) a word

Part ɪ: Building Serverless Api│Story 09: Create endpoint to GET word by id

GIT : Repo | Branch | PR

In the previous post, we added a lambda function and set up the endpoint to our serverless API to get a word by its id.

In this post, we would add an endpoint to our API to update the word record.

📄 create a file update.ts under src/modules/words:👇
(here is the code gist)

click over image to zoom (Github code gist)

Add the API Endpoint function in serverless.yml

Next, configure the API endpoint to map to the above function we added.
In serverless.yml, add a updateWord function in the functions block as shown below:👇

This is similar to the as the previous endpoint functions that we added in the previous sections.

👆 We added a new function updateWord.
events: — http, method:put ⇒ This means the event to trigger this function is http put.
path: words/{id} ⇒ The endpoint would have the path /words/{id},
handler: src/modules/words/update.main ⇒ the request would be handled at the main function in the update.ts file (we added above).
cors: true ⇒ to support CORS as the frontend would be on a different domain.
authorier: aws_iam ⇒ this means the access to the API is restricted as per the IAM permissions of the federated identity that the user has been authorized with. We will see how exactly this would work when we would set up an Identity Pool from AWS Cognito service.

Add mock data to test the service locally

From your AWS Console →DynamoDB → tables →words-dev table → Items, note down the wordId and userID of the Item/record that we have posted to the table in the last part when testing the create-word API.

With this id, set the mock data, create a file named update.json under mocks/words with below data:👇

With the mock data ready, let’s test our lambda function

Deploy the service to AWS

Great, so we got endpoint to update a word. Next, let’s add the endpoint to delete a word.

Prev: Get Word by Id🏠Next: Delete word

--

--