This is an example of a Next.js application using DynamoDB for creating, updating, and deleting documents.
Option 1: Use an existing table.
Retrieve your existing access key, secret key, region and table name. Provide those values after clicking "Deploy" to automatically set the environment variables.
Option 2: Create a new table.
- Create a new IAM role with permission for
AmazonDynamoDBFullAccessandAWSCloudFormationFullAccess. - Save the access key and secret key.
- Install the AWS CLI and run
aws configure. - Install the AWS CDK:
npm i -g aws-cdk. - This will prompt you to enter the access key and secret key.
- Create an
.env.localfile similar to.env.local.example. - Add the access key and secret key to
.env.local. - Run
cdk deployto create a new tableItems. - View the newly created table and copy the name to
.env.local. - Run
yarn devto start the Next app atlocalhost:3000.
// Create
$ curl -X PUT http://localhost:3000/api/item -d '{"content": "test"}' -H "Content-type: application/json"
// Read
$ curl http://localhost:3000/api/item\?id\=bdc38386-2b35-47a3-bdfc-8ee29bd0686f
// Update
$ curl -X POST http://localhost:3000/api/item -d '{"content": "updated", "id": "bdc38386-2b35-47a3-bdfc-8ee29bd0686f"}' -H "Content-type: application/json"
// Delete
$ curl -X DELETE http://localhost:3000/api/item\?id\=bdc38386-2b35-47a3-bdfc-8ee29bd0686fyarn dev– Starts the Next.js app atlocalhost:3000.cdk deploy– Deploy this stack to your default AWS account/regioncdk diff– Compare deployed stack with current statecdk synth– Emits the synthesized CloudFormation template