First, checkout session-2 branch, and deploy the ressources for session-2.
git checkout session-2-v2
cd backend
sls deploy
You can then open:
- Cloudformation interface to check out the ressources that have been created
- API Gateway interface to check out your API and your routes.
- AWS Lambda to check out your lambdas
- AWS Cloudwatch to check out their execution logs (log stream tab)
If you haven't done it yet, in the front-end folder, copy-paste .env.development as .env.development.local and replace the httpApiId by yours (you can find it in API Gateway), and run yarn and yarn start.
Then, in the back-end folder, notice the new dynamodb.yml resource in the resources folder. Notice its TableName, AttributeNames and KeySchema.
To deploy it:
- Add the dynamo-db table as a ressource in the
serverless.ts(end of file) - Give your lambdas the IAM rights to
Query,PutandDeleteItems on this table by uncommenting the correct blocks in theserverless.ts. - Deploy your stack
- Go to DynamoDB service to check your newly created table. Notice the default item attributes:
partitionKeyandsortKeyfrom the yml ressource.
We will use the AWS SDK to interact with this table:
import { DynamoDB } from "aws-sdk";Now, it's your turn:
- Open the AWS dynamoDb DocumentClient documentation to learn how to use its
query,putanddeletemethods. - Implement the get route to get Virus items instead of hard-coded ones.
- Create a post route that adds a Virus item to the db by adding a trigger to the
createViruslambda and call it in the frontend in the addVirus button onClick. - Implement the delete route and call it in the frontend in viruses onClicks.
Pro tips:
- If you want to deploy only one function, use
yarn deploy -f <your-function-name>. It is much faster (~10s) than deploying a stack (~30s) but will only deploy the function's code, not any config change in yourserverless.ts. - To call locally one function, use
serverless invoke -f <your-function-name> --path path/to/mocked-event.json
Done ? Nice work ! Don't forget to kill your stack by running serverless remove in the backend folder !
To see final result, checkout the start of session 3.