How can I lower my billing in gcp app engine deployment? - google-app-engine

I have hosted multiple applications in GCP App Engine. We are currently in development and testing environment, and the user requests are almost nil. In my understanding, the billing should have been low when the traffic is low. But the billing of past two months is way more than what we had initially expected. Our target is to host over hundred application in near future, but if the current billing trend continues, the potential situation after we scale is scary.
Until October we had hosted 5 applications and the billing was around 250 USD per month, but since November we've added two more applications-practically of same size and requirements as that of our previous five applications, and the billing has crossed 700 USD per month.
Is there any possibility that we could have been doing something wrong? or is it better if we shift to Kubernetes or VM instance?

App Engine is billed per instance/hour, compared to the market its prices are more than fair but you have to consider the whole picture while forecasting costs of your applications including, price of other services, traffic, etc.
First off I suggest you review the pricing of App Engine, which instance types are you using ? Can you use a more cheap instance type ?
Check how many instances your application spawn, you can do it in GPC App Engine info page or with Stackdriver Monitoring. Is the behaviour the one you expect ? Are you spawning too many instances at one point because of cron jobs, etc ? Will it be possible for your application to limit the max number or instances at a time in order to contain costs?
If you are using also other services, carefully review costs for each project inside the specific page, what is costing more than expecting ?
Review your total costs using the gcp pricing calculator, understand what you didn't expect and adjust your application to cover spikes in costs.

