title: Creating a valid signed request to an AWS ES instance from node.js
body: <p>I'm trying to find an example of how to connect to an AWS ES instance in node.js, and then hit the ES cluster with a simple request.</p>
  
  <p>I'm attempting to use the <a href="https://www.npmjs.com/package/elasticsearch" rel="nofollow noreferrer">elasticsearch node package</a>, along with an open source addon called <a href="https://github.com/TheDeveloper/http-aws-es" rel="nofollow noreferrer">http-aws-es</a> to do this.</p>
  
  <p>I have configured my aws ES access policy to look like the following:</p>
  
  <pre><code>{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Principal": {
          "AWS": "arn:aws:iam::&lt;account-id&gt;:root"
        },
        "Action": "es:*",
        "Resource": "example-domain.us-east-1.es.amazonaws.com:&lt;account-id&gt;:domain/*"
      },
      {
        "Sid": "",
        "Effect": "Allow",
        "Principal": {
          "AWS": "*"
        },
        "Action": "es:*",
        "Resource": "example-domain.us-east-1.es.amazonaws.com:&lt;account-id&gt;:domain/*",
        "Condition": {
          "IpAddress": {
            "aws:SourceIp": "&lt;my-ip&gt;"
          }
        }
      }
    ]
  } 
  </code></pre>
  
  <p>So, I would like to be able to issue put and get requests on the es instance from either an ip address or from an IAM user linked to my aws account.</p>
  
  <p>I have the following code that attempts this in node.js:</p>
  
  <pre><code>var aws_access_key = 'example';
  var aws_secret_key = 'key';
  
  var es = require('elasticsearch').Client({
      hosts: 'example-domain.us-east-1.es.amazonaws.com',
      connectionClass: require('http-aws-es'),
      amazonES: {
          region: 'us-east-1',
          accessKey: aws_access_key,
          secretKey: aws_secret_key
      }
  });
  
  es.ping({
      // ping usually has a 3000ms timeout
      requestTimeout: Infinity,
  
      // undocumented params are appended to the query string
      hello: "elasticsearch!"
  }, function (error) {
      if (error) {
          console.log(error);
          console.trace('elasticsearch cluster is down!');
      } else {
          console.log('All is well');
      }
  });
  </code></pre>
  
  <p>Which currently returns an authorization error:</p>
  
  <pre><code>{ [Error: Authorization Exception]
    status: 403,
    displayName: 'AuthorizationException',
    message: 'Authorization Exception' }
  </code></pre>
  
  <p>I've yet to see a working example of using an aws ES instance by using a signed policy in node.js. Anyone have insights?</p>