-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_lambda_function.py
More file actions
36 lines (31 loc) · 1.09 KB
/
test_lambda_function.py
File metadata and controls
36 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import boto3
import datetime
from playwright.sync_api import sync_playwright
s3 = boto3.client('s3')
# Set the environment variable within the script
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = "/opt/playwright-browsers"
def run_playwright():
"""
Launches a headless Chromium browser using Playwright, navigates to a website,
takes a screenshot, and saves it locally as 'screenshot.png'.
"""
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
try:
page = browser.new_page()
page.goto("https://www.whatismybrowser.com")
page.screenshot(path="/tmp/screenshot.png")
finally:
browser.close()
def lambda_handler(event, context):
run_playwright()
key = f"screenshot_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
print(f"Uploading screenshot to S3 with key: {key}")
s3.upload_file('/tmp/screenshot.png', 'bucket-playwright', key)
return {
'statusCode': 200,
'body': 'Screenshot uploaded to S3!'
}
if __name__ == '__main__':
lambda_handler(None, None)