AWS Lambda - 无法调用第三个资源
AWS Lambda - unable to make call to 3rd resource
我在 API 网关后面有 Lambda 节点功能,我想做的是:
- 打电话给API
- 做一些逻辑
- 向 Slack 发送消息
问题:
当我 运行 默认 test 请求时,该函数成功完成但它不会向 Slack 发送消息(日志仅打印 Slack 而不是 HAHA1/2),但是,当我 运行 从本地机器执行此功能时,它会执行此操作,这让我相信 AWS 会停止任何 none 客户端-服务器流量。我的功能不在 VPC 中。我该怎么做才能允许传出流量到 Slack?谢谢。
const AWS = require('aws-sdk');
const Slack = require('slack-node');
const dynamo = new AWS.DynamoDB.DocumentClient();
/**
* Demonstrates a simple HTTP endpoint using API Gateway. You have full
* access to the request and response payload, including headers and
* status code.
*
* To scan a DynamoDB table, make a GET request with the TableName as a
* query string parameter. To put, update, or delete an item, make a POST,
* PUT, or DELETE request respectively, passing in the payload to the
* DynamoDB API as a JSON body.
*/
sendSlack()
function sendSlack() {
webhookUri = "https://hooks.slack.com/services/....";
slack = new Slack();
slack.setWebhook(webhookUri);
console.log('Slack');
slack.webhook({
channel: "...",
username: "...",
text: "..."
}, function(err, response) {
console.log("HAHA1");
console.log(response);
console.log("HAHA2");
});
}
exports.handler = async (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));
sendSlack()
let body;
let statusCode = '200';
const headers = {
'Content-Type': 'application/json',
};
try {
switch (event.httpMethod) {
case 'DELETE':
body = await dynamo.delete(JSON.parse(event.body)).promise();
break;
case 'GET':
body = await dynamo.scan({ TableName: event.queryStringParameters.TableName }).promise();
break;
case 'POST':
body = await dynamo.put(JSON.parse(event.body)).promise();
break;
case 'PUT':
body = await dynamo.update(JSON.parse(event.body)).promise();
break;
default:
throw new Error(`Unsupported method "${event.httpMethod}"`);
}
} catch (err) {
statusCode = '400';
body = err.message;
} finally {
body = JSON.stringify(body);
}
return {
statusCode,
body,
headers,
};
};
可能是您的 lambda 处理程序在成功执行对 Slack 的调用之前完成。
由于对 Slack 的调用是异步的,我建议您在处理程序 returns 响应之前尝试 await
它的响应。
我不是 100% 确定,我的猜测是异步 Slack 调用被安排但 运行 直到函数的主体完成,此时 lambda 被“停转” " 没有什么比 运行.
您需要通过提供回调或承诺来等待松弛调用:
function sendSlack() {
return new Promise((resolve, reject) => {
webhookUri = "https://hooks.slack.com/services/....";
slack = new Slack();
slack.setWebhook(webhookUri);
console.log('Slack');
slack.webhook({
channel: "...",
username: "...",
text: "..."
}, function(err, response) {
if(err) return reject(err)
resolve(response)
});
})
}
然后在你的代码中
await sendSlack()
我在 API 网关后面有 Lambda 节点功能,我想做的是:
- 打电话给API
- 做一些逻辑
- 向 Slack 发送消息
问题:
当我 运行 默认 test 请求时,该函数成功完成但它不会向 Slack 发送消息(日志仅打印 Slack 而不是 HAHA1/2),但是,当我 运行 从本地机器执行此功能时,它会执行此操作,这让我相信 AWS 会停止任何 none 客户端-服务器流量。我的功能不在 VPC 中。我该怎么做才能允许传出流量到 Slack?谢谢。
const AWS = require('aws-sdk');
const Slack = require('slack-node');
const dynamo = new AWS.DynamoDB.DocumentClient();
/**
* Demonstrates a simple HTTP endpoint using API Gateway. You have full
* access to the request and response payload, including headers and
* status code.
*
* To scan a DynamoDB table, make a GET request with the TableName as a
* query string parameter. To put, update, or delete an item, make a POST,
* PUT, or DELETE request respectively, passing in the payload to the
* DynamoDB API as a JSON body.
*/
sendSlack()
function sendSlack() {
webhookUri = "https://hooks.slack.com/services/....";
slack = new Slack();
slack.setWebhook(webhookUri);
console.log('Slack');
slack.webhook({
channel: "...",
username: "...",
text: "..."
}, function(err, response) {
console.log("HAHA1");
console.log(response);
console.log("HAHA2");
});
}
exports.handler = async (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));
sendSlack()
let body;
let statusCode = '200';
const headers = {
'Content-Type': 'application/json',
};
try {
switch (event.httpMethod) {
case 'DELETE':
body = await dynamo.delete(JSON.parse(event.body)).promise();
break;
case 'GET':
body = await dynamo.scan({ TableName: event.queryStringParameters.TableName }).promise();
break;
case 'POST':
body = await dynamo.put(JSON.parse(event.body)).promise();
break;
case 'PUT':
body = await dynamo.update(JSON.parse(event.body)).promise();
break;
default:
throw new Error(`Unsupported method "${event.httpMethod}"`);
}
} catch (err) {
statusCode = '400';
body = err.message;
} finally {
body = JSON.stringify(body);
}
return {
statusCode,
body,
headers,
};
};
可能是您的 lambda 处理程序在成功执行对 Slack 的调用之前完成。
由于对 Slack 的调用是异步的,我建议您在处理程序 returns 响应之前尝试 await
它的响应。
我不是 100% 确定,我的猜测是异步 Slack 调用被安排但 运行 直到函数的主体完成,此时 lambda 被“停转” " 没有什么比 运行.
您需要通过提供回调或承诺来等待松弛调用:
function sendSlack() {
return new Promise((resolve, reject) => {
webhookUri = "https://hooks.slack.com/services/....";
slack = new Slack();
slack.setWebhook(webhookUri);
console.log('Slack');
slack.webhook({
channel: "...",
username: "...",
text: "..."
}, function(err, response) {
if(err) return reject(err)
resolve(response)
});
})
}
然后在你的代码中
await sendSlack()