Skip to content

Commit 3fc5309

Browse files
authored
Add example lambda build script (#24)
1 parent ffe33c7 commit 3fc5309

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Perfect for AWS Lambda and other serverless environments. Uses [@sparticuz/chrom
3030

3131
> [!IMPORTANT]
3232
> This project uses Playwright, which ships with a specific version of Chromium. You need to install the matching version of `@sparticuz/chromium`. For example, we are using [Playwright 1.48](https://playwright.dev/docs/release-notes#version-148) (which supports to Chromium 130), you should install `@sparticuz/chromium@130`.
33-
> For running on AWS Lambda, lambda layer with ARM64 architecture is preferred. You will also need to install dependencies of canvas.
33+
>
34+
> For running on AWS Lambda, lambda layer with ARM64 architecture is preferred. You will also need to install dependencies of canvas. Here is an example of [Lambda Layer Build Script](./lambda-layer-build.sh).
3435
3536
```typescript
3637
import { BrowserAgent } from "@lightfeed/browser-agent";

lambda-layer-build.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# Lambda Layer Build Script for Serverless Browser Agent
4+
# This script builds a lambda layer with all necessary dependencies for running
5+
# browser-agent in AWS Lambda environment (ARM64)
6+
7+
# Navigate to the layer directory
8+
cd lambda-layers/serverless-browser
9+
10+
# Clean up existing files
11+
rm -rf lib
12+
rm -rf nodejs/node_modules
13+
rm -rf nodejs/package-lock.json
14+
rm -rf chromium
15+
rm -rf tmp
16+
17+
# Create lib directory if it doesn't exist
18+
mkdir -p lib
19+
20+
# Build using Amazon Linux 2023 ARM64 Docker image with all canvas dependencies
21+
# Reference: https://github.com/charoitel/lambda-layer-canvas-nodejs/blob/ac16c9af8eee82fd64e29f8f178debfe78a32026/build-layer.sh
22+
docker run --rm -v $(pwd):/var/task --platform linux/arm64 amazonlinux:2023 bash -c "\
23+
yum update -y && \
24+
yum groupinstall 'Development Tools' -y && \
25+
yum install gcc-c++ cairo-devel pango-devel libjpeg-turbo-devel giflib-devel librsvg2-devel pango-devel bzip2-devel python3 -y && \
26+
# Install Node.js 20
27+
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash - && \
28+
yum install -y nodejs && \
29+
node --version && npm --version && \
30+
cd /var/task/nodejs && \
31+
npm init -y && \
32+
npm install canvas@3.1.0 --build-from-source && \
33+
echo '=== Copying shared libraries ===' && \
34+
mkdir -p /var/task/lib && \
35+
echo 'Copying direct dependencies...' && \
36+
ldd node_modules/canvas/build/Release/canvas.node | grep '=>' | awk '{print \$3}' | xargs -I{} cp -L {} /var/task/lib/ && \
37+
echo '=== Testing canvas in container ===' && \
38+
LD_LIBRARY_PATH=/var/task/lib node -e \"try { const canvas = require('canvas'); console.log('Canvas loaded successfully'); } catch(e) { console.error('Error:', e); }\" && \
39+
echo '=== Installing Chromium ===' && \
40+
mkdir -p /tmp/assets && \
41+
mkdir -p /var/task/tmp/bin && \
42+
echo 'Downloading Chromium pack...' && \
43+
curl -SL --retry 5 --retry-delay 10 -o /tmp/assets/chromium-pack.tar https://github.com/Sparticuz/chromium/releases/download/v137.0.0/chromium-v137.0.0-pack.arm64.tar && \
44+
echo 'Extracting Chromium pack...' && \
45+
tar -xf /tmp/assets/chromium-pack.tar -C /var/task/tmp/bin/ && \
46+
if [ -d /var/task/tmp/bin/bin ]; then mv /var/task/tmp/bin/bin/* /var/task/tmp/bin/; rmdir /var/task/tmp/bin/bin; fi && \
47+
chmod -R a+r /var/task/tmp/bin/* && \
48+
chmod a+x /var/task/tmp/bin && \
49+
echo '=== Verifying Chromium setup ===' && \
50+
ls -lR /var/task/tmp/bin && \
51+
rm -rf /tmp/assets"
52+
53+
# Set correct permissions
54+
chmod -R 755 nodejs/node_modules
55+
chmod -R 755 lib
56+
chmod -R 755 tmp
57+
58+
echo "=== Layer Build Complete ==="
59+
echo "The layer has been built for AWS Lambda (ARM64) environment."
60+
echo "Do not attempt to test the libraries locally as they are built for Lambda's environment."
61+
echo ""
62+
echo "Next steps:"
63+
echo "1. Zip the contents of this directory to create your Lambda layer"
64+
echo "2. Upload the layer to AWS Lambda"
65+
echo "3. Reference the layer in your Lambda function configuration"

0 commit comments

Comments
 (0)