sakhunzai
sakhunzai

Reputation: 14470

Unable to instantiate \Aws\Ec2\Ec2Client

Trying to instantiate ec2client w.r.t

$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
    'profile' => 'default',
    'region'  => 'us-west-2',
    'version' => '2014-10-01'
));

but fails with error

Uncaught exception 'Aws\Common\Exception\CredentialsException' with message 'Could not load credentials.'

I have :

cat ~/.aws/credentials 

[default]
aws_access_key_id = xxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx

I am using aws-sdk through composer "aws/aws-sdk-php": "3.*@dev".

Edit 1): I am trying this on my local development machine.

Edit 2): I tried this:

 use Aws\Common\Credentials\InstanceProfileProvider;
    $profile= new InstanceProfileProvider(['profile'=>'default']);
    $ec2Client = \Aws\Ec2\Ec2Client::factory(array(
        'region'  => 'us-west-2',
        'version' =>'2014-10-01',
        'credentials' => $profile
    ));

This gives me different error :

'Error retrieving credentials from the instance profile metadata server. When you are not running inside of Amazon EC2, you must provide your AWS Access Key ID and Secret Access Key in the "key" and "secret" options when creating a client or provide an instantiated Aws\Common\Credentials\CredentialsInterface object. (cURL error 28: Connection timed out after 1000 milliseconds)' in .. /InstanceProfileProvider.php:9 ..

This gives me the impression that credential profiles are only when application running from within the AWS cloud instance ! Which makes the idea of profiles useless (which where intended for dev environment )

Edit 3)

After debugging this, It seems that sdk with credential profiles is broken or not as expected . Both credential profiles and environment variable both depends on environment variables . If environment variables are disabled, both will fail.

Possible work around:

a) Enable environment variables

b) Set HOME evn variable

putenv('HOME=/Users/yourname');
$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
'region'  => 'us-west-2',
'version' => '2014-10-01'
));

Profile init() function has filename option but there is not apparent way to pass credentails profile path other than HOME env variable

Aws\Common\Credentails\Profiler->init()
public static function ini($profile = null, $filename = null){
...
}

Again if you are unable to read the /Users/yourname/.aws/credentials file you have to tweak it little bit

chmod 644 /Users/yourname/.aws/credentials

Upvotes: 3

Views: 1267

Answers (2)

Franco Risso
Franco Risso

Reputation: 1582

You have to place the .aws/credentials file with your configuration in the home directory of the web service.

Put this into some PHP file of your website

echo getenv('HOME');

then in that directory:

mkdir .aws
touch credentials
nano credentials

And there you have to paste the content (no need for quotes in the keys):

[default]
aws_access_key_id     = xxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx

And that should do it!


Edit: As sakhunzai says in the comment (thanks!), echo getenv('HOME') can return null sometimes, if that happens he offer this solution: "However from command line php -i |grep HOME gives me correct user home directory"

So have that in mind.

Upvotes: 1

Anti
Anti

Reputation: 458

You should place the .aws/credentials file in the root of your web service, not in the root of your ssh user

Upvotes: 2

Related Questions