99x / serverless-dynamodb-local

Serverless Dynamodb Local Plugin - Allows to run dynamodb locally for serverless
MIT License
627 stars 232 forks source link

Table created on migrate but not found on shell #241

Open KingDarBoja opened 4 years ago

KingDarBoja commented 4 years ago

Actual Behaviour

Even though I followed the install steps and provided the below configuration, trying to access the table on one of my lambda functions throws ResourceNotFoundException: Cannot do operations on a non-existent table.

dynamoConfig.ts

import DynamoDB = require('aws-sdk/clients/dynamodb');

// Setup DynamoDB options
const options: DynamoDB.ClientConfiguration = {
    region: 'localhost',
    endpoint: 'http://localhost:8000',
    accessKeyId: process.env.ACCESS_KEY_ID,
    secretAccessKey: process.env.SECRET_ACCESS_KEY
};

// connect to local DB if running offline
const isOffline = (): boolean => {
    // Depends on serverless provider environment which adds IS_OFFLINE to process.env
    return Boolean(process.env.IS_OFFLINE);
};

// Initialize our client to connect to DynamoDB.
const dynamodb = {
    doc: isOffline() ? new DynamoDB.DocumentClient(options) : new DynamoDB.DocumentClient(),
    raw: isOffline() ? new DynamoDB(options) : new DynamoDB()
};

export default dynamodb;

Expected Behaviour

State here what the feature should enable the user to do.

Steps to reproduce it

Install whatever plugin you need, in my case, I make use of the following on my serverless.yml file:

plugins:
    - serverless-dotenv-plugin
    - serverless-apigw-binary
    - serverless-api-compression
    - serverless-plugin-typescript
    - serverless-dynamodb-local
    - serverless-offline

Install the dynamodb plugin following the readme and provide custom options like as:

custom:
    dynamodb:
        stages:
            - ${self:provider.stage}
        start:
            port: 8000
            inMemory: true
            migrate: ${env:IS_OFFLINE}

Run SLS_DEBUG=* serverless offline start

The environment variables are being properly initialized, as I am trying to test it offline.

LogCat for the issue

**Log from sls offline start** ```console Serverless: Load command interactiveCli Serverless: Load command config Serverless: Load command config:credentials Serverless: Load command create Serverless: Load command install Serverless: Load command package Serverless: Load command deploy Serverless: Load command deploy:function Serverless: Load command deploy:list Serverless: Load command deploy:list:functions Serverless: Load command invoke Serverless: Load command invoke:local Serverless: Load command info Serverless: Load command logs Serverless: Load command metrics Serverless: Load command print Serverless: Load command remove Serverless: Load command rollback Serverless: Load command rollback:function Serverless: Load command slstats Serverless: Load command plugin Serverless: Load command plugin Serverless: Load command plugin:install Serverless: Load command plugin Serverless: Load command plugin:uninstall Serverless: Load command plugin Serverless: Load command plugin:list Serverless: Load command plugin Serverless: Load command plugin:search Serverless: Load command config Serverless: Load command config:credentials Serverless: Load command rollback Serverless: Load command rollback:function Serverless: Load command upgrade Serverless: Load command uninstall Serverless: DOTENV: Loading environment variables from .env: Serverless: - IS_OFFLINE Serverless: - ACCESS_KEY_ID Serverless: - SECRET_ACCESS_KEY Serverless: Load command dynamodb Serverless: Load command dynamodb:migrate Serverless: Load command dynamodb:seed Serverless: Load command dynamodb:start Serverless: Load command dynamodb:noStart Serverless: Load command dynamodb:remove Serverless: Load command dynamodb:install Serverless: Load command offline Serverless: Load command offline:start Serverless: Load command login Serverless: Load command logout Serverless: Load command generate-event Serverless: Load command test Serverless: Load command dashboard Serverless: Invoke offline:start Serverless: Compiling with Typescript... Serverless: Using local tsconfig.json Serverless: Typescript compiled. Serverless: Watching typescript files... Dynamodb Local Started, Visit: http://localhost:8000/shell Serverless: DynamoDB - created table production-careers_types Serverless: DynamoDB - created table production-category_types offline: Starting Offline: production/us-east-1. [offline] options: { apiKey: 'd41d8cd98f00b204e9800998ecf8427e', corsAllowCredentials: true, corsAllowHeaders: [ 'accept', 'content-type', 'x-api-key', 'authorization' ], corsAllowOrigin: [ '*' ], corsExposedHeaders: [ 'WWW-Authenticate', 'Server-Authorization' ], disableCookieValidation: false, enforceSecureCookies: false, hideStackTraces: false, host: 'localhost', httpPort: 3000, httpsProtocol: '', lambdaPort: 3002, noAuth: false, noTimeout: false, printOutput: false, resourceRoutes: false, useChildProcesses: false, useWorkerThreads: false, websocketPort: 3001, useDocker: false, stage: undefined, region: undefined, corsConfig: { credentials: true, exposedHeaders: [ 'WWW-Authenticate', 'Server-Authorization' ], headers: [ 'accept', 'content-type', 'x-api-key', 'authorization' ], origin: [ '*' ] } } offline: Offline [http for lambda] listening on http://localhost:3002 ...some lambda endpoints... [offline] Loading handler... (D:\Programming\ts_projects\Angular-serverless-sample\.build\api\types) null { TableNames: [ 'production-careers_types', 'production-category_types' ] } ResourceNotFoundException: Cannot do operations on a non-existent table at Request.extractError (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\protocol\json.js:51:27) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:14:12) at D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:38:9) at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:14:12) at D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:38:9) at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at callNextListener (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:96:12) at IncomingMessage.onEnd (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\event_listeners.js:307:13) at IncomingMessage.emit (events.js:215:7) at IncomingMessage.EventEmitter.emit (domain.js:475:20) at endReadableNT (_stream_readable.js:1184:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) { message: 'Cannot do operations on a non-existent table', code: 'ResourceNotFoundException', time: 2020-02-19T13:46:01.800Z, requestId: 'e13f24e0-7e7c-4f73-bfb3-928535f13efd', statusCode: 400, retryable: false, retryDelay: 13.170882137685869 } [offline] _____ HANDLER RESOLVED _____ offline: Failure: Cannot do operations on a non-existent table ResourceNotFoundException: Cannot do operations on a non-existent table at Request.extractError (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\protocol\json.js:51:27) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:14:12) at D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:38:9) at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:14:12) at D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:38:9) at Request. (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at callNextListener (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\sequential_executor.js:96:12) at IncomingMessage.onEnd (D:\Programming\ts_projects\Angular-serverless-sample\node_modules\aws-sdk\lib\event_listeners.js:307:13) at IncomingMessage.emit (events.js:215:7) at IncomingMessage.EventEmitter.emit (domain.js:475:20) at endReadableNT (_stream_readable.js:1184:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) [offline] Using response 'default' [offline] _____ RESPONSE PARAMETERS PROCCESSING _____ [offline] Found 0 responseParameters for 'default' response [offline] headers {} ```

Screenshots of the issue

Navigating to http://localhost:8000/shell/ and using the list tables example code, doesn't show the tables on the output list. Where-ever possible attach a screenshot of the issue. no-tables-shell

Would you like to work on the issue?

Not at the moment.

mikesir87 commented 4 years ago

I ran into this today. It's probably because the plugin has hard-coded values for the access key, secret, and region. You'll have to update your shell and any other code that is trying to use the table to have the same values. There is a PR (#204), but it looks like it's gone a little stale. Anywho... the values are as follows:

https://github.com/99xt/serverless-dynamodb-local/blob/07e3edc6d138142b9e9d47b569033c61d5d578b4/index.js#L177-L179