From 8c79e07ba263adae3be1371bd8f1dfef07b6d152 Mon Sep 17 00:00:00 2001 From: Adam Jones Date: Mon, 5 Feb 2024 23:40:43 +0000 Subject: [PATCH] Fix serverless-dynamodb examples --- aws-node-express-dynamodb-api/README.md | 10 ++++----- aws-node-http-api-dynamodb-local/.gitignore | 1 + aws-node-http-api-dynamodb-local/README.md | 8 +++---- .../db-seeds/todos.json | 16 ++++++++++++++ .../offline/migrations/todos.json | 21 ------------------- aws-node-http-api-dynamodb-local/package.json | 8 +++---- .../serverless.yml | 16 +++++++++----- .../.gitignore | 1 + .../README.md | 18 +++++++--------- .../db-seeds/todos.json | 16 ++++++++++++++ .../offline/migrations/todos.json | 21 ------------------- .../package.json | 8 +++---- .../serverless.yml | 20 +++++++++++------- aws-python-flask-dynamodb-api/README.md | 8 +++---- aws-ruby-sinatra-dynamodb-api/README.md | 12 +++++------ 15 files changed, 92 insertions(+), 92 deletions(-) create mode 100644 aws-node-http-api-dynamodb-local/db-seeds/todos.json delete mode 100644 aws-node-http-api-dynamodb-local/offline/migrations/todos.json create mode 100644 aws-node-rest-api-with-dynamodb-and-offline/db-seeds/todos.json delete mode 100644 aws-node-rest-api-with-dynamodb-and-offline/offline/migrations/todos.json diff --git a/aws-node-express-dynamodb-api/README.md b/aws-node-express-dynamodb-api/README.md index 28304957b..95b2c5376 100644 --- a/aws-node-express-dynamodb-api/README.md +++ b/aws-node-express-dynamodb-api/README.md @@ -84,10 +84,10 @@ If you try to retrieve user that does not exist, you should receive the followin ### Local development -It is also possible to emulate DynamoDB, API Gateway and Lambda locally using the `serverless-dynamodb-local` and `serverless-offline` plugins. In order to do that, run: +It is also possible to emulate DynamoDB, API Gateway and Lambda locally using the `serverless-dynamodb` and `serverless-offline` plugins. In order to do that, run: ```bash -serverless plugin install -n serverless-dynamodb-local +serverless plugin install -n serverless-dynamodb serverless plugin install -n serverless-offline ``` @@ -95,7 +95,7 @@ It will add both plugins to `devDependencies` in `package.json` file as well as ``` plugins: - - serverless-dynamodb-local + - serverless-dynamodb - serverless-offline ``` @@ -134,6 +134,6 @@ After that, running the following command with start both local API Gateway emul serverless offline start ``` -To learn more about the capabilities of `serverless-offline` and `serverless-dynamodb-local`, please refer to their corresponding GitHub repositories: +To learn more about the capabilities of `serverless-offline` and `serverless-dynamodb`, please refer to their corresponding GitHub repositories: - https://github.com/dherault/serverless-offline -- https://github.com/99x/serverless-dynamodb-local +- https://github.com/raisenational/serverless-dynamodb diff --git a/aws-node-http-api-dynamodb-local/.gitignore b/aws-node-http-api-dynamodb-local/.gitignore index e0f039d9d..48da7f814 100644 --- a/aws-node-http-api-dynamodb-local/.gitignore +++ b/aws-node-http-api-dynamodb-local/.gitignore @@ -1,2 +1,3 @@ .serverless node_modules +.dynamodb \ No newline at end of file diff --git a/aws-node-http-api-dynamodb-local/README.md b/aws-node-http-api-dynamodb-local/README.md index c1a89aec4..f6d749ef0 100644 --- a/aws-node-http-api-dynamodb-local/README.md +++ b/aws-node-http-api-dynamodb-local/README.md @@ -2,7 +2,7 @@ title: 'AWS Serverless HTTP API with DynamoDB and offline support example in NodeJS' description: 'This example demonstrates how to run a service locally, using the ''serverless-offline'' plugin. It provides an HTTP API to manage Todos stored in DynamoDB.' layout: Doc -framework: v1 +framework: v3 platform: AWS language: nodeJS authorLink: 'https://github.com/adambrgmn' @@ -16,7 +16,7 @@ This example demonstrates how to run a service locally, using the provides an HTTP API to manage Todos stored in a DynamoDB, similar to the [aws-node-http-api-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-http-api-dynamodb) example. A local DynamoDB instance is provided by the -[serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local) +[serverless-dynamodb](https://github.com/raisenational/serverless-dynamodb) plugin. ## Use-case @@ -27,9 +27,7 @@ Test your service locally, without having to deploy it first. ```bash npm install -serverless dynamodb install (or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d) -serverless offline start -serverless dynamodb migrate (this imports schema) +serverless dynamodb install # or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d ``` ## Run service offline diff --git a/aws-node-http-api-dynamodb-local/db-seeds/todos.json b/aws-node-http-api-dynamodb-local/db-seeds/todos.json new file mode 100644 index 000000000..e8deb75bf --- /dev/null +++ b/aws-node-http-api-dynamodb-local/db-seeds/todos.json @@ -0,0 +1,16 @@ +[ + { + "text": "Deploy my first service", + "id": "849fbd50-c926-4e08-9029-d47438b5f487", + "checked": true, + "createdAt": 1707175709304, + "updatedAt": 1707175709304 + }, + { + "text": "Learn Serverless", + "id": "cc6fbe30-c47f-11ee-9a7b-7d80f9b962e5", + "checked": false, + "createdAt": 1707175709241, + "updatedAt": 1707175770849 + } +] diff --git a/aws-node-http-api-dynamodb-local/offline/migrations/todos.json b/aws-node-http-api-dynamodb-local/offline/migrations/todos.json deleted file mode 100644 index aa05790fa..000000000 --- a/aws-node-http-api-dynamodb-local/offline/migrations/todos.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "Table": { - "TableName": "serverless-http-api-dynamodb-local-dev", - "KeySchema": [ - { - "AttributeName": "id", - "KeyType": "HASH" - } - ], - "AttributeDefinitions": [ - { - "AttributeName": "id", - "AttributeType": "S" - } - ], - "ProvisionedThroughput": { - "ReadCapacityUnits": 1, - "WriteCapacityUnits": 1 - } - } -} diff --git a/aws-node-http-api-dynamodb-local/package.json b/aws-node-http-api-dynamodb-local/package.json index 42eeb45f2..4e63a6bb2 100644 --- a/aws-node-http-api-dynamodb-local/package.json +++ b/aws-node-http-api-dynamodb-local/package.json @@ -6,11 +6,11 @@ "author": "Christoph Gysin ", "license": "MIT", "dependencies": { - "uuid": "^2.0.3" + "uuid": "^9.0.1" }, "devDependencies": { - "aws-sdk": "^2.12.0", - "serverless-dynamodb-local": "^0.2.18", - "serverless-offline": "^6.8.0" + "aws-sdk": "^2.1551.0", + "serverless-dynamodb": "^0.2.49", + "serverless-offline": "^13.3.3" } } diff --git a/aws-node-http-api-dynamodb-local/serverless.yml b/aws-node-http-api-dynamodb-local/serverless.yml index be190e27d..0633430cc 100644 --- a/aws-node-http-api-dynamodb-local/serverless.yml +++ b/aws-node-http-api-dynamodb-local/serverless.yml @@ -3,7 +3,7 @@ service: serverless-http-api-dynamodb-local frameworkVersion: "3" plugins: - - serverless-dynamodb-local + - serverless-dynamodb - serverless-offline custom: @@ -13,11 +13,17 @@ custom: start: port: 8000 inMemory: true + # Uncomment if you have a DynamoDB instance already running locally you want to use instead + # noStart: true + # Create the tables according to the resources.Resources block migrate: true - # Comment if you don't have a DynamoDB running locally - noStart: true - migration: - dir: offline/migrations + # Insert seed items on creation, configured by the custom.dynamodb.seed block + seed: true + seed: + default: + sources: + - table: ${self:provider.environment.DYNAMODB_TABLE} + sources: [./db-seeds/todos.json] provider: name: aws diff --git a/aws-node-rest-api-with-dynamodb-and-offline/.gitignore b/aws-node-rest-api-with-dynamodb-and-offline/.gitignore index e0f039d9d..48da7f814 100644 --- a/aws-node-rest-api-with-dynamodb-and-offline/.gitignore +++ b/aws-node-rest-api-with-dynamodb-and-offline/.gitignore @@ -1,2 +1,3 @@ .serverless node_modules +.dynamodb \ No newline at end of file diff --git a/aws-node-rest-api-with-dynamodb-and-offline/README.md b/aws-node-rest-api-with-dynamodb-and-offline/README.md index 920803cf0..54bd5d988 100644 --- a/aws-node-rest-api-with-dynamodb-and-offline/README.md +++ b/aws-node-rest-api-with-dynamodb-and-offline/README.md @@ -2,7 +2,7 @@ title: 'AWS Serverless REST API with DynamoDB and offline support example in NodeJS' description: 'This example demonstrates how to run a service locally, using the ''serverless-offline'' plugin. It provides a REST API to manage Todos stored in DynamoDB.' layout: Doc -framework: v1 +framework: v3 platform: AWS language: nodeJS priority: 10 @@ -17,7 +17,7 @@ This example demonstrates how to run a service locally, using the provides a REST API to manage Todos stored in a DynamoDB, similar to the [aws-node-rest-api-with-dynamodb](https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb) example. A local DynamoDB instance is provided by the -[serverless-dynamodb-local](https://github.com/99xt/serverless-dynamodb-local) +[serverless-dynamodb](https://github.com/raisenational/serverless-dynamodb) plugin. ## Use-case @@ -28,9 +28,7 @@ Test your service locally, without having to deploy it first. ```bash npm install -serverless dynamodb install (or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d) -serverless offline start -serverless dynamodb migrate (this imports schema) +serverless dynamodb install # or to use a persistent docker dynamodb instead, open a new terminal: cd ./dynamodb && docker-compose up -d ``` ## Run service offline @@ -46,7 +44,7 @@ You can create, retrieve, update, or delete todos with the following commands: ### Create a Todo ```bash -curl -X POST -H "Content-Type:application/json" http://localhost:3000/todos --data '{ "text": "Learn Serverless" }' +curl -X POST -H "Content-Type:application/json" http://localhost:3000/dev/todos --data '{ "text": "Learn Serverless" }' ``` Example Result: @@ -57,7 +55,7 @@ Example Result: ### List all Todos ```bash -curl -H "Content-Type:application/json" http://localhost:3000/todos +curl -H "Content-Type:application/json" http://localhost:3000/dev/todos ``` Example output: @@ -69,7 +67,7 @@ Example output: ```bash # Replace the part with a real id from your todos table -curl -H "Content-Type:application/json" http://localhost:3000/todos/ +curl -H "Content-Type:application/json" http://localhost:3000/dev/todos/ ``` Example Result: @@ -81,7 +79,7 @@ Example Result: ```bash # Replace the part with a real id from your todos table -curl -X PUT -H "Content-Type:application/json" http://localhost:3000/todos/ --data '{ "text": "Learn Serverless", "checked": true }' +curl -X PUT -H "Content-Type:application/json" http://localhost:3000/dev/todos/ --data '{ "text": "Learn Serverless", "checked": true }' ``` Example Result: @@ -93,7 +91,7 @@ Example Result: ```bash # Replace the part with a real id from your todos table -curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/todos/ +curl -X DELETE -H "Content-Type:application/json" http://localhost:3000/dev/todos/ ``` No output diff --git a/aws-node-rest-api-with-dynamodb-and-offline/db-seeds/todos.json b/aws-node-rest-api-with-dynamodb-and-offline/db-seeds/todos.json new file mode 100644 index 000000000..e8deb75bf --- /dev/null +++ b/aws-node-rest-api-with-dynamodb-and-offline/db-seeds/todos.json @@ -0,0 +1,16 @@ +[ + { + "text": "Deploy my first service", + "id": "849fbd50-c926-4e08-9029-d47438b5f487", + "checked": true, + "createdAt": 1707175709304, + "updatedAt": 1707175709304 + }, + { + "text": "Learn Serverless", + "id": "cc6fbe30-c47f-11ee-9a7b-7d80f9b962e5", + "checked": false, + "createdAt": 1707175709241, + "updatedAt": 1707175770849 + } +] diff --git a/aws-node-rest-api-with-dynamodb-and-offline/offline/migrations/todos.json b/aws-node-rest-api-with-dynamodb-and-offline/offline/migrations/todos.json deleted file mode 100644 index 2a3b29611..000000000 --- a/aws-node-rest-api-with-dynamodb-and-offline/offline/migrations/todos.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "Table": { - "TableName": "serverless-rest-api-with-dynamodb-dev", - "KeySchema": [ - { - "AttributeName": "id", - "KeyType": "HASH" - } - ], - "AttributeDefinitions": [ - { - "AttributeName": "id", - "AttributeType": "S" - } - ], - "ProvisionedThroughput": { - "ReadCapacityUnits": 1, - "WriteCapacityUnits": 1 - } - } -} diff --git a/aws-node-rest-api-with-dynamodb-and-offline/package.json b/aws-node-rest-api-with-dynamodb-and-offline/package.json index c6e86ef27..ae7334f5b 100644 --- a/aws-node-rest-api-with-dynamodb-and-offline/package.json +++ b/aws-node-rest-api-with-dynamodb-and-offline/package.json @@ -6,11 +6,11 @@ "author": "Christoph Gysin ", "license": "MIT", "dependencies": { - "uuid": "^2.0.3" + "serverless-dynamodb": "^0.2.49", + "uuid": "^9.0.1" }, "devDependencies": { - "aws-sdk": "^2.12.0", - "serverless-dynamodb-local": "^0.2.18", - "serverless-offline": "^6.8.0" + "aws-sdk": "^2.1551.0", + "serverless-offline": "^13.3.3" } } diff --git a/aws-node-rest-api-with-dynamodb-and-offline/serverless.yml b/aws-node-rest-api-with-dynamodb-and-offline/serverless.yml index ad9bc6628..97326f7f2 100644 --- a/aws-node-rest-api-with-dynamodb-and-offline/serverless.yml +++ b/aws-node-rest-api-with-dynamodb-and-offline/serverless.yml @@ -3,7 +3,7 @@ service: serverless-rest-api-with-dynamodb frameworkVersion: "3" plugins: - - serverless-dynamodb-local + - serverless-dynamodb - serverless-offline custom: @@ -13,17 +13,23 @@ custom: start: port: 8000 inMemory: true + # Uncomment if you have a DynamoDB instance already running locally you want to use instead + # noStart: true + # Create the tables according to the resources.Resources block migrate: true - # Comment if you don't have a DynamoDB running locally - noStart: true - migration: - dir: offline/migrations + # Insert seed items on creation, configured by the custom.dynamodb.seed block + seed: true + seed: + default: + sources: + - table: ${self:provider.environment.DYNAMODB_TABLE} + sources: [./db-seeds/todos.json] provider: name: aws runtime: nodejs18.x environment: - DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage} + DYNAMODB_TABLE: ${self:service}-${sls:stage} iam: role: statements: @@ -35,7 +41,7 @@ provider: - dynamodb:PutItem - dynamodb:UpdateItem - dynamodb:DeleteItem - Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}" + Resource: "arn:aws:dynamodb:${aws:region}:*:table/${self:provider.environment.DYNAMODB_TABLE}" functions: create: diff --git a/aws-python-flask-dynamodb-api/README.md b/aws-python-flask-dynamodb-api/README.md index e91d265b8..ac198e542 100644 --- a/aws-python-flask-dynamodb-api/README.md +++ b/aws-python-flask-dynamodb-api/README.md @@ -112,10 +112,10 @@ pip install werkzeug boto3 pip install -r requirements.txt ``` -Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb-local` plugin. In order to do that, execute the following commands: +Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb` plugin. In order to do that, execute the following commands: ```bash -serverless plugin install -n serverless-dynamodb-local +serverless plugin install -n serverless-dynamodb serverless dynamodb install ``` @@ -162,6 +162,6 @@ At this point, you can run your application locally with the following command: serverless wsgi serve ``` -For additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories: +For additional local development capabilities of `serverless-wsgi` and `serverless-dynamodb` plugins, please refer to corresponding GitHub repositories: - https://github.com/logandk/serverless-wsgi -- https://github.com/99x/serverless-dynamodb-local +- https://github.com/raisenational/serverless-dynamodb diff --git a/aws-ruby-sinatra-dynamodb-api/README.md b/aws-ruby-sinatra-dynamodb-api/README.md index b95fef2a9..07f15759e 100644 --- a/aws-ruby-sinatra-dynamodb-api/README.md +++ b/aws-ruby-sinatra-dynamodb-api/README.md @@ -123,10 +123,10 @@ bundle install --path vendor/bundle serverless rack serve ``` -Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb-local` plugin. In order to do that, execute the following commands: +Additionally, you will need to emulate DynamoDB locally, which can be done by using `serverless-dynamodb` plugin. In order to do that, execute the following commands: ```bash -serverless plugin install -n serverless-dynamodb-local +serverless plugin install -n serverless-dynamodb serverless dynamodb install ``` @@ -152,8 +152,8 @@ client_options = if ENV['IS_OFFLINE'] region: 'localhost', endpoint: 'http://localhost:8000', credentials: Aws::Credentials.new( - 'DEFAULT_ACCESS_KEY', - 'DEFAULT_SECRET' + 'MockAccessKeyId', + 'MockSecretAccessKey' ) } else @@ -174,7 +174,7 @@ At this point, you can run your application locally with the following command: serverless rack serve ``` -For additional local development capabilities of `serverless-rack` and `serverless-dynamodb-local` plugins, please refer to corresponding GitHub repositories: +For additional local development capabilities of `serverless-rack` and `serverless-dynamodb` plugins, please refer to corresponding GitHub repositories: - https://github.com/logandk/serverless-rack -- https://github.com/99x/serverless-dynamodb-local +- https://github.com/99x/serverless-dynamodb