Skip to content

Instantly share code, notes, and snippets.

@aditodkar
Last active January 10, 2023 13:40
Show Gist options
  • Save aditodkar/588b83fa82080ac1dc5755cb4e960a68 to your computer and use it in GitHub Desktop.
Save aditodkar/588b83fa82080ac1dc5755cb4e960a68 to your computer and use it in GitHub Desktop.

I am trying to run puppeteer with proxy chain package on aws lambda but I am getting this error message:

"errorType": "Error", "errorMessage": "Protocol error (Target.createTarget): Target closed."

const chromium = require('chrome-aws-lambda');
const { addExtra } = require("puppeteer-extra");
const puppeteerExtra = addExtra(chromium.puppeteer);
const proxyChain = require('proxy-chain');
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
puppeteerExtra.use(StealthPlugin());
exports.handler = async (event, context, callback) => {
let finalResult = [];
const url = ``;
let browser;
const oldProxyUrl = ''; // --> bright data proxy
const newProxyUrl = await proxyChain.anonymizeProxy(oldProxyUrl);
console.log("newProxyUrl", newProxyUrl)
try {
browser = await puppeteerExtra.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox', `--proxy-server=${newProxyUrl}`],
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless
});
const page = await browser.newPage();
await page.goto(url);
finalResult = await extractElements(page);
} catch (error) {
return callback(error);
} finally {
await browser.close();
await proxyChain.closeAnonymizedProxy(newProxyUrl, true);
}
return callback(null, finalResult);
};
@chuongtrh
Copy link

@aditodkar Did you test well at local? If yes, maybe AWS lambda block proxy. let's try to config allow proxy on lambda

@aditodkar
Copy link
Author

Sorry did not get you. Are you saying I have to enable proxy settings for lambda function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment