The ByteLab logo

Setting up Laravel Sail with DynamoDB Local

Posted on 16 August 2021

This article is intended for use with the baopham/laravel-dynamodb package, however, this can be adapted for any other package providing integration with DynamoDB. Just remember the core concept as this will most likely remain the same: DynamoDB local.


If you're building functionality in to your Laravel app that uses DynamoDB, it can be a cumbersome process to get a DynamoDB instance set up locally. Luckily, Laravel Sail is highly customisable and exposes a docker-compose.yml in the project root for you to make your own.

First, if your Sail environment is running, stop it by running: ./vendor/bin/sail down.

You're going to want to open the docker-compose.yml generated by Laravel Sail and add a new service to the services object array called dynamodb:

dynamodb:
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
    image: amazon/dynamodb-local:latest
    working_dir: /home/dynamodblocal
    volumes:
        - './volumes/dynamodb:/home/dynamodblocal/data'
    environment:
        AWS_ACCESS_KEY_ID: dynamodb_local
        AWS_SECRET_ACCESS_KEY: secret
        AWS_REGION: stub
    ports:
        - "8042:8000"
    networks:
        - sail

You're also going to want to update the laravel.test service to depend on your shiny new dynamodb service. Update the depends_on to include dynamodb - this should look similar to the following:

depends_on:
  - mysql
  - redis
  - dynamodb

This will ensure your dynamodb service will start before the service running your Laravel app.

A full example of the docker-compose.yml is available as a Gist on GitHub. Please note that the contents of the docker-compose.yml found in the Gist may be out of date by the time you read this as the Sail's docker-compose is of course subject to change.


Next up is configuring the baopham/laravel-dynamodb package to use the DynamoDB local instance you just set up.

Open your .env found in the project root and set the value of DYNAMODB_CONNECTION to local - if this doesn't exist in your config, create it:

DYNAMODB_CONNECTION=local

Inside the same .env, also set the value of DYNAMODB_LOCAL_ENDPOINT to http://dynamodb:8000:

DYNAMODB_LOCAL_ENDPOINT=http://dynamodb:8000

That's it! You've set up Laravel Sail with an instance of DynamoDB Local and configured baopham/laravel-dynamodb to use it. This is super useful if you're needing to write tests to assert functionality with DynamoDB is working correctly.