当在 AWS Lambda 上使用 Node.js 16 运行时时,可能会遇到容器镜像错误,错误消息为“Missing Runtime API Server configuration”。要解决此问题,需要在 Lambda 函数及其相关功能访问的 IAM 角色上添加一些权限。具体步骤如下:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface"
],
"Resource": "*"
}
]
}
const binaryMimeTypes = [
'application/javascript',
'application/json',
'application/octet-stream',
'application/xml',
'font/eot',
'font/opentype',
'font/otf',
'image/jpeg',
'image/png',
'image/svg+xml',
'text/comma-separated-values',
'text/css',
'text/html',
'text/javascript',
'text/plain',
'text/text',
'text/xml'
];
const server = createServer((req, res) => {
const done = finalhandler(req, res)
return handler(req, res, (error) => {
done(error)
})
})
server.on('error', err => {
console.error('Server error:', err)
})
server.listen(8080, () => {
console.log('Listening on http://localhost:8080')
// When running in a container, we need to bind to 0.0.0.0
if (process.env.LAMBDA_RUNTIME_API) {
server.emit('listening', null)
}
})
if (process.env.NODE_ENV === 'test') {
module.exports = server
}