serverless-dynamodb-local icon indicating copy to clipboard operation
serverless-dynamodb-local copied to clipboard

Error: connect ECONNREFUSED 127.0.0.1:8000 Docker

Open mapeveri opened this issue 4 years ago • 3 comments

Hi everyone, i have this issue. When i run sls dynamodb migrate I have this error:

image

My serverless.yml

service: asistencias-api

# serverless supports different cloud environments to run at.
# we will be deploying and running this project at AWS cloud with Node v8.10 environment
provider:
  name: aws
  runtime: nodejs10.0
  region: us-east-1
  stage: dev
  environment:
    DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"

# here we describe our lambda function
functions:
  main: # function name
    handler: src/handler.main
    events:
        - http:
            path: /
            method: get
            cors: true
  login:
    handler: src/handler.login
    events:
        - http:
            path: /login
            method: post
            cors: true

plugins:
  - serverless-dynamodb-local
  - serverless-offline

custom:
  dynamodb:
    start:
      port: 8000
      inMemory: false
      migrate: false
      dbPath: .
    stages:
      - dev
  serverless-offline:
    host: 0.0.0.0

resources:
  Resources:
    ItemsDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}

I am using docker and this is my configuration:

Dockefile

FROM node:10-alpine

RUN mkdir -p /var/www/app
WORKDIR /var/www/app

RUN apk update && apk add openjdk7-jre
RUN npm install -g serverless

COPY . /var/www/app
RUN npm install
RUN npm run db:install

Docker-compose

version: '3'
services:
  asistencia-api:
    build: .
    image: backend_asistencia-api_1:latest
    depends_on:
      - dynamodb
    restart: always
    ports:
      - 3000:3000
    links:
      - dynamodb
    volumes:
      - ./:/var/www/app
      - /var/www/app/.dynamodb
      - /var/www/app/node_modules
      - /var/www/app/dynamodb_local_db
    command: npm run start
    networks:
      - net
  dynamodb:
    image: backend_asistencia-api_1:latest
    ports:
      - 8000:8000
    expose:
        - "8080"
    command: npm run db:start
    networks:
      - net

networks:
  net:

The table is not being created. When I make a connection in the code it gives me timeout.

The docker container lifts well, and even the dynamodb shell.

image

My dynamodb client configuration:

'use strict';

const AWS = require('aws-sdk');

let options = {};

// connect to local DB if running offline
if (process.env.IS_OFFLINE) {
  options = {
    region: 'localhost',
    accessKeyId: 'xxxx',
    secretAccessKey: 'xxxx',
    endpoint: 'http://dynamodb:8000',
  };
}

const client = new AWS.DynamoDB.DocumentClient(options);
module.exports = client;

I don't know what I may be missing. Thanks!

mapeveri avatar Aug 19 '19 08:08 mapeveri

Hi there, I have an example of using serverless-dynamodb-local together with docker here https://github.com/jch254/serverless-node-dynamodb-api. I am using Amazon's dynamodb-local image to run Dynamo in a separate container and the Serverless API in another.

jch254 avatar Sep 19 '19 11:09 jch254

Doing a similar thing and seem to be having the same issue 😞. @mapeveri have you manage to figure this out since?

abdimaye avatar Oct 23 '19 10:10 abdimaye

have a look at this pr, it helped me. Just need to add the host as an option in index.js of the plugin https://github.com/99xt/serverless-dynamodb-local/pull/235

ionush avatar Apr 11 '20 06:04 ionush