The bill suggests something is not truly idle.
Could you provide more details like a screenshot of the App Engine->Instances summary page? Or the billing summary? ( I can't add comments yet, hence my answer is a question for now .. )

#standard app.yaml
# service name or project name
service: default
​
# python runtime version
runtime: python37
​
entrypoint: #django
​
# type of app engine standard or flex
env: standard
​
# environment varible required for the project
env_variables:
​
# GCP cloud database envs
# Bucket storage envs
​
handlers:
- url: /static
static_dir: static
#flex app.yaml
service: #service name
​
runtime: custom
​
entrypoint: #django
env: flex
​
env_variables:
​
#GCP cloud sql
# bucket link
​
handlers:
- url: /static
static_dir: static/
​
runtime_config:
python_version: 3.6 # enter your Python version BASE ONLY here. Enter 2 for 2.7.9 or 3 for 3.6.4
#flex docker file
FROM gcr.io/google-appengine/python
LABEL python_version=python3.6
RUN virtualenv --no-download /env -p python3.6
# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-add-repository ppa:ricotz/testing
RUN apt-get update
RUN apt-get install -y libcairo2-dev
RUN apt-get install -y build-essential python3-dev python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info
RUN apt install -y pkg-config
RUN pip install -r requirements.txt
ADD . /app/
CMD exec gunicorn -b :$PORT name.wsgi

Related

Possibly memory leak in Pyramid on Appengine Flexible with MemoryStore

We're working on migrating our backend from appengine Standard (and the "webapp2" framework) to Flexible using pyramid. We have a proof of concept of sorts running seemingly without many issues. All it does in this early phase is take requests from a third party ("pings") and then go kick off a task to another internal service to go fetch some data. It connects with googles MemoryStore to cache a user-id to indicate that we've already fetched that users data (or attempted to) within the last 6 hours.
Speaking of 6 hours, it seems that every 6 hours or so, memory usage on the Flexible instance seems to reach a tipping point, and then probably flushes, and all is fine again. This instance is set to have 512MB of memory, yet like clockwork it does this at around 800MB (some kind of grace-usage? or maybe these can't be set to under 1GB)
It's clear by how gradual it's moving that memory isn't being cleared as often as maybe it should be.
When this happens, latency on the instance also spikes.
I'm not sure what's useful in debugging something like this so I'll try to show what I can.
Appengine YAML file:
runtime: custom
env: flex
service: ping
runtime_config:
python_version: 3.7
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
Dockerfile (as custom-runtime Flexible needs it)
FROM gcr.io/google-appengine/python
RUN virtualenv /env
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
ADD . /app
RUN pip install -e .
CMD gunicorn -b :$PORT main:app
Why custom? I couldn't get this working under the default Python runtime. The pip install -e . was what appeared to be needed.
Then, in root __init__ I have:
from pyramid.config import Configurator
from externalping.memcache import CacheStore
cachestore = CacheStore()
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
with Configurator(settings=settings) as config:
config.include('.routes')
config.scan()
return config.make_wsgi_app()
Maybe having the connection to MemoryStore defined so early is the issue? Cachestore:
class CacheStore(object):
redis_host = os.environ.get('REDISHOST', 'localhost')
redis_port = int(os.environ.get('REDISPORT', 6379))
client = None
def __init__(self):
self.client = redis.StrictRedis(host=self.redis_host, port=self.redis_port)
def set_json(self, key, value):
self.client.set(key, json.dumps(value))
return True
def get_json(self, key):
return json.loads(self.client.get(key))
On the actual request itself, after importing from externalping import cachestore, I'm simply calling those methods shown above: cachestore.client.get(user['ownerId'])
This does appear to be how google's documentation says to implement this, as best I can tell. Only difference really is I put a wrapper around it.

How to allow App Engine to authenticate and download private Go modules

My project uses Go modules hosted in private GitHub repositories.
Those are listed in my go.mod file, among the public ones.
On my local computer, I have no issue authenticating to the private repositories, by using the proper SSH key or API token in the project’s local git configuration file. The project compiles fine here.
Neither the git configuration nor the .netrc file are taken into account during the deployment (gcloud app deploy) and the build phase in the cloud, so my project compilation fails there with an authentication error for the private modules.
What is the best way to fix that? I would like to avoid a workaround which would consist in including the private modules’ source code in the deployed files, and have rather find a way to make the remote go or git use credentials I can provide.
You could try to deploy it directly from a build. According to the Accessing private GitHub repositories, you can set up git with key and domain on one of the build steps.
After that you can specify a step to run the gcloud app deploy command, as suggested in the Quickstart for automating App Engine deployments with Cloud Build.
An example of the cloudbuild.yaml necessary to do this would be:
# Decrypt the file containing the key
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args:
- kms
- decrypt
- --ciphertext-file=id_rsa.enc
- --plaintext-file=/root/.ssh/id_rsa
- --location=global
- --keyring=my-keyring
- --key=github-key
volumes:
- name: 'ssh'
path: /root/.ssh
# Set up git with key and domain.
- name: 'gcr.io/cloud-builders/git'
entrypoint: 'bash'
args:
- '-c'
- |
chmod 600 /root/.ssh/id_rsa
cat <<EOF >/root/.ssh/config
Hostname github.com
IdentityFile /root/.ssh/id_rsa
EOF
mv known_hosts /root/.ssh/known_hosts
volumes:
- name: 'ssh'
path: /root/.ssh
# Deploy app
- name: "gcr.io/cloud-builders/gcloud"
args: ["app", "deploy"]
timeout: "16000s"

How to have dynamic version name at run time when deploying google app engine in Travis CI?

I am studying to automate the build and deployment of my google app engine application in Travis, so far it allows me to have static or predefined version name during deployment in .travis.yml.
Is there any way to make it dynamically generated at runtime? Like for example below in my .travis.yml file, I have deployment for production and staging version of the application, both are named or labeled as production and qa-staging, and I would like to suffix the version names with a timestamp or anything as long as it would be unique every successful build and deployment.
language: node_js
node_js:
- "10"
before_install:
- openssl aes-256-cbc -K $encrypted_c423808ed406_key -iv $encrypted_c423808ed406_iv
-in gae-creds.json.enc -out gae-creds.json -d
- chmod +x test.sh
- cat gae-creds.json
install:
- npm install
script:
- "./test.sh"
deploy:
- provider: gae
skip_cleanup: true
keyfile: gae-creds.json
project: traviscicd
no_promote: true
version: qa-staging
on:
branch: staging
- provider: gae
skip_cleanup: true
keyfile: gae-creds.json
project: traviscicd
version: production
on:
branch: master
Have you tried with https://yaml.org/type/timestamp.html ?
Im not sure if the context is the correct but seems a good and elegant option for your yaml file.
Perhaps you can use go generate to generate a version string that can be included? You need to run go generate as part of the build process for it to work, though.

Problem with nltk during app deployment on google cloud

I tried to deploy my application on gcloud app engine, when the deployment finish and I tried to brows the URL, I got 502 server error. The log shows that there is problem with nltk package:
[31m>>> import nltk
>>> nltk.download('punkt')
[0m
Searched in:
- '/root/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
- '/env/nltk_data'
- '/env/lib/nltk_data'
- ''
I have put the necessary hardware requirement on my app.yaml file :
service: vapi
runtime: python
env: flex
health_check:
enable_health_check: True
check_interval_sec: 5
timeout_sec: 4
unhealthy_threshold: 2
healthy_threshold: 2
entrypoint: gunicorn -b :$PORT wsgi:app
runtime_config:
python_version: 3.5
resources:
cpu: 2
memory_gb: 8
disk_size_gb: 20
I have tried to install the nltk packages into one of the search path shown in the log above.
also, I have created app engine configuration file:
# appengine_config.py
from google.appengine.ext import vendor
# Add any libraries install in the "lib" folder.
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
any suggestions?
You're mixing up the documentation for the standard environment with the one for the flexible environment.
Installing dependencies into the lib directory and using a appengine_config.py file is specific for the 1st generation standard environment.
For the flexible environment you specify your python dependencies using the requirements.txt file, see Using Python Libraries:
The Python runtime will automatically install all dependencies
declared in your requirements.txt during deployment.
For non-python dependencies or those which aren't pip-installable you can use a custom runtime, see Up-to-date pip with AppEngine Python flex env?
Maybe of interest: How to tell if a Google App Engine documentation page applies to the 1st/2nd generation standard or the flexible environment

Time out error when trying to create Google managed vm

I'm trying to create a managed vm for my node 4 application using google custom runtime.
I created the following Dockerfile:
FROM node:4.2.1
ENV PORT 8080
ADD package.json package.json
RUN npm install
ADD . .
CMD [ "npm", "start" ]
Along with this app.yaml:
# [START runtime]
runtime: custom
vm: true
api_version: 1
# [END runtime]
health_check:
enable_health_check: false
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- ^(.*/)?.*/node_modules/.*$
- ^(.*/)?.*\.log$
I deploy the app using gcloud preview command:
gcloud preview app deploy app.yaml --promote
It seems like the docker is being built correctly but the at the end of the process I get this message:
Copying files to Google Cloud Storage...
Synchronizing files to [gs://staging.my-project-id.appspot.com/].
Updating module [default]...\Deleted [https://www.googleapis.com/compute/v1/projects/my-project-id/zones/us-central1-f/instances/gae-builder-vm-20151030t142257].
Updating module [default]...failed.
ERROR: (gcloud.preview.app.deploy) Error Response: [4] Timed out creating VMs.
I have my deployment working now. I have had to troubleshoot the same problem before, for another project, but I didn't have the code on hand, so I had to work through the problems again.
The deployment ran smoothly up until the last steps, where updating the module would timeout. This made me think it was something to do with the application starting up on VM and not responding appropriately, so the final hook would time out.
You'll find a lot of information here - https://cloud.google.com/appengine/docs/managed-vms/config . I checked the following things:
logging - ensure that you are writing to the correct log file. See https://cloud.google.com/appengine/docs/managed-vms/custom-runtimes#logging
ensure you have a .dockerignore file and are skipping files in app.yaml so you are not asking the process to copy across unneeded node_modules or log files
turn off health checking if you are not using it, or ensure you have the correct express.js routes configured for it
check that your environment variables are set and match what GAE can use. This was my final step - GAE will let you bind to a VM port on 8080. I had to pass through a NODE_ENV flag in my app.yaml which told the app to use 8080 and not 3000.
Lift the resources of the GAE instance in app.yaml. I specified two logical CPUs and made the ram 2 gig.
Good luck.

Resources