Playwright python code works locally with headless browser but fails in AWS Lambda

314 views Asked by At

I have developed a Playwright script that successfully runs on my local machine using a headless browser, but when I deploy it to AWS Lambda, it doesn't work as expected. The Lambda function times out, and the automation doesn't complete as it should.

async def run(self) -> None:
    async with async_playwright() as playwright:
        lead_create_status = "not_created"
        lead_id = "null"

        custom_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"

        identifier_logger = self.logger_generator.get_logger(YesBankCc.IDENTIFIER)
        browser = await playwright.chromium.launch(headless=True, args=["--single-process"])
        wait_until = "load"
        # context = await browser.new_context(user_agent=custom_user_agent,
        #                                     geolocation={"longitude": 72.877655, "latitude": 19.075983},
        #                                     permissions=["geolocation"])
        geolocator = Nominatim(user_agent="MyApp")

        location = geolocator.geocode("Mumbai")

        print("The latitude of the location is: ", location.latitude)
        print("The longitude of the location is: ", location.longitude)
        context = await browser.new_context(
            ignore_https_errors=True,
            # java_script_enabled=True,
            user_agent=custom_user_agent,
            viewport={"width": 1920, "height": 1080},
            geolocation={"longitude": location.longitude, "latitude": location.latitude},
            permissions=["geolocation"], )

        identifier_logger.debug("New context created...")
        page = await context.new_page()
        page.set_default_timeout(120000)
        identifier_logger.debug("New page created...")

        # Navigate to login page
        identifier_logger.debug("Navigating to login page...")
        await page.goto(url=self._portal_info["crm_url"], wait_until="load")

        print(f'URL = {self._portal_info["crm_url"]}')

        identifier_logger.info("Successfully navigated to login page")
        identifier_logger.debug("Filling in login details...")

        await sleep(10)
        identifier_logger.debug("Waited for 10 seconds")

        identifier_logger.debug("Going to click mobile number field")

        await page.locator('[id="username"]').click()

        # await page.get_by_placeholder('Enter Mobile Number').click()
        identifier_logger.debug("Clicked enter mobile number field")

        await page.locator('[id="username"]').fill(value=str(self._portal_info["crm_username"]))

        # await page.get_by_placeholder('Enter Mobile Number').fill(value=str(self._portal_info["crm_username"]))
        identifier_logger.debug("Filled enter mobile number field")
        await page.wait_for_load_state('domcontentloaded')
        await page.locator(
            '[class="btn-primary btndisable mat-button mat-button-base ng-star-inserted"]').click()
        identifier_logger.debug("Send OTP button clicked")
        identifier_logger.debug("Login OTP Sent successfully..."

)

Note we are using this script inside the docker image in that image we have a lambda handler and lambda layers inside the image. we have an internal middleware system to take care that works It also runs perfectly for most of the automated tasks that run on my local Linux machine. But this one website automation script needs the latitude and longitude to run headless on a local machine but that code doesn't work in lambda

My questions are:

  1. Are there any known limitations or differences when using Playwright in AWS Lambda compared to a local environment?

  2. What could be causing the Lambda function to time out, and how can I troubleshoot and resolve this issue?

  3. Are there any specific configurations or considerations I should be aware of when deploying Playwright code in an AWS Lambda function?

I appreciate any insights or suggestions on how to make my Playwright code run smoothly in AWS Lambda. Thank you for your help!

Environment Details:

AWS Lambda runtime: Python 3.9, Playwright version: 1.35.0, AWS region: ap-south-1

1

There are 1 answers

1
Abhishek On

I am also having this issue. My code is working fine in python locally as soon as i am trying to run on lambda it gives an error. What i found out that i was using asyncio in my code. After the remove it from requirements.txt and from my code, it works through sam local run. See if you are also using the same