Skip to content

Commit

Permalink
Fix dockerfile and add image to serverless config (#3)
Browse files Browse the repository at this point in the history
* Attempt fix dockerfile and add image to serverless config

* Run only using one image

* WIP

* remove unneeded code

* WIP

* WIP

* Get Dockerfile to work and run locally

* Get chrome to work within Docker

* Update GitHub Action

* Add finishing touches
  • Loading branch information
aurora-dot authored May 13, 2024
1 parent 6d648a3 commit 50cd6af
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 1,455 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHROME_PATH=/usr/bin/google-chrome-stable
19 changes: 3 additions & 16 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache: true
- uses: Jerome1337/[email protected]

# build:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3

# - name: Set up Go
# uses: actions/setup-go@v5
# with:
# go-version: ${{ env.GO_VERSION }}

# - name: Build
# run: go build -v ./...

# - name: Test
# run: go test -v ./...
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ node_modules

# Ignore build directory
bin

.aws-lambda-rie
.env
76 changes: 57 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,64 @@
FROM public.ecr.aws/lambda/go:1 as lambda
FROM ubuntu:20.04

# Copy over go source code
COPY scraper/main.go /src/main.go
COPY go.mod /src/go.mod
COPY go.sum /src/go.sum
USER root

ARG DEBIAN_FRONTEND=noninteractive
ARG TZ=Europe/London

ENV TZ=$TZ
ENV DEBIAN_FRONTEND=$DEBIAN_FRONTEND
ENV LANG="C.UTF-8"
ENV DEBUG_COLORS=true
ENV CHROME_PATH=/src/chrome/chrome

RUN apt-get update && apt-get install -y \
ca-certificates \
curl \
dumb-init \
git \
gnupg \
libu2f-udev \
software-properties-common \
ssh \
wget \
xvfb

RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 libnss3-dev lsb-release xdg-utils libgbm-dev

RUN apt install golang-go -y

WORKDIR /src

# Install packages
RUN yum install go xz atk cups-libs gtk3 libXcomposite alsa-lib tar \
libXcursor libXdamage libXext libXi libXrandr libXScrnSaver \
libXtst pango at-spi2-atk libXt xorg-x11-server-Xvfb \
xorg-x11-xauth dbus-glib dbus-glib-devel unzip bzip2 -y -q
COPY scraper/main.go go.mod go.sum ./

RUN GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o app main.go && \
rm main.go go.mod go.sum

RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections && \
apt-get -y -qq install software-properties-common &&\
apt-add-repository "deb http://archive.canonical.com/ubuntu $(lsb_release -sc) partner" && \
apt-get -y -qq --no-install-recommends install \
fontconfig \
fonts-freefont-ttf \
fonts-gfs-neohellenic \
fonts-indic \
fonts-ipafont-gothic \
fonts-kacst \
fonts-liberation \
fonts-noto-cjk \
fonts-noto-color-emoji \
fonts-roboto \
fonts-thai-tlwg \
fonts-ubuntu \
fonts-wqy-zenhei

RUN apt install curl unzip -y

# Build go lambda function
RUN GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o ${LAMBDA_TASK_ROOT} /src/main.go
RUN mkdir -p "/src/chrome/" \
&& curl -Lo "/src/chrome/chrome-linux.zip" "https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Linux_x64%2F1299153%2Fchrome-linux.zip?generation=1715336417866122&alt=media" \
&& unzip -q "/src/chrome/chrome-linux.zip" -d "/src/chrome/" && mv /src/chrome/chrome-linux/* /src/chrome/ \
&& rm -rf /src/chrome/chrome-linux "/src/chrome/chrome-linux.zip"

# Install chrome
RUN mkdir -p /opt/chrome/
RUN curl -Lo "/opt/chrome/chrome-linux.zip" "https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Linux_x64%2F1299153%2Fchrome-linux.zip?generation=1715336417866122&alt=media"
RUN unzip -q "/opt/chrome/chrome-linux.zip" -d "/opt/chrome/"
RUN mv /opt/chrome/chrome-linux/* /opt/chrome/
RUN rm -rf /opt/chrome/chrome-linux "/opt/chrome/chrome-linux.zip"
RUN echo CHROME_PATH=${CHROME_PATH} > .env

CMD [ "app" ]
ENTRYPOINT [ "src/app" ]
33 changes: 20 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
.PHONY: install build debugBuild clean deploy
.PHONY: clean install build buildDocker runDebug runDebugDocker getDebugTools deploy

clean:
rm -rf ./node_modules

install:
npm i

getDebugTools:
wget https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie
mkdir -p .aws-lambda-rie
mv aws-lambda-rie .aws-lambda-rie/aws-lambda-rie
chmod +x .aws-lambda-rie/aws-lambda-rie

build:
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/main.go
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/world world/main.go
env GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/scraper scraper/main.go
GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/scraper scraper/main.go

debugBuild:
env GOARCH=amd64 GOOS=linux go build -v -gcflags='all=-N -l' -ldflags="-s -w" -o bin/hello hello/main.go
env GOARCH=amd64 GOOS=linux go build -v -gcflags='all=-N -l' -ldflags="-s -w" -o bin/world world/main.go
debug:
env GOARCH=amd64 GOOS=linux go build -v -gcflags='all=-N -l' -ldflags="-s -w" -o bin/scraper scraper/main.go

debugDeploy: clean install debugBuild
env SLS_DEBUG=* npx sls offline --useDocker
runDebug: debug
~/.aws-lambda-rie/aws-lambda-rie ./bin/scraper

clean:
rm -rf ./node_modules
rm -rf ./bin
buildDocker:
docker build -t watcher-local-build .

runDebugDocker: buildDocker
docker run --platform linux/amd64 -v ./.aws-lambda-rie:/aws-lambda -p 9000:8080 --entrypoint /aws-lambda/aws-lambda-rie watcher-local-build /src/app

deploy: clean install build
deploy: install
npx sls deploy --verbose
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ This is a serverless service to be ran on an aws lambda to provide functions to

# Development

- To run locally run: `make debugDeploy`
- First run `make getDebugTools`
- To build and run locally:
- Set `CHROME_PATH` in .env to chrome instillation
- Run: `make runDebug` in one terminal
- In another terminal run `curl -XPOST "http://localhost:8080/2015-03-31/functions/function/invocations" -d '{"Name": "World"}'`
- Wait for the response!
- To run docker image locally, first run:
- Build the docker image: `docker run -p 9000:8080 watcher-local-build:latest`
- Run the image: `make runDebugDocker`
- In another terminal, run `url -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'`
- Wait for the response!
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
require github.com/aws/aws-lambda-go v1.47.0

require (
github.com/joho/godotenv v1.5.1 // indirect
github.com/ysmood/fetchup v0.2.3 // indirect
github.com/ysmood/goob v0.4.0 // indirect
github.com/ysmood/got v0.34.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-rod/rod v0.116.0 h1:ypRryjTys3EnqHskJ/TdgodFMvXV0EHvmy4bSkKZgHM=
github.com/go-rod/rod v0.116.0/go.mod h1:aiedSEFg5DwG/fnNbUOTPMTTWX3MRj6vIs/a684Mthw=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
Expand Down
45 changes: 0 additions & 45 deletions hello/main.go

This file was deleted.

Loading

0 comments on commit 50cd6af

Please sign in to comment.