In the previous part, we added a lambda function and set up the endpoint to our serverless API to update a word.
In this part, we would add an endpoint to our API to delete a word from the dynamoDB table.
📄 Create a file named delete.ts
under src/modules/words
:👇
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 deleteWord
function in the functions
block as shown below:👇
functions:
createWord:
...
...
...
deleteWord:
handler: src/modules/words/delete.main
events:
- http:
path: words/{id}
method: delete
cors: true
authorizer: aws_iam
👆 We added a new function deleteWord
.events: — http, method:delete
⇒ This means the event to trigger this function is http delete
. path: words/{id}
⇒ The endpoint would have the path /words/{id}
, handler: src/modules/words/delete.main
⇒ the request would be handled at the main
function in the delete.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 delete.json
under mocks/words
with below data:👇
With the mock data ready, let’s test our lambda function
$ serverless invoke local --function deleteWord --path mocks/words/delete.json --stage dev
Deploy the service to AWS
$ npm run devDeploy
👏 Congratulations!! Our serverless API is ready now. We got all the endpoints for our API deployed for doing CRUD operations with the vocabWords
dynamoDB table.
Note down the URL prefix of the endpoints, we would need this later when making calls to API from UI
📌 🅝🅞🅣🅔 API Endpoints URL prefix:https://c5wrtyr85s6.execute-api.us-east-1.amazonaws.com/dev
This concludes the first section of the series - Building Serverless API.
Next, we would start with building the frontend/UI of our application.
← Prev: Update a word
┈ 🏠 ┈ Next: Part II
→