Node.js 将请求转发给内部服务lambda AWS
我需要将接收到的http请求转发给lambda函数到另一个url(ECS服务)并发送回响应 我通过以下代码实现此行为:Node.js 将请求转发给内部服务lambda AWS,node.js,amazon-web-services,aws-lambda,Node.js,Amazon Web Services,Aws Lambda,我需要将接收到的http请求转发给lambda函数到另一个url(ECS服务)并发送回响应 我通过以下代码实现此行为: exports.handler = async (event) => { const response = { statusCode: 302, // also tried 301 headers: { Location: 'http://ec2-xx-yy-zz-ww.us-west-x.compute.a
exports.handler = async (event) => {
const response = {
statusCode: 302, // also tried 301
headers: {
Location: 'http://ec2-xx-yy-zz-ww.us-west-x.compute.amazonaws.com:2222/healthcheck'
}
};
return response;
};
它似乎可以工作,但这会将原始url(类似于toing.co:5500)更改为重定向的aws url
因此,我尝试在lambda中创建一个异步请求,该请求将查询并返回响应:
const http = require('http');
const doPostRequest = () => {
const data = {};
return new Promise((resolve, reject) => {
const options = {
host: 'http://ec2-xx-yy-zz-ww.us-west-x.compute.amazonaws.com:5112/healthcheck',
port: "2222",
path: '/healthcheck',
method: 'POST'
};
const req = http.request(options, (res) => {
resolve(JSON.stringify(res.statusCode));
});
req.on('error', (e) => {
reject(e.message);
});
//do the request
req.write(JSON.stringify(data));
req.end();
});
};
exports.handler = async (event) => {
await doPostRequest()
.then(result => console.log(`Status code: ${result}`))
.catch(err => console.error(`Error doing the request for the event: ${JSON.stringify(event)} => ${err}`));
};
const http = require('http')
let response = {
statusCode: 200,
headers: {'Content-Type': 'application/json'},
body: ""
}
let requestOptions = {
timeout: 10,
host: "ec2-x-xxx-xx-xxx.xx-xx-x.compute.amazonaws.com",
port: 2222,
path: "/healthcheck",
method: "POST"
}
let request = async (httpOptions, data) => {
return new Promise((resolve, reject) => {
let req = http.request(httpOptions, (res) => {
let body = ''
res.on('data', (chunk) => { body += chunk })
res.on('end', () => { resolve(body) })
})
req.on('error', (e) => {
reject(e)
})
req.write(data)
req.end()
})
}
exports.handler = async (event, context) => {
try {
let result = await request(requestOptions, JSON.stringify({v: 1}))
response.body = JSON.stringify(result)
return response
} catch (e) {
response.body = `Internal server error: ${e.code ? e.code : "Unspecified"}`
return response
}
}
但是我得到了一个坏网关(502)错误。如何为post请求实现一个简单的转发器(带有消息体)?问题是lambda函数的响应是一个普通的
json
字符串,而不是html(正如@acorbel所指出的),因此负载平衡器无法处理响应,导致502错误
解决方案是向响应中添加http头和状态代码:
const http = require('http');
const doPostRequest = () => {
const data = {};
return new Promise((resolve, reject) => {
const options = {
host: 'http://ec2-xx-yy-zz-ww.us-west-x.compute.amazonaws.com:5112/healthcheck',
port: "2222",
path: '/healthcheck',
method: 'POST'
};
const req = http.request(options, (res) => {
resolve(JSON.stringify(res.statusCode));
});
req.on('error', (e) => {
reject(e.message);
});
//do the request
req.write(JSON.stringify(data));
req.end();
});
};
exports.handler = async (event) => {
await doPostRequest()
.then(result => console.log(`Status code: ${result}`))
.catch(err => console.error(`Error doing the request for the event: ${JSON.stringify(event)} => ${err}`));
};
const http = require('http')
let response = {
statusCode: 200,
headers: {'Content-Type': 'application/json'},
body: ""
}
let requestOptions = {
timeout: 10,
host: "ec2-x-xxx-xx-xxx.xx-xx-x.compute.amazonaws.com",
port: 2222,
path: "/healthcheck",
method: "POST"
}
let request = async (httpOptions, data) => {
return new Promise((resolve, reject) => {
let req = http.request(httpOptions, (res) => {
let body = ''
res.on('data', (chunk) => { body += chunk })
res.on('end', () => { resolve(body) })
})
req.on('error', (e) => {
reject(e)
})
req.write(data)
req.end()
})
}
exports.handler = async (event, context) => {
try {
let result = await request(requestOptions, JSON.stringify({v: 1}))
response.body = JSON.stringify(result)
return response
} catch (e) {
response.body = `Internal server error: ${e.code ? e.code : "Unspecified"}`
return response
}
}
您不会从lambda返回响应。API网关无法处理无效的lambda响应(statusCode*,body)我没有使用API网关,这只是发送到负载平衡器的请求,需要转发。为什么要将HTTP请求代理到lambda中的ECS?我在ECS上的不同端口上侦听不同的服务,我想读取body,并将请求发送到应该发送的位置