我使用的是serverless框架和AWS (node.js)。
lambda使用axios进行两个API调用-一个GET和一个POST。当在本地运行该函数时,一切都很好,但是当我在AWS中部署和运行它时,POST请求就不会发生,但是在日志中没有收到任何错误消息。
我看到了以前关于这个问题的一些问题(例如here),它们都提到需要在运行在VPC中的函数中定义一些东西。这个特定的函数没有配置VPC,所以我认为这不会是问题所在。
我还在serverless.yml中启用了cors (见下文)。
任何帮助都将不胜感激。
serverless.yml
service: someservice
app: makingapps
org: orgname
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 11111
httpApi:
cors: true
functions:
myfunc:
handler: myfunc.myfunc
events:
- httpApi:
path: /
method: get
plugins:
- serverless-offlinemyFunc.js
const axios = require('axios');
module.exports.myFunc = async (event) => {
async function sendEmail(number) {
console.log('[sendingEmail] function started' ) //Shows in the logs
const message = `The number is ${number}`
let options = {
method: 'POST',
url: 'https://rapidprod-sendgrid-v1.p.rapidapi.com/mail/send',
headers: {
'content-type': 'application/json',
'x-rapidapi-key': 'key-hash',
'x-rapidapi-host': 'hostname.com'
},
data: {
foo: [{'bar_key': 'bar_value'}],
content: [{type: 'text/plain', value: message}]
}
};
try {
const emailResponse = await axios(options)
console.log(emailResponse.data); //Only shows in the log when run locally
} catch(error) {
console.error(error); //Doesn't show in the log
}
}
const response = {
statusCode: 200,
body: null,
};
try {
const numResponse = await axios.get('https://example.com');
const number = numResponse.data
console.log(number) //Shows on logs
response.body = 'The current number is: ' + number;
response.statusCode = 200;
sendEmail(number);
console.log('[main] after sendEmail function') //Shows in logs
} catch (err) {
response.body = err;
response.statusCode = 'Error';
} finally {
console.log('Here is the response: ', response.body);
return response;
}
};发布于 2021-02-23 05:47:32
sendMail是一个async函数。你必须await它。
替换
sendEmail(number);await sendEmail(number);https://stackoverflow.com/questions/66319392
复制相似问题