如何在AWS Lambda中使用Node.js列出我的所有Amazon EC2实例?

时间:2014-12-29 13:42:35

标签: node.js amazon-web-services amazon-ec2 aws-lambda

我在AWS上使用AWS SDK for JavaScript in Node.js。我正在尝试构建一个AWS Lambda函数,我希望获得所有Amazon EC2实例的列表,但我似乎无法让它运行起来。谁能发现我做错了什么?

这是我的Lambda函数代码:

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!');  
};

这是我的政策(我认为这是正确的吗?)

{
  "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:::*"
      ]
    }
  ]
}

如果我在' ec2'上做了一个console.log我明白了:

{ 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/' } }

2 个答案:

答案 0 :(得分:13)

最可能的原因是您在完成对EC2 DescribeInstances API的调用之前明确终止了Lambda函数。

原因是Lambda假设您的代码在调用context.done(...)后立即执行完毕。这是console.log(... data ...)调用之前发生的。

这种奇怪的排序是因为NodeJS的工作方式以及AWS SDK for JavaScript的工作原理。在NodeJS中,您永远不应该阻止执行。对Web服务(例如EC2)的调用将阻止执行。因此,AWS SDK for JavaScript(以及大多数NodeJS库)通过进行异步调用来工作。

通常,当您进行异步调用时,会将回调函数传递给该调用。结果准备就绪后,NodeJS将执行回调功能。

在您的代码中,function(err, data) {...}回调函数。这不会立即执行,但会在NodeJS看到ec2.describeInstances调用已收到其结果时安排执行。

一旦你安排执行你的回电,你就打电话给context.done(...),告诉Lambda:我已经完成了,你可以杀了我< / em>的。在EC2 DescribeInstances调用收到数据并将其传递给回调函数之前,它很乐意遵守和中断您的功能。

如何解决问题?

现在答案应该是明确的:只需将context.done(...)来电转移到回调功能内,紧接在包含console.log(...data...)来电的if / else块之后:< / p>

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!');  
});

答案 1 :(得分:0)

截至2019年(10月),给出的答案无济于事,挖掘后我发现现在它是基于诺言的

exports.handler = async function(event) {
  const promise = new Promise(function(resolve, reject) {
      //your logic, goes here at the end just call resolve 
      resolve("data you want to return"); // here lamda exits 
    })
  return promise;// lamda does not  exits here, it waits for to resolve
}