Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在AWS Lambda上获得docker图像中的无头铬(木偶演员)_Node.js_Docker_Aws Lambda_Puppeteer_Google Chrome Headless - Fatal编程技术网

Node.js 如何在AWS Lambda上获得docker图像中的无头铬(木偶演员)

Node.js 如何在AWS Lambda上获得docker图像中的无头铬(木偶演员),node.js,docker,aws-lambda,puppeteer,google-chrome-headless,Node.js,Docker,Aws Lambda,Puppeteer,Google Chrome Headless,我想创建一个函数,从HTML文件创建pdf,并从AWS Lambda触发它。我已经将函数放在docker映像中,并将docker映像部署到AWS Lambda。当我在本地运行docker时,它工作得很好,但是当我将它部署到AWS Lambda时,它就不工作了 负责创建pdf文件的My app.js: const fs = require('fs'); const puppeteer = require('puppeteer'); const path = require('path'); co

我想创建一个函数,从HTML文件创建pdf,并从AWS Lambda触发它。我已经将函数放在docker映像中,并将docker映像部署到AWS Lambda。当我在本地运行docker时,它工作得很好,但是当我将它部署到AWS Lambda时,它就不工作了

负责创建pdf文件的My app.js:

const fs = require('fs');

const puppeteer = require('puppeteer');
const path = require('path');
const chromium = require('chrome-aws-lambda');

const buildPaths = {
    buildPathHtml: path.resolve('./index.html'),
    buildPathPdf: path.resolve('./my-file.pdf')
};

const { buildPathHtml, buildPathPdf } = buildPaths;

const isFileExist = (filePath) => {
    try {
        fs.statSync(filePath);
        return true;
    } catch (error) {
        return false;
    }
};

const printPdf = async () => {
    if (!isFileExist(buildPathHtml)) {
        throw new Error('HTML file does not exist.');
    }
    console.log('Creating browser...');
    browser = await chromium.puppeteer.launch({
        args: ['--no-sandbox', '--disable-dev-shm-usage', '--disable-web-security'],
        executablePath: await chromium.executablePath,
        headless: true,
        });
    console.log('Browser created. Creating page...');
    const page = await browser.newPage();
    console.log('Page created. Going to html file path...');
    await page.goto(`file:${buildPathHtml}`, { waitUntil: 'networkidle0' });
    console.log('Went to html file path. Creating pdf...');
    const pdf = await page.pdf({
        format: 'A4',
        printBackground: true,
        landscape: true,
        displayHeaderFooter: true,
        headerTemplate: ``,
        footerTemplate: `
            <div style="font-size: 10px; font-family: 'Raleway'; font-weight: bold; width: 1000px; text-align: center; color: grey; padding-left: 10px;">
                <span>Page: </span>
                <span class="pageNumber"></span> / <span class="totalPages"></span>
                <span> - Date: </span>
                <span class="date"></span>
            </div>`,
        margin: {
            top: '20px',
            right: '20px',
            bottom: '40px',
            left: '20px'
        }
    });
    console.log('Pdf created. Closing the browser...');
    await browser.close();
    console.log('Succesfully created the PDF');
    fs.writeFileSync(buildPathPdf, pdf);
    return {
        headers: {
            "Content-type": "application/pdf",
        },
        statusCode: 200,
        body: pdf.toString("base64"),
        isBase64Encoded: true,
        };
    
};

exports.lambdaHandler = async (event) => {
  try {
        console.log('Handler invoked. Event: ', event);
        const response = await printPdf();
        console.log('Response generated. response: ', response);
        return response;
    } catch (error) {
        console.log('Error generating PDF', error);
    }
}

exports.lambdaHandler();
我使用以下命令生成docker:

docker build -t my-function .
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
然后我像这样运行它:

docker run -p 9000:8080 my-function:latest
现在要在本地测试它,我运行以下命令:

docker build -t my-function .
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
而且效果很好。但当我将其部署到AWS Lambda并从那里调用测试时,我得到以下错误:

START RequestId: 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570 Version: $LATEST
2021-03-18T22:06:30.748Z    3de1cb55-eee5-4c82-a3c4-4dabf7eb9570    INFO    Handler invoked. Event:  {}
2021-03-18T22:06:30.750Z    3de1cb55-eee5-4c82-a3c4-4dabf7eb9570    INFO    Creating browser...
2021-03-18T22:06:36.269Z    3de1cb55-eee5-4c82-a3c4-4dabf7eb9570    INFO    Error generating PDF Error: Protocol error (Target.setDiscoverTargets): Target closed.
    at /var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:71:63
    at new Promise (<anonymous>)
    at Connection.send (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Connection.js:70:16)
    at Function.create (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/common/Browser.js:116:26)
    at ChromeLauncher.launch (/var/task/node_modules/puppeteer/lib/cjs/puppeteer/node/Launcher.js:101:56)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async printPdf (/var/task/app.js:27:12)
    at async Runtime.exports.lambdaHandler [as handler] (/var/task/app.js:75:20)
END RequestId: 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570
REPORT RequestId: 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570  Duration: 5540.02 ms    Billed Duration: 6366 ms    Memory Size: 512 MB Max Memory Used: 377 MB Init Duration: 825.77 ms
启动请求ID:3de1cb55-eee5-4c82-a3c4-4dabf7eb9570版本:$LATEST
2021-03-18T22:06:30.748Z 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570已调用信息处理程序。事件:{}
2021-03-18T22:06:30.750Z 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570信息创建浏览器。。。
2021-03-18T22:06:36.269Z 3de1cb55-eee5-4c82-a3c4-4dabf7eb9570信息错误生成PDF错误:协议错误(Target.setDiscoverTargets):目标关闭。
at/var/task/node_modules/puppeter/lib/cjs/puppeter/common/Connection.js:71:63
在新的承诺()
在Connection.send(/var/task/node_modules/puppeter/lib/cjs/puppeter/common/Connection.js:70:16)
在Function.create(/var/task/node_modules/puppeter/lib/cjs/puppeter/common/Browser.js:116:26)
在ChromeLauncher.launch(/var/task/node_modules/puppeter/lib/cjs/puppeter/node/Launcher.js:101:56)
在处理和拒绝时(内部/process/task_queues.js:97:5)
异步打印PDF(/var/task/app.js:27:12)
在async Runtime.exports.lambdaHandler[作为处理程序](/var/task/app.js:75:20)
结束请求ID:3de1cb55-eee5-4c82-a3c4-4dabf7eb9570
报告请求ID:3de1cb55-eee5-4c82-a3c4-4dabf7eb9570持续时间:5540.02毫秒计费持续时间:6366毫秒内存大小:512 MB最大使用内存:377 MB初始持续时间:825.77毫秒

谷歌chrome stable似乎尚未成功安装在AWS Lambda环境中。有什么好办法吗?

嘿,你把它修好了吗?@lilverman还没有