Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serverless.yml not creating dynamodb table locally

I have a docker setup that runs serverless and dynamodb both in docker. In my serverless.yml I defined instructions for creating a table, yet when I run docker compose up it does not create the table (I've checked by running a query to list tables.

serverless.yml

...
provider:
  name: aws
  runtime: nodejs8.10
  stage: dev
  region: eu-west-1

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

custom:
  dynamodb:
    inMemory: true
    port: 8000
    migrate: true # create tables on start
    onStart: true
  serverless-offline:
    babelOptions:
      presets: ["es2015"]

resources:
  Resources:
    UserTable:
      Type: "AWS::DynamoDB::Table"
      Properties:
        TableName: UserTable
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
          - AttributeName: fullname
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

docker-compose.yml

version: "3"

services:
  serverless:
    build: .
    restart: always
    privileged: true
    working_dir: /home/node-app
    environment:
      - NODE_ENV=development
      - DYNAMO_ENDPOINT=http://dynamodb:8000
      - AWS_ACCESS_KEY_ID='dev-key-id'
      - AWS_SECRET_ACCESS_KEY='dev-key'
      - AWS_REGION='eu-west-1'
    links:
      - "dynamodb:dynamodb"
    command: npm run start
  ....
  dynamodb:
    image: amazon/dynamodb-local
    ports:
      - "8000:8000"
    environment:
      - AWS_ACCESS_KEY_ID='dev-key-id'
      - AWS_SECRET_ACCESS_KEY='dev-key'

npm run start simply runs sls offline. Once I run the docker compose, everything starts up fine but the UserTable is not created.

enter image description here

like image 594
John Mike Avatar asked Dec 29 '25 09:12

John Mike


1 Answers

If you are using Docker to manage your DynamoDB service, you will need to instruct the serverless-dynamodb-local plugin to create the table for you.

You can do this by running:

serverless dynamodb migrate

One common gotcha with the Docker version of DynamoDB local is that the tables are only visible to a client using the same AWS Access Key ID used to create the table e.g running list-tables will show no tables, even if you have successfully created one.

$ aws dynamodb list-tables --endpoint "http://localhost:8000"
{
    "TableNames": []
}

The simplest solution to this is probably to pass the -sharedDb option to DynamoDB:

docker run -p 8000:8000 -d amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb

(GitHub source)

This then produces the expected output after running serverless dynamodb migrate:

aws dynamodb list-tables --endpoint "http://localhost:8000"
{
    "TableNames": [
        "SampleDynamoTable-local"
    ]
}
like image 112
jbourne Avatar answered Jan 01 '26 16:01

jbourne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!