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

AWS Environment variable provider not accepting credentials


I'm connecting dynamoDb locally using the below code

$dotenv = new Dotenv\Dotenv(dirname(__DIR__, 2));
    $dotenv->load();
    $this->key = getenv('aws_access_key_id');
    $this->secret = getenv('aws_secret_access_key');
    $this->dynamodb = new DynamoDbClient([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => 'latest',
        'credentials' => [
            'key' => $this->key,
            'secret' => $this->secret,
        ]
    ]);

My .env file

aws_access_key_id = ....
aws_secret_access_key = ....

I'm getting this error

Type: Aws\Exception\CredentialsException
Message: Cannot read credentials from /Users/user/.aws/credentials
File: /path/to/project/vendor/aws/aws-sdk-php/src/Credentials/CredentialProvider.php
Line: 394

Tried the basic hard-coding of access key & secret key as well but it doesn't seem to be working rather every time it's fetching from the default path ~/.aws/credentials

TIA


Solution

  • Try to print the environment variable to make sure your application is getting those environment variables.

    It is not finding it from those variables. You need to export them before you run this script.

    Hope it helps.

    EDIT1:

    use Aws\Common\Credentials\Credentials;
    
    $credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
    

    //assign these credentials to Dynamodbclient

    EDIT2: AWS SDK 3.x follows different way to initialize.

    $sdk = new Aws\Sdk([
        'credentials' => $credentials,
        'region' => 'us-east-1',
        'version' => 'latest',
        'DynamoDb' => [
            'region' => 'us-west-2',
        ],
    ]);
    
    $dynamodb = $sdk->createDynamoDb();  // This is dynamodb client