Search code examples
phplaravelamazon-web-servicesamazon-dynamodbdynamo-local

Is there a way to set up DynamoDB Local with the Laravel AWS SDK?


I have run into problems trying to get DynamoDB Local up and running with my current laravel project.

The Laravel AWS SDK allows for a few keys to be set in the .env file to change the key/secret/region, but does not seem to support changing the endpoint, and that would be needed to use DynamoDB Local (key options are in the readme here: https://github.com/aws/aws-sdk-php-laravel).

The documentation for the regular PHP SDK seems pretty straight-forward about how to set up Dynamo DB Local:

$client = DynamoDbClient::factory(array(
    'profile' => 'default',
    'region' => 'us-west-2', #replace with your desired region
    'endpoint' => 'http://localhost:8000'
));

With the Laravel AWS SDK I don't have direct access to the DynamoDBClient without hacking up a bunch of stuff that is necessary for the production DynamoDB connection.

For example with the Laravel AWS SDK the DynamoDB is accessed by something like:

$dynamoDB = AWS::get('DynamoDb');

I am really looking for something like an environment variable that can be changed so that I can easily switch between production and my local, but I can't find it.

Is there any easy way to set up DynamoDB Local with the Laravel AWS SDK?


Solution

  • The way I ended up getting this to work was to create my own environment variable and then check to see if it was set when I got the DynamoDBClient.

    AWS::get('DynamoDb')
    

    The above returns a DynamoDBClient that is automatically using your AWS configuration.

    So, I did a check for the env variable, and return a DynamoDBClient with the local configuration if the env variable is set. I had to use the Aws DynamoDBClient class:

    use Aws\DynamoDb\DynamoDbClient;
    

    Then I did:

    if( env("DYNAMODB_LOCAL")) {
        $this->client = DynamoDbClient::factory(array(
            'key' => 'YOUR_KEY', // Doesn't actually matter what it is since it won't be verified
            'secret' => 'YOUR_SECRET', // Doesn't actually matter what it is since it won't be verified
            'profile' => 'default',
            'region' => 'us-west-2', #replace with your desired region
            'endpoint' => 'http://localhost:8000' // Replace if your local endpoint is different than default
        ));
    }
    else {
        $this->client = AWS::get('DynamoDb');
    }
    

    It would have been nice if the Laravel AWS SDK came with some kind of easy environment configuration for DynamoDB Local, but this seems to work for my uses.