Run Selenium in AWS Lambda for UI testing

[LAST UPDATED: 29-July-2022]

Let me begin by expressing my frustration 😡😡😡 with the fact that AWS doesn't have a pre-configured selenium image for Lambda on their public ECR marketplace. Selenium is the go-to tool for UI testing and for building many kinds of bots but running it on Lambda is complicated.

The easiest method is to use SAM CLI for Docker for Lambda to create an image with Selenium, Chrome / Chromium headless and webdriver, but given the way Lambda restricts the environment making it work on Selenium is quite difficult but not impossible.

In this tutorial I will provide a guide on how to do exactly that.


Follow these instructions to setup your development environment.

It will guide you to install and configure AWS CLI & SAM CLI.

Create the app

Follow the instructions in this guide to create Lambda with Docker.

Your folder structure should look like below.

├── events
│   └── event.json
├── hello_world
│   ├── Dockerfile
│   ├──
│   ├──
│   └── requirements.txt
├── template.yaml
└── tests
    └── unit

Customising the app

First, change the name of hello-world directory to src.

Both the files should be empty

Events: events/event.json

Leave the contents of the event.json file unchanged.

Application: src/

We write a simple Python program that uses selenium webdriver to scape a website.

Change the contents of the file to below.

## Run selenium and chrome driver to scrape data from
import time
import json
import os.path
from selenium import webdriver
from import Service
from import By
from import Options

def handler(event=None, context=None):
    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = "/opt/chrome/chrome"
    chrome = webdriver.Chrome("/opt/chromedriver", options=chrome_options)
    description = chrome.find_element(By.NAME, "description").get_attribute("content")
    return {
        "statusCode": 200,
        "body": json.dumps(
                "message": description,

Python Dependencies: src/requirements.txt

Capture the app dependencies in requirements.txt


Chrome dependencies: src/chrome-deps.txt

Create a file named src/chrome-deps.txt with the following contents

acl adwaita-cursor-theme adwaita-icon-theme alsa-lib at-spi2-atk at-spi2-core 
atk avahi-libs cairo cairo-gobject colord-libs cryptsetup-libs cups-libs dbus 
dbus-libs dconf desktop-file-utils device-mapper device-mapper-libs elfutils-default-yama-scope
elfutils-libs emacs-filesystem fribidi gdk-pixbuf2 glib-networking gnutls graphite2 
gsettings-desktop-schemas gtk-update-icon-cache gtk3 harfbuzz hicolor-icon-theme hwdata jasper-libs
jbigkit-libs json-glib kmod kmod-libs lcms2 libX11 libX11-common libXau libXcomposite libXcursor 
libXdamage libXext libXfixes libXft libXi libXinerama libXrandr libXrender libXtst libXxf86vm libdrm 
libepoxy liberation-fonts liberation-fonts-common liberation-mono-fonts liberation-narrow-fonts 
liberation-sans-fonts liberation-serif-fonts libfdisk libglvnd libglvnd-egl libglvnd-glx libgusb 
libidn libjpeg-turbo libmodman libpciaccess libproxy libsemanage libsmartcols libsoup libthai libtiff 
libusbx libutempter libwayland-client libwayland-cursor libwayland-egl libwayland-server libxcb 
libxkbcommon libxshmfence lz4 mesa-libEGL mesa-libGL mesa-libgbm mesa-libglapi nettle pango pixman 
qrencode-libs rest shadow-utils systemd systemd-libs trousers ustr util-linux vulkan 
vulkan-filesystem wget which xdg-utils xkeyboard-config

Dockerfile: src/Dockerfile

Change the contents of the file to below.

FROM as stage

# Hack to install chromium dependencies
RUN yum install -y -q sudo unzip

# Find the version of latest stable build of chromium from below
# Then follow the instructions here in below URL 
# to download old builds of Chrome/Chromium that are stable
# Current stable version of Chromium

# Install Chromium
COPY /tmp/
RUN /usr/bin/bash /tmp/

FROM as base

COPY chrome-deps.txt /tmp/
RUN yum install -y $(cat /tmp/chrome-deps.txt)

# Install Python dependencies for function
COPY requirements.txt /tmp/
RUN python3 -m pip install --upgrade pip -q
RUN python3 -m pip install -r /tmp/requirements.txt -q 

COPY --from=stage /opt/chrome /opt/chrome
COPY --from=stage /opt/chromedriver /opt/chromedriver

CMD [ "app.handler" ]

Script to install browser: src/

Create a file at src/ We will use a simple shell script to install the latest Chrome and Chrome webdriver.


echo "Downloading Chromium..."
curl "\
Linux_x64%2F$" > /tmp/

unzip /tmp/ -d /tmp/
mv /tmp/chrome-linux/ /opt/chrome

curl "\
Linux_x64%2F$" > /tmp/

unzip /tmp/ -d /tmp/
mv /tmp/chromedriver_linux64/chromedriver /opt/chromedriver

Then run the below command to make the script executable.

chmod +x src/


Change the contents to below. Based on the complexity of your app, you may need to increase the memory and timeout values under Globals:Function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >

  Sample SAM Template for selenium

# More info about Globals:
    Timeout: 120
    MemorySize: 2048

    Type: AWS::Serverless::Function 
      PackageType: Image
        - x86_64
          Type: Api 
            Path: /selenium
            Method: get
      Dockerfile: Dockerfile
      DockerContext: ./src
      DockerTag: python3.9-v1

  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
    Description: "API Gateway endpoint URL for Prod stage for Seleniumc function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}"
    Description: "Selenium Lambda Function ARN"
    Value: !GetAtt SeleniumFunction.Arn
    Description: "Implicit IAM Role created for Selenium function"
    Value: !GetAtt SeleniumFunctionRole.Arn

Build & test the app

To build the app run,

sam build

To test run

sam local invoke


You should see something similar to below depending on the URL you scraped

sam local invoke success

Deploy the app

To deploy the app for the first time run,

sam deploy --guided

This will start the interactive deployment to Lambda. You can use options as shown below.

sam deploy guided

This will also create a samconfig.toml file that will contain these configurations.

Next time after you build the app, just run sam deploy to deploy the app.

After a successful deployment, you should see something similar to below. Note the API URL in the output at the bottom.


Test the app

Using the API URL from the output, you can test the app by running

curl -X GET <API URL>


To delete the app, run sam delete.

Using the GitHub repository directly

You need AWS SAM CLI installed and AWS credentials configured.

Open your terminal and run the following command to clone the repository.

git clone

Navigate to the app directory.

cd selenium-in-aws-lambda/selenium

Build the app.

sam build

Test the app locally.

sam local invoke

Deploy the app to AWS.

sam deploy --guided
Need Help? Open a discussion thread on GitHub.

Related Posts