Reputation: 185
To enable the webdriver in my google cloud function, I created a custom container using a docker file:
FROM python:3.7
COPY . /
WORKDIR /
RUN pip3 install -r requirements.txt
RUN apt-get update
RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils
#download and install chrome
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
#install python dependencies
COPY requirements.txt requirements.txt
RUN pip install -r ./requirements.txt
# Downloading gcloud package
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
# Installing the package
RUN mkdir -p /usr/local/gcloud \
&& tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz \
&& /usr/local/gcloud/google-cloud-sdk/install.sh
# Adding the package path to local
ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin
#some envs
ENV PORT 5000
#copy local files
COPY . .
CMD exec gunicorn --bind :${PORT} --workers 1 --threads 8 main:app
ENTRYPOINT ["webcrawler"]
I installed gcloud in this docker so that I will be able to use gcloud deploy
to deploy my cloud functions. Then, I deploy my script using this cloudbuild.yaml:
steps:
- name: 'us-central1-docker.pkg.dev/$PROJECT_ID/webcrawler-repo/webcrawler:tag1'
entrypoint: 'gcloud'
args: ['functions', 'deploy', 'MY_FUN', '--trigger-topic=MY_TOPIC', '--runtime=python37', '--entry-point=main', '--region=us-central1', '--memory=512MB', '--timeout=540s']
id: 'deploying MY_FUN'
dir: 'MY_DIR'
However, I end up getting this error for my deployment:
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: invalid storage source object "MY_FUN-ba7acf95-4297-46b3-b76e-1c25ba21ba03/version-14/function-source.zip" in bucket "gcf-sources-967732204245-us-central1": failed to get storage object: Get "https://storage.googleapis.com/storage/v1/b/gcf-sources-967732204245-us-central1/o/MY_FUN-ba7acf95-4297-46b3-b76e-1c25ba21ba03%2Fversion-14%2Ffunction-source.zip?alt=json&prettyPrint=false": RPC::UNREACHABLE: gslb: no reachable backends
ERROR
ERROR: build step 0 "us-central1-docker.pkg.dev/PROJECT_ID/webcrawler-repo/webcrawler:tag1" failed: step exited with non-zero status: 1
Any idea how to resolve this issue?
Thanks!
Upvotes: 11
Views: 16837
Reputation: 75745
Cloud functions allows you to deploy only your code. The packaging into a container, with buildpack, is performed automatically for you.
If you have already a container, the best solution is to deploy it on Cloud Run. If your webserver listen on the port 5000, don't forget to override this value during the deployment (use --port
parameter).
To plug your PubSub topic to your Cloud Run service, you have 2 solutions
In both cases, you need to take care of the security by using a service account with the role run.invoker on the Cloud Run service that you pass to PubSub push subscription or to EventArc
Upvotes: 21