johnrjj / nextjs-aws-dynamodb

Example Next.js app for CRUD actions in DynamoDB.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Next.js + AWS DynamoDB

This is an example of a Next.js application using DynamoDB for creating, updating, and deleting documents.

Getting Started

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.

Deploy with Vercel

Option 2: Create a new table.

  1. Create a new IAM role with permission for AmazonDynamoDBFullAccess and AWSCloudFormationFullAccess.
  2. Save the access key and secret key.
  3. Install the AWS CLI and run aws configure.
  4. Install the AWS CDK: npm i -g aws-cdk.
  5. This will prompt you to enter the access key and secret key.
  6. Create an .env.local file similar to .env.local.example.
  7. Add the access key and secret key to .env.local.
  8. Run cdk deploy to create a new table Items.
  9. View the newly created table and copy the name to .env.local.
  10. Run yarn dev to start the Next app at localhost:3000.

Testing

// 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-8ee29bd0686f

Commands

  • yarn dev – Starts the Next.js app at localhost:3000.
  • cdk deploy – Deploy this stack to your default AWS account/region
  • cdk diff – Compare deployed stack with current state
  • cdk synth – Emits the synthesized CloudFormation template

About

Example Next.js app for CRUD actions in DynamoDB.


Languages

Language:JavaScript 100.0%