@sparticuz/chromium

repository·master·Indexed 23 days ago

https://github.com/sparticuz/chromium

A Chromium build tailored for serverless platforms like AWS Lambda. It provides Brotli-compressed binaries and optimized arguments for headless execution in resource-constrained environments. The package requires puppeteer-core or playwright-core as a production dependency and offers a minimized version via @sparticuz/chromium-min for strict file size limits.

Tokens
8.4K
Snippets
17
Records
29
Agent score
82%

What's inside @sparticuz/chromium

  1. Configure `AUTOMATION_TOKEN` (GitHub PAT)

    master

    The AUTOMATION_TOKEN is a GitHub Personal Access Token (PAT) required for two critical automation steps:

    1. repository_dispatch: The EC2 instance calls the GitHub API to trigger build-complete.yml upon build success or failure. This requires the contents:write scope.
    2. Tag Pushing: The prepare-release.yml workflow uses this token to push tags (git push origin master --follow-tags). Using the default GITHUB_TOKEN will not trigger downstream workflows like release.yml. This also requires contents:write.

    Recommended Scopes:

    • Fine-grained PAT: contents:write
    • Classic PAT: repo
  2. Upload Chromium Lambda layer to AWS manually

    master

    To use @sparticuz/chromium as a pre-existing AWS Lambda Layer, follow these manual steps:

    1. Download the layer: Get the layer .zip file from the GitHub Releases page.
    2. Upload to S3: Create an S3 bucket (or use an existing one), upload the .zip file, and copy its S3 URL.
    3. Create/Update Layer: In the AWS Console, create a new Lambda Layer or create a new version of an existing one.
    4. Load from S3: Use the S3 file URL to load the content into the AWS Lambda Layer.
    5. Attach to Function: Add the Layer ARN to your serverless function configuration.
  3. Configure custom fonts for AWS Lambda

    master

    The AWS Lambda runtime does not include font faces. While @sparticuz/chromium includes Open Sans (supporting Latin, Greek, and Cyrillic), you can add custom fonts by uploading them as an AWS Lambda Layer.

    1. Create a directory named .fonts or fonts.
    2. Place your .ttf files inside.
    3. Zip the directory.
    4. Upload as a layer.

    Supported font directories include:

    • /var/task/.fonts
    • /var/task/fonts
    • /opt/fonts
    • /tmp/fonts
    zip -9 --filesync --move --recurse-paths fonts.zip fonts/
  4. Automate Chromium Lambda layer deployment via CLI

    master

    You can automate the process of downloading the Chromium layer, uploading it to S3, and publishing it as a new Lambda Layer version using the AWS CLI.

    Note: Ensure you set the correct chromiumVersion, bucketName, and compatible runtimes/architectures for your environment.

    $ chromiumVersion="112.0.0"
    $ bucketName="chromiumUploadBucket"
    $ wget "https://github.com/Sparticuz/chromium/releases/download/v${chromiumVersion}/chromium-v${chromiumVersion}-layer.zip"
    $ aws s3 cp "chromium-v${chromiumVersion}-layer.zip" "s3://${bucketName}/chromiumLayers/chromium-v${chromiumVersion}-layer.zip"
    $ aws lambda publish-layer-version --layer-name chromium --description "Chromium v${chromiumVersion}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-v${chromiumVersion}-layer.zip" --compatible-runtimes nodejs --compatible-architectures x86_64
  5. Monitor the EC2 Chromium build process

    master

    The Chromium build process runs on EC2 instances via a user-data script. You can monitor progress either by checking a JSON file in S3 or by SSHing into the instance to view a live screen session.

    Method 1: Progress via S3 (Non-interactive)

    Use the AWS CLI to fetch the build.json file from the S3 bucket to see the current status.

    Method 2: Live Monitoring via SSH

    If the build:ssh label was applied to the PR, you can SSH into the instance and attach to the active screen session named build to watch the build logs in real-time.

  6. Configure IAM Instance Profile: `chromium-build`

    master

    The EC2 build instance itself requires an IAM role (via an instance profile) to upload build artifacts and logs to S3. This role does not need EC2 or SSM permissions, only S3 access.

    1. Create the Role Policy: Use a policy that allows s3:PutObject, s3:GetObject, and s3:DeleteObject on your build bucket.
    2. Attach Trust Policy: Ensure the role has a trust policy allowing ec2.amazonaws.com to assume the role.
    3. Create and Associate Instance Profile: Use the AWS CLI to create the profile and link it to the role.

    Required Permissions on EC2:

    • s3:PutObject: Upload artifacts, build logs, and build.json.
    • s3:DeleteObject: Remove pending.json during teardown or failure.
    • s3:GetObject: Read/modify build.json to report progress.
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "S3ArtifactUpload",
          "Effect": "Allow",
          "Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
          "Resource": ["arn:aws:s3:::BUCKET_NAME", "arn:aws:s3:::BUCKET_NAME/*"]
        }
      ]
    }
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": { "Service": "ec2.amazonaws.com" },
          "Action": "sts:AssumeRole"
        }
      ]
    }
    aws iam create-instance-profile --instance-profile-name chromium-build
    aws iam add-role-to-instance-profile \
      --instance-profile-name chromium-build --role-name chromium-build
  7. Use the @sparticuz/chromium-min package

    master

    The -min package does not include the Chromium Brotli files. This is useful when your host has file size limits. When using it, you must provide the location of the Brotli files to chromium.executablePath().

    Option 1: Local Path Provide a path to a directory containing the Brotli files (e.g., /opt/chromium).

    Option 2: Remote URL Provide a URL to a .tar file containing the Brotli files. On the first run, the package will download the pack, untar it to /tmp/chromium-pack, and decompress the binary to /tmp/chromium. Subsequent warm starts will use the existing files in /tmp.

    // Example using a local path
    const browser = await puppeteer.launch({
      args: await puppeteer.defaultArgs({ args: chromium.args, headless: "shell" }),
      defaultViewport: viewport,
      executablePath: await chromium.executablePath("/opt/chromium"),
      headless: "shell",
    });
    
    // Example using a remote URL
    const browser = await puppeteer.launch({
      args: await puppeteer.defaultArgs({ args: chromium.args, headless: "shell" }),
      defaultViewport: viewport,
      executablePath: await chromium.executablePath(
        "https://www.example.com/chromiumPack.tar",
      ),
      headless: "shell",
    });
  8. Use Chromium on ARM64 (Graviton) instances

    master

    The standard @sparticuz/chromium npm package only includes x64 binaries. To use ARM64 (Graviton) architectures, you must use the @sparticuz/chromium-min package and provide the binary via one of two methods:

    1. Lambda Layer: Download the chromium-VERSION-layer.arm64.zip from the GitHub releases and upload it as an AWS Lambda layer.
    2. Remote Pack: Host the chromium-VERSION-pack.arm64.tar file at an HTTPS URL and pass that URL to chromium.executablePath().

    Note: For ARM64, the npm package itself should be treated as a dev dependency if you are using a layer.

  9. Configure IAM User for GitHub Actions Runner

    master

    To allow the GitHub Actions runner to manage the EC2 build infrastructure (launching instances, managing security groups, and interacting with S3), create an IAM user (or use OIDC federation) with the following policy. Replace BUCKET_NAME with your actual S3 bucket name.

    Required Permissions Summary:

    • EC2 Management: Describe VPCs/Subnets/Security Groups, Create Security Groups, Run/Terminate Instances, and Create Tags.
    • SSM: Fetch the latest Amazon Linux 2023 AMI ID.
    • IAM: iam:PassRole to attach the chromium-build instance profile to the EC2 instance.
    • S3: Full access (PutObject, GetObject, DeleteObject, ListBucket) to the specified build bucket.
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "EC2BuildManagement",
          "Effect": "Allow",
          "Action": [
            "ec2:DescribeVpcs",
            "ec2:DescribeSubnets",
            "ec2:DescribeSecurityGroups",
            "ec2:CreateSecurityGroup",
            "ec2:AuthorizeSecurityGroupIngress",
            "ec2:DescribeInstanceTypeOfferings",
            "ec2:RunInstances",
            "ec2:DescribeInstances",
            "ec2:TerminateInstances",
            "ec2:CreateTags"
          ],
          "Resource": "*"
        },
        {
          "Sid": "SSMReadAMI",
          "Effect": "Allow",
          "Action": "ssm:GetParameters",
          "Resource": "arn:aws:ssm:*:*:parameter/aws/service/ami-amazon-linux-latest/*"
        },
        {
          "Sid": "IAMPassRole",
          "Effect": "Allow",
          "Action": "iam:PassRole",
          "Resource": "arn:aws:iam::*:role/chromium-build"
        },
        {
          "Sid": "S3ArtifactAccess",
          "Effect": "Allow",
          "Action": [
            "s3:PutObject",
            "s3:GetObject",
            "s3:DeleteObject",
            "s3:ListBucket"
          ],
          "Resource": ["arn:aws:s3:::BUCKET_NAME", "arn:aws:s3:::BUCKET_NAME/*"]
        }
      ]
    }
  10. Configure `NPM_PUBLISH_TOKEN`

    master

    The NPM_PUBLISH_TOKEN is used by the release.yml workflow to publish the following packages with provenance:

    • @sparticuz/chromium
    • @sparticuz/chromium-min

    Setup: Create a granular access token on npmjs.com with Read and write permissions specifically for the @sparticuz scope.

  11. Create an AWS Lambda Layer for @sparticuz/chromium

    master

    You can create a layer artifact locally using the provided Makefile. Replace archType with x64 or arm64 as needed.

    Local Build:

    archType="x64" && \
    git clone --depth=1 https://github.com/sparticuz/chromium.git && \
    cd chromium && \
    make chromium.${archType}$.zip

    Deploy to S3 and Publish via AWS CLI:

    bucketName="chromiumUploadBucket" && archType="x64" && versionNumber="v135.0.0" && \
    aws s3 cp chromium.${archType}.zip "s3://${bucketName}/chromiumLayers/chromium-${versionNumber}-layer.${archType}.zip" && \
    aws lambda publish-layer-version --layer-name chromium --description "Chromium v${versionNumber} for ${archType}" --content "S3Bucket=${bucketName},S3Key=chromiumLayers/chromium-${versionNumber}-layer.${archType}.zip" --compatible-runtimes "nodejs20.x" "nodejs22.x" --compatible-architectures $(if [ "$archType" = "x64" ]; then echo "x86_64"; else echo "$archType"; fi)
  12. Install @sparticuz/chromium

    master

    To use @sparticuz/chromium, you must also install puppeteer-core or playwright-core. The version of @sparticuz/chromium you install must match the Chromium version supported by your chosen Puppeteer/Playwright version. Check the Puppeteer Chromium Support page to find the correct version.

    Important Dependency Rules:

    • puppeteer-core or playwright-core is always a production dependency.
    • If you are using an AWS Lambda Layer, @sparticuz/chromium can be a dev dependency.
    • If you are not using a layer, @sparticuz/chromium must be a production dependency.

    If your deployment environment has strict file size limits, use the @sparticuz/chromium-min package instead, which requires you to host the Brotli files separately.

    # Puppeteer or Playwright is a production dependency
    npm install --save puppeteer-core@$PUPPETEER_VERSION
    
    # @sparticuz/chromium can be a DEV dependency IF YOU ARE USING A LAYER.
    # If you are not using a layer, use it as a production dependency!
    npm install --save-dev @sparticuz/chromium@$CHROMIUM_VERSION