Support for using IAM authentication with the official Elasticsearch PHP client

0.3.0 2019-07-20 01:13 UTC

This package is auto-updated.

Last update: 2024-04-15 01:20:14 UTC


Apache 2 License Total Downloads Author Build Status

This package provides a signing handler for use with the official Elasticsearch-PHP (elasticsearch/elasticsearch) client. By default, the handler will load AWS credentials from the environment and send requests using a RingPHP cURL handler.

Basic Usage

Instances of Aws\ElasticsearchService\ElasticsearchPhpHandler are callables that fulfill Elasticsearch-PHP's handler contract. They can be passed to Elasticsearch\ClientBuilder's setHandler method:

use Aws\ElasticsearchService\ElasticsearchPhpHandler;
use Elasticsearch\ClientBuilder;

// Create a handler (with the region of your Amazon Elasticsearch Service domain)
$handler = new ElasticsearchPhpHandler('us-west-2');

// Use this handler to create an Elasticsearch-PHP client
$client = ClientBuilder::create()

// Use the client as you normally would
    'index' => $index,
    'type' => $type,
    'id' => $id,
    'body' => [$key => $value]

Using custom credentials

By default, the handler will attempt to source credentials from the environment as described in the AWS SDK for PHP documentation. To use custom credentials, pass in a credential provider:

use Aws\Credentials\CredentialProvider;
use Aws\Credentials\Credentials;
use Aws\ElasticsearchService\ElasticsearchPhpHandler;

$provider = CredentialProvider::fromCredentials(
    new Credentials('foo', 'bar', 'baz')

$handler = new ElasticsearchPhpHandler('us-west-2', $provider);

Using a custom HTTP handler

By default, the handler will use Elasticsearch\ClientBuilder::defaultHandler() to dispatch HTTP requests, but this is customizable via an optional constructor parameter. For example, this repository's tests use a custom handler to mock network traffic:

class ElasticsearchPhpHandlerTest extends \PHPUnit_Framework_TestCase
    public function testSignsRequestsPassedToHandler()
        $toWrap = function (array $ringRequest) {
            $this->assertArrayHasKey('X-Amz-Date', $ringRequest['headers']);
            $this->assertArrayHasKey('Authorization', $ringRequest['headers']);
                'AWS4-HMAC-SHA256 Credential=',

            return $this->getGenericResponse();
        $handler = new ElasticsearchPhpHandler('us-west-2', null, $toWrap);

        $client = \Elasticsearch\ClientBuilder::create()

            'index' => 'index',
            'type' => 'type',
            'id' => 'id',



composer require jsq/amazon-es-php