Tôi đang sử dụng AWS và sử dụng AWS SDK for JavaScript in Node.js. Tôi đang cố gắng xây dựng một hàm AWS Lambda và bên trong tôi muốn lấy danh sách tất cả các cá thể Amazon EC2 của mình, nhưng tôi dường như không thể làm cho nó hoạt động được. Bất cứ ai có thể phát hiện ra những gì tôi đang làm sai?Tôi có thể liệt kê tất cả các cá thể Amazon EC2 của mình bằng cách sử dụng Node.js trong AWS Lambda như thế nào?
Đây là mã chức năng Lambda của tôi:
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!');
};
Và đây là chính sách của tôi (? Tôi nghĩ đó là đúng)
{
"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:::*"
]
}
]
}
Và nếu tôi làm một console.log vào 'EC2' Tôi get:
{ 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/' } }
Cảm ơn bạn @Bruno. Làm thế nào để bạn hoàn thành cùng một vòng lặp - như tạo 10 hàng đợi SQS, chèn 20 thông điệp [batch api tồn tại, bên cạnh đó], nếu tôi đặt context.done() bên trong vòng lặp, điều đó không có nghĩa là lặp lại sẽ dừng sau khi thực hiện đơn lẻ (1 sau đó thực hiện một phần) –