Browse Source

feat: add Dockerfile to create image for Lambda

Olivier Hoareau 4 years ago
parent
commit
82294983b0
4 changed files with 82 additions and 1 deletions
  1. 2 1
      .gitignore
  2. 35 0
      Dockerfile.lambda
  3. 30 0
      Makefile
  4. 15 0
      bin/lambda.js

+ 2 - 1
.gitignore

@@ -7,4 +7,5 @@ results/*
 coverage
 front/build
 package-lock.json
-har.json
+har.json
+.idea/

+ 35 - 0
Dockerfile.lambda

@@ -0,0 +1,35 @@
+FROM public.ecr.aws/lambda/nodejs:12
+
+ENV DOCKERIZED yes
+
+RUN yum install -y \
+        git gcc make gcc-c++ zlib-devel libjpeg-turbo-devel nasm automake autoconf libtool \
+        ca-certificates freetype freetype-devel harfbuzz nss \
+        cups-libs dbus-glib libXrandr libXcursor libXinerama cairo cairo-gobject pango && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/a/atk-2.26.0-1.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/a/at-spi2-atk-2.26.0-1.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/a/at-spi2-core-2.26.0-1.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libxkbcommon-0.7.1-5.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libXcomposite-0.4.4-11.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/a/alsa-lib-1.1.4.1-3.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libXi-1.7.9-4.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libXtst-1.2.3-4.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libxkbcommon-x11-0.7.1-5.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/g/gtk3-3.22.24-1.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libepoxy-1.4.3-3.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libwayland-cursor-1.14.0-1.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/m/mesa-libwayland-egl-17.2.2-4.fc27.x86_64.rpm && \
+    rpm -ivh --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/g/gdk-pixbuf2-2.36.11-1.fc27.x86_64.rpm && \
+    rpm -ivh --force --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/l/libpng-1.6.31-1.fc27.x86_64.rpm && \
+    rpm -ivh --force --nodeps http://dl.fedoraproject.org/pub/archive/fedora/linux/releases/27/Everything/x86_64/os/Packages/z/zlib-1.2.11-4.fc27.x86_64.rpm && \
+    yum clean all && \
+    rm -rf /var/cache/yum
+
+ADD . /var/task
+
+RUN rm -rf node_modules && \
+    npm install jpegoptim-bin --unsafe-perm=true --allow-root && \
+    NODE_ENV=development && npm install --only=prod && \
+    sed -i 's@"--disable-dev-shm-usage"@"--disable-dev-shm-usage", "--no-sandbox", "--no-zygote", "--disable-gpu", "--user-data-dir=/tmp/user-data", "--data-path=/tmp/data-path", "--homedir=/tmp", "--disk-cache-dir=/tmp/cache-dir", "--single-process"@' node_modules/phantomas/lib/browser.js
+
+CMD [ "bin/lambda.runner" ]

+ 30 - 0
Makefile

@@ -0,0 +1,30 @@
+env ?= dev
+AWS_PROFILE ?= yellowlabtools-$(env)
+AWS_REGION ?= us-east-1
+DOCKER_IMAGE_TAG ?= latest
+RESULT_BUCKET_NAME ?= $(env)-yellowlabtools-storage
+TEST_NAME ?= test-local
+TEST_URL ?= https://www.google.fr
+
+all: install
+
+install:
+	@npm install
+
+build-docker-image-lambda:
+	@docker build -f Dockerfile.lambda -t yellowlabtools-runner-lambda .
+
+tag-docker-image-lambda:
+	@docker tag yellowlabtools-runner-lambda:$(DOCKER_IMAGE_TAG) $(DOCKER_REGISTRY)/yellowlabtools-runner-lambda:$(DOCKER_IMAGE_TAG)
+
+push-docker-image-lambda:
+	@docker push $(DOCKER_REGISTRY)/yellowlabtools-runner-lambda:$(DOCKER_IMAGE_TAG)
+
+login-docker:
+	@AWS_PROFILE=$(AWS_PROFILE) aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(DOCKER_REGISTRY)
+
+start-local-lambda:
+	@docker run --rm -it -p 9000:8080 -e RESULT_BUCKET_NAME=$(RESULT_BUCKET_NAME) -e AWS_ACCESS_KEY_ID=$(AWS_ACCESS_KEY_ID) -e AWS_SECRET_ACCESS_KEY=$(AWS_SECRET_ACCESS_KEY) yellowlabtools-runner-lambda:$(DOCKER_IMAGE_TAG)
+
+run-local-test:
+	@curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"id": "$(TEST_NAME)", "url": "$(TEST_URL)"}'

+ 15 - 0
bin/lambda.js

@@ -0,0 +1,15 @@
+const s3 = new require('aws-sdk').S3();
+const ylt = require('..');
+
+// noinspection JSUnusedLocalSymbols
+async function runner({id, url, options = {}}, context) {
+    console.log(`Processing run #${id} on ${url}`);
+    const bucket = process.env.RESULT_BUCKET_NAME;
+    const keyPrefix = `results/${id}`;
+    const saveFile = async (path, content) => s3.putObject({Bucket: bucket, Key: `${keyPrefix}/${path}`, Body: content})
+        .promise();
+    await saveFile('results.json', JSON.stringify(await ylt(url, {...options, saveFile})));
+    return {status: 'processed', id, bucket, keyPrefix};
+}
+
+module.exports = {runner}