title: How can I list all my Amazon EC2 instances using Node.js in AWS Lambda?
body: <p>I'm on AWS and using <a href="https://aws.amazon.com/sdk-for-node-js/" rel="noreferrer">AWS SDK for JavaScript in Node.js</a>. I'm trying to build an AWS Lambda function and inside I want to get a list of all my Amazon EC2 instances, but I just can't seem to get it working. Can anyone spot what I'm doing wrong?</p>
  
  <p>Here is my Lambda function code:</p>
  
  <pre><code>var AWS = require('aws-sdk');
  AWS.config.region = 'us-west-1';
  
  exports.handler = function(event, context) {
      console.log("\n\nLoading handler\n\n");
      var ec2 = new AWS.EC2();
      ec2.describeInstances( function(err, data) {
          console.log("\nIn describe instances:\n");
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log("\n\n" + data + "\n\n"); // successful response
      });
      context.done(null, 'Function Finished!');  
  };
  </code></pre>
  
  <p>And this is my policy (I think it's correct?)</p>
  
  <pre><code>{
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [
          "logs:*"
        ],
        "Resource": "arn:aws:logs:*:*:*"
      },
      {
      "Effect": "Allow",
      "Action": [
        "ec2:*"
      ],
      "Resource": "arn:aws:ec2:*"
    },
      {
        "Effect": "Allow",
        "Action": [
          "s3:GetObject",
          "s3:PutObject"
        ],
        "Resource": [
          "arn:aws:s3:::*"
        ]
      }
    ]
  }
  </code></pre>
  
  <p>And if I do a console.log on 'ec2' I get:</p>
  
  <pre><code>{ config: 
     { credentials: 
        { expired: false,
          expireTime: null,
          accessKeyId: 'XXXXXXXXXXXXXXXXXX',
          sessionToken: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          envPrefix: 'AWS' },
       credentialProvider: { providers: [Object] },
       region: 'us-west-1',
       logger: null,
       apiVersions: {},
       apiVersion: null,
       endpoint: 'ec2.us-west-1.amazonaws.com',
       httpOptions: { timeout: 120000 },
       maxRetries: undefined,
       maxRedirects: 10,
       paramValidation: true,
       sslEnabled: true,
       s3ForcePathStyle: false,
       s3BucketEndpoint: false,
       computeChecksums: true,
       convertResponseTypes: true,
       dynamoDbCrc32: true,
       systemClockOffset: 0,
       signatureVersion: 'v4' },
    isGlobalEndpoint: false,
    endpoint: 
     { protocol: 'https:',
       host: 'ec2.us-west-1.amazonaws.com',
       port: 443,
       hostname: 'ec2.us-west-1.amazonaws.com',
       pathname: '/',
       path: '/',
       href: 'https://ec2.us-west-1.amazonaws.com/' } }
  </code></pre>