Skip to content

fix: lambda script - #61

Merged
Perry2004 merged 1 commit into
mainfrom
fix/debug-lambda
Nov 1, 2025
Merged

Perry2004 merged 1 commit into
mainfrom
fix/debug-lambda

Conversation

@Perry2004

Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings November 1, 2025 20:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the Pexels image scraper Lambda function for better compatibility with AWS Lambda environment and improved debugging capabilities. The changes focus on transitioning from ARM64 to AMD64 architecture, adding extensive logging, and improving resource cleanup.

  • Enhanced Playwright browser configuration with Lambda-specific arguments and improved error handling
  • Added comprehensive logging throughout the scraping and Lambda handler execution
  • Improved Docker configuration for AWS Lambda deployment with AMD64 architecture

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
scripts/src/main.ts Added extensive logging, Lambda-specific Chromium configuration, improved resource cleanup with separate page/context/browser closing
scripts/src/lambda-handler.ts Enhanced error logging with stack traces and added execution tracking logs
scripts/build-lambda.sh Changed platform from linux/arm64 to linux/amd64, added ECR push commands with hardcoded AWS account ID
scripts/Dockerfile.script Updated PLAYWRIGHT_BROWSERS_PATH, added cleanup step, and ensured chromium deps are installed

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread scripts/src/main.ts
let page = null;

// Set environment variable to use system Chromium in Lambda
process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH || '/ms-playwright/chromium-*/chrome-linux/chrome';

Copilot AI Nov 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wildcard path '/ms-playwright/chromium-*/chrome-linux/chrome' will not work at runtime. Shell glob patterns are not resolved when setting environment variables. You need to use an explicit path or resolve the glob pattern programmatically using the fs module.

Copilot uses AI. Check for mistakes.
Comment thread scripts/build-lambda.sh
Comment on lines +3 to +11
docker buildx build --platform linux/amd64 --provenance=false -f Dockerfile.script -t pwp/pexels-image-scraper-lambda .
# docker run --platform linux/amd64 -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
# --entrypoint /aws-lambda/aws-lambda-rie \
# lambda-script:test \
# /usr/local/bin/npx aws-lambda-ric dist/lambda-handler.handler

docker tag pwp/pexels-image-scraper-lambda:latest 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest

docker push 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest No newline at end of file

Copilot AI Nov 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AWS account ID (279892746640) is hardcoded in the script. This should be parameterized using an environment variable (e.g., AWS_ACCOUNT_ID) to avoid exposing sensitive account information and to make the script reusable across different environments.

Suggested change
docker buildx build --platform linux/amd64 --provenance=false -f Dockerfile.script -t pwp/pexels-image-scraper-lambda .
# docker run --platform linux/amd64 -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
# --entrypoint /aws-lambda/aws-lambda-rie \
# lambda-script:test \
# /usr/local/bin/npx aws-lambda-ric dist/lambda-handler.handler
docker tag pwp/pexels-image-scraper-lambda:latest 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest
docker push 279892746640.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest
if [ -z "$AWS_ACCOUNT_ID" ]; then
echo "Error: AWS_ACCOUNT_ID environment variable is not set."
exit 1
fi
docker buildx build --platform linux/amd64 --provenance=false -f Dockerfile.script -t pwp/pexels-image-scraper-lambda .
# docker run --platform linux/amd64 -v ~/.aws-lambda-rie:/aws-lambda -p 9000:8080 \
# --entrypoint /aws-lambda/aws-lambda-rie \
# lambda-script:test \
# /usr/local/bin/npx aws-lambda-ric dist/lambda-handler.handler
docker tag pwp/pexels-image-scraper-lambda:latest ${AWS_ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest
docker push ${AWS_ACCOUNT_ID}.dkr.ecr.us-west-2.amazonaws.com/pwp/pexels-image-scraper-lambda:latest

Copilot uses AI. Check for mistakes.
Comment thread scripts/src/main.ts
Comment on lines +108 to +115
try {
page = await context.newPage();
console.log("New page created successfully");
} catch (pageError) {
console.error("Failed to create new page");
console.error(`Browser connected: ${browser.isConnected()}`);
throw pageError;
}

Copilot AI Nov 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This nested try-catch block adds unnecessary complexity. The pageError will be caught by the outer catch block (line 179), making this inner try-catch redundant. The browser.isConnected() check could be added to the outer error handling instead.

Copilot uses AI. Check for mistakes.
@Perry2004
Perry2004 merged commit 46bb849 into main Nov 1, 2025
8 checks passed
@Perry2004
Perry2004 deleted the fix/debug-lambda branch November 1, 2025 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants