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

2

5

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?

Heptagon answered 15/9, 2015 at 18:49 Comment(0)
H
2

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.

Heptagon answered 15/9, 2015 at 19:42 Comment(0)
V
0

This is possible in V3 of the Laravel AWS SDK. I do something similar to the following in my Service Provider class.

use Aws\DynamoDb\DynamoDbClient;
use Aws\Laravel\AwsFacade;

// This would be http://localhost:8000 or whatever your local is configured to be
$endpoint = getYourLocalEndpointFromConfig();

$args = [
  'key' => 'anything',
  'secret' => 'anything',
  'region' => 'us-east-1',
];

if ($endpoint !== null) {
  $args['endpoint'] = $endpoint;
}

/** @var DynamoDbClient $client */
$client = AwsFacade::createClient('dynamodb', $args);

Your $endpoint as well as all the values in the $args array can be read from your config or .env file or anywhere else for that matter. In non-local environments, the $endpoint variable would be null so it would not be included when creating a $client object, and so you'd connect to the real DynamoDB web service (assuming keys were valid).

After this, the $client object can make any of the calls listed in the documentation.

Vivianaviviane answered 5/1, 2022 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.