API endpoint to update(PUT) a word
Part ɪ: Building Serverless Api│Story 09: Create endpoint to GET word by id
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)
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:👇
functions:
createWord:
...
...
...
updateWord:
handler: src/modules/words/upate.main
events:
- http:
path: words/{id}
method: put
cors: true
authorizer: aws_iam
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
$ serverless invoke local --function updateWord --path mocks/words/update.json --stage dev
Deploy the service to AWS
$ npm run devDeploy
Great, so we got endpoint to update a word. Next, let’s add the endpoint to delete a word.