Failed to instantiate \ Aws \ Ec2 \ Ec2Client
Trying to instantiate ec2client wrt
$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2014-10-01'
));
but with an 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 via composer "aws/aws-sdk-php": "3.*@dev"
.
Edit 1): I am trying to do this on my local development machine.
Edit 2): I've 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 another 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 the impression that credential profiles are only when the application is launched from an AWS cloud instance! Which makes the idea of ββprofiles useless (where intended for a developer environment)
Edit 3)
After debugging this file, it appears that the sdk with credential profiles is broken or not as expected. Both credential profiles
and environment variable
both depend on environment variables. If environment variables are disabled, they will fail.
Possible work around:
a) Include 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()
the function has a filename parameter, but there is no way to skip the Credentails profile path other than HOME env
variable
Aws\Common\Credentails\Profiler->init()
public static function ini($profile = null, $filename = null){
...
}
Again, if you can't read the file /Users/yourname/.aws/credentials
, you need to tweak it a bit
chmod 644 /Users/yourname/.aws/credentials
source to share
You have to place the .aws / credentials file in your configuration in the home directory of the web service.
Put this in some PHP file of your website
echo getenv('HOME');
then in that directory:
mkdir .aws
touch credentials
nano credentials
And there you need to insert the content (no need for quotes in the keys):
[default]
aws_access_key_id = xxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx
And it should be!
Edit: As sahunzai says in the comment (thanks!), Echo getenv ("HOME") can sometimes return null, if it does, he suggests this solution: "However, from the command line php -i | grep HOME gives me the correct home directory user "
So keep that in mind.
source to share