Categories
php

Laravel AWS SDK credentials using .env and configs

Every now and then, I need to integrate with an Amazon web service (aws). I like the whole IAM user way of doing this, so that is usually my preferred choice.

However, every time i need to setup a connection in a project (using mostly Laravel these days), I forget how I did it last time.

If you ever struggle with remembering as well, or simple think the documentation on their AWS SDK for PHP is a bit rubbish, then this is my little guide to setting up your clients for connections.

The steps are as following:

  • Log in to their AWS console on aws.amazon.com
  • Open the IAM service and create a new user. Follow their guide if necessary. Basically just follow the wizard, then click create user and go back and permissions under the “Permission” tab, for the service you need (e.g. AmazonSNSFullAccess for SNS)
  • Add their composer library to your project:
    composer require aws/aws-sdk-php

After composer gets done, you need to open your Laravel project in the editor (This guide also “works” for other php libraries, but I am just using Laravels .env and config settings)

Open up .env located in your application root and add a few settings:

AWS_SNS_KEY=
AWS_SNS_SECRET=
AWS_SNS_REGION=
AWS_SNS_VERSION=latest

I like to have different IAM users for different services, that is why I am adding the _SNS_ in my settings. Remember to fill them out with your credentials from your newly created IAM user.

Next step is to create an new aws.php file in config/ folder.
This will hold your AWS settings, so they can be accessed using the config method.

Add the following contents to config/aws.php:

<?php

return [
    'sns' => [
        'region' => env('AWS_SNS_REGION'),
        'version' => env('AWS_SNS_VERSION'),
        'credentials' => [
            'key' => env('AWS_SNS_KEY'),
            'secret' => env('AWS_SNS_SECRET'),
        ],
    ],
];

By adding the ‘sns’ key, your settings available under aws.sns.xx

Let us build a quick unit test to see if we can access our service.
Create a new file called /test/Integration/Aws/SnsTest.php

<?php

namespace Tests\Integration\Aws;

use Aws\Sns\SnsClient;
use Tests\TestCase;

class SnsTest extends TestCase
{
    public function testAwsConnection()
    {
        $client = new SnsClient([
            'region' => config('aws.sns.region'),
            'version' => config('aws.sns.version'),
            'credentials' => config('aws.sns.credentials'),
        ]);

        $topics = $client->listTopics();
        self::assertNotNull($topics);
    }
}

We create a new SnsClient, passing an array of settings.
If you have a look at their documentation, they have a 'profile' => 'default'

Do not add that, as it will tell the client to find the credentials to use, in your local ~/.aws/credentials file.

Using the credentials key, will read the credentials from your aws.sns.credentials (which points to AWS_SNS_KEY and AWS_SNS_SECRET).

Run the unit test and profit!

vendor/bin/phpunit tests/Integration/Aws/SnsTest.php

Leave a Reply

Your email address will not be published. Required fields are marked *