首页 > 解决方案 > 调用 AWS Lambda 容器映像时进程退出并显示错误代码 100

问题描述

我正在关注https://docs.aws.amazon.com/lambda/latest/dg/images-create.html并从 AWS 基础映像为 Lambda 创建了一个映像,并在将其推送到 ECR 之前先在本地对其进行了测试。在本地调用函数时,我得到了预期的结果,即错误代码 0。但是在 ECR 上推送图像并从 AWS lambda 控制台调用它之后,我得到了以下问题。

START RequestId: 55930a26-5d88-4d1f-9a5b-14599b369585 Version: $LATEST
[04:46:19] I/launcher - Running 1 instances of WebDriver
[04:46:19] I/direct - Using ChromeDriver directly...
[04:46:22] E/runner - Unable to start a WebDriver session.
[04:46:22] E/launcher - Error: NoSuchSessionError: invalid session id
at Object.throwDecodedError (/var/task/node_modules/selenium-webdriver/lib/error.js:514:15)
at parseHttpResponse (/var/task/node_modules/selenium-webdriver/lib/http.js:519:13)
at /var/task/node_modules/selenium-webdriver/lib/http.js:441:30
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Promise.all (index 0)
[04:46:22] E/launcher - Process exited with error code 100
2021-08-11T04:46:22.089Z    55930a26-5d88-4d1f-9a5b-14599b369585    INFO    error code:  100
END RequestId: 55930a26-5d88-4d1f-9a5b-14599b369585
REPORT RequestId: 55930a26-5d88-4d1f-9a5b-14599b369585  Duration: 9465.93 ms    Billed Duration: 10270 ms   Memory Size: 10000 MB   Max Memory Used: 314 MB Init Duration: 803.88 ms    

我的 conf.js

exports.config = {
     directConnect: true,
    ignoreUncaughtExceptions: true,
    SELENIUM_PROMISE_MANAGER: false,
    'specs': ['index.js'],
    jasmineNodeOpts: {
        defaultTimeoutInterval: 1000 * 6,
        realtimeFailure: true,
        showColors: true,
        isVerbose: true,
        includeStackTrace: true,
        displaySpecDuration: true,
        print: function () {},
    },
    'capabilities': {
      'browserName': 'chrome',
        acceptInsecureCerts: true,
        acceptSslCerts: true,
        chromeOptions: {
            binary: "/usr/bin/google-chrome",
            "excludeSwitches": [ "enable-automation" ],
            "useAutomationExtension": false,
            args: ["--no-sandbox","--disable-web-security","--headless","--disable-dev-shm-usage","--disable-extensions", "--disable-gpu", "--start-maximized", "--disable-infobars"]
        }

    },
    framework: "jasmine"
};

我的 package.json

{
  "name": "protractor",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node_modules/.bin/protractor conf.js",
    "runlocal": "node_modules/.bin/sls invoke local --function protractor",
    "deploy": "node_modules/.bin/sls deploy"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "jasmine": "^3.8.0",
    "npm": "^6.14.14",
    "protractor": "^7.0.0",
    "protractor-beautiful-reporter": "^1.3.6"
  }
}

我的码头文件

FROM amazon/aws-lambda-nodejs:12
COPY index.js conf.js handler.js package*.json screenshots ./
ADD install-google-chrome.sh .
RUN chmod +x install-google-chrome.sh
RUN curl https://intoli.com/install-google-chrome.sh | bash
ADD gtk-firefox.sh .
RUN chmod 755 gtk-firefox.sh
RUN ./gtk-firefox.sh
RUN npm install
RUN node ./node_modules/protractor/bin/webdriver-manager clean
RUN node ./node_modules/protractor/bin/webdriver-manager update
CMD [ "handler.runtest" ]

handler.js

'use strict';

module.exports.runtest = (event, context, callback) => {

  var npm = require('npm');
  var path = require('path');
  var childProcess = require('child_process');
  var args = ['conf.js'];

  npm.load(function() {
    var child = childProcess
    .fork(path.join(npm.root, 'protractor/bin/protractor'), args)   
        .on('close', function(errorCode) {
      console.log('error code: ', errorCode);

      // Use this block to have Lambda respond when the tests are done.
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: `Selenium Test executed on Chrome ! Child process Error Code: ${errorCode}`,
        }),
      };
      callback(null, response);
    });
    process.on('SIGINT', child.kill);
  });


};

index.js

const {by} = require("protractor");
const {protractor} = require("protractor");
const {browser} = require("protractor");
describe('Google\'s Search Functionality', function() {
    it('can find search results', async function() {
        const signin = "Sign in"
        await browser.waitForAngularEnabled(false);
        await browser.get('https://google.com/ncr');
        await browser.findElement(by.name('q')).sendKeys('BrowserStack', protractor.Key.ENTER);
        const texti = browser.findElement(by.xpath('//a[contains(@href,"sign_in")]'));
        expect(await texti.getText()).toEqual(signin);
    });
});

所有这些文件都在根项目目录中的同一级别。

我用谷歌搜索了很多,但找不到合适的分辨率。是权限问题吗?因为我不明白是什么让相同的 lambda 容器映像在 lambda 控制台中失败,而不是在本地失败。

标签: google-chromeaws-lambdaprotractorselenium-chromedriver

解决方案


推荐阅读