phpamazon-web-servicesamazon-ec2amazon-snsaws-php-sdk

How is it possible to use the current EC2 instances IAM role in AWS SDK PHP?


I would like to use SNS/SQS in my PHP application, that is running on an EC2 instance.

I was able to find the guide about credentials for the AWS SDK for PHP Version 3, but I still have no idea how to simply use the IAM Role that is associated with my EC2 instance, my app is running on.

This is what I tried:

$SnSclient = new SnsClient([
    'profile' => 'default',
    'region' => 'eu-central-1',
    'version' => '2010-03-31',
    'credentials' => \Aws\Credentials\CredentialProvider::defaultProvider()
]);

But I get this error:

PHP Fatal error:  Uncaught Aws\Exception\CredentialsException: Cannot read credentials from /home/ec2-user/.aws/credentials in phar:///var/www/html/aws.phar/Aws/Credentials/CredentialProvider.php:874

I would like to avoid uploading my pem file to the instance, since it is already running in AWS, as far as I know it should be able to use it's IAM role


Solution

  • The 'profile' => 'default', line had to be removed, as suggested in this answer

    It does not seem to be well documented, but if you are using the IAM Role of the instance, then no profile or credentials attributes are necessary