Gcp
GcpIntermediate

Building and Deploying Applications on Google App Engine

4 min read
gcpcomputeapp-enginepaasserverless

TL;DR

Learn how to deploy and scale applications on Google App Engine. This guide covers standard and flexible environments, configuration options, and best practices for optimal performance.

Building and Deploying Applications on Google App Engine

Google App Engine (GAE) is a fully managed Platform-as-a-Service (PaaS) that makes deploying and scaling applications easy. This guide covers everything you need to know about using App Engine effectively.

$1

``mermaid

graph TB

subgraph AppEngine["App Engine Service"]

direction TB

subgraph Environments["Environments"]

direction LR

SE["Standard Environment"]

FE["Flexible Environment"]

end

subgraph Features["Platform Features"]

direction LR

AS["Auto Scaling"]

LB["Load Balancing"]

VS["Version Management"]

end

subgraph Services["Platform Services"]

direction LR

DS["Datastore"]

MM["Memcache"]

TQ["Task Queue"]

end

end

subgraph External["External Services"]

direction TB

SQL["Cloud SQL"]

CDN["Cloud CDN"]

SM["Secret Manager"]

end

AppEngine --> External

classDef primary fill:#4285f4,stroke:#666,stroke-width:2px,color:#fff

classDef secondary fill:#34a853,stroke:#666,stroke-width:2px,color:#fff

classDef tertiary fill:#fbbc05,stroke:#666,stroke-width:2px,color:#fff

class AppEngine,Environments primary

class Features,Services secondary

class External tertiary

`

$1

| Feature | Standard Environment | Flexible Environment |

|---------|---------------------|---------------------|

| Startup | Seconds | Minutes |

| SSH Access | No | Yes |

| Network Access | Via App Engine services | Full network access |

| Pricing | Per instance hour | Per vCPU, memory, disk |

| Scale to Zero | Yes | No |

| Custom Runtime | No | Yes (Docker) |

$1

$1

Example Python application:

`python

main.py

from flask import Flask

app = Flask(__name__)

@app.route('/')

def hello():

return 'Hello from App Engine!'

if __name__ == '__main__':

app.run(host='127.0.0.1', port=8080, debug=True)

`

Configuration file:

`yaml

app.yaml

runtime: python39

instance_class: F1

automatic_scaling:

target_cpu_utilization: 0.65

min_instances: 1

max_instances: 10

handlers:

  • url: /.*
  • script: auto

    `

    $1

    `yaml

    app.yaml

    runtime: custom

    env: flex

    manual_scaling:

    instances: 2

    resources:

    cpu: 1

    memory_gb: 2

    disk_size_gb: 10

    env_variables:

    ENV: 'production'

    `

    Dockerfile for custom runtime:

    `dockerfile

    FROM python:3.9-slim

    WORKDIR /app

    COPY requirements.txt .

    RUN pip install -r requirements.txt

    COPY . .

    CMD gunicorn -b :$PORT main:app

    `

    $1

    $1

    `bash

    Deploy application

    gcloud app deploy app.yaml

    Deploy specific version

    gcloud app deploy app.yaml --version=v1

    Deploy with traffic splitting

    gcloud app deploy app.yaml --version=v2 --no-promote

    `

    $1

    `bash

    List versions

    gcloud app versions list

    Split traffic

    gcloud app services set-traffic default \

    --splits=v1=0.5,v2=0.5

    Migrate traffic

    gcloud app services set-traffic default \

    --splits=v2=1 \

    --migrate

    `

    $1

    $1

    `yaml

    Automatic scaling

    automatic_scaling:

    target_cpu_utilization: 0.65

    target_throughput_utilization: 0.6

    min_instances: 1

    max_instances: 10

    max_concurrent_requests: 50

    Basic scaling

    basic_scaling:

    max_instances: 5

    idle_timeout: 10m

    Manual scaling

    manual_scaling:

    instances: 3

    `

    $1

    `yaml

    env_variables:

    DATABASE_URL: 'postgresql://user:pass@host:5432/db'

    API_KEY: '${SECRET_KEY}'

    CACHE_TTL: '3600'

    `

    $1

    $1

    `bash

    Map custom domain

    gcloud app domain-mappings create \

    --domain=www.example.com

    Update SSL certificate

    gcloud app ssl-certificates update \

    --display-name=main-cert \

    --domain=www.example.com

    `

    $1

    `yaml

    queue.yaml

    queue:

  • name: default
  • rate: 5/s

    bucket_size: 10

    retry_parameters:

    task_retry_limit: 3

    min_backoff_seconds: 30

    `

    $1

    $1

    `bash

    Set IAM policy

    gcloud app set-iam-policy policy.yaml

    Grant access

    gcloud projects add-iam-policy-binding PROJECT_ID \

    --member="user:jane@example.com" \

    --role="roles/appengine.deployer"

    `

    $1

    `bash

    Create firewall rule

    gcloud app firewall-rules create allow-internal \

    --action allow \

    --source-range 10.0.0.0/8 \

    --description "Allow internal traffic"

    `

    $1

    $1

    `bash

    View application logs

    gcloud app logs tail

    Read specific logs

    gcloud logging read "resource.type=gae_app" \

    --project=PROJECT_ID \

    --limit=10

    `

    $1

    Key metrics to monitor:

  • Instance count
  • Request latency
  • Error rates
  • Memory usage
  • CPU utilization
  • $1

    $1

    `python

    Using Memcache

    from google.appengine.api import memcache

    def get_data(key):

    data = memcache.get(key)

    if data is None:

    data = fetch_from_database(key)

    memcache.add(key, data, 3600) # Cache for 1 hour

    return data

    `

    $1

    `yaml

    app.yaml

    handlers:

  • url: /static
  • static_dir: static

    http_headers:

    Cache-Control: public, max-age=3600

  • url: /.*
  • script: auto

    `

    $1

    $1

    `bash

    Check application status

    gcloud app describe

    View instance details

    gcloud app instances list

    Debug routing issues

    gcloud app routes list

    `

    $1

    `yaml

    app.yaml

    health_check:

    enable_health_check: true

    check_interval_sec: 5

    timeout_sec: 4

    unhealthy_threshold: 2

    healthy_threshold: 2

    ``

    $1

    1. Instance Management

    - Use automatic scaling

    - Set appropriate instance classes

    - Implement efficient caching

    - Use CDN for static content

    2. Resource Utilization

    - Monitor and adjust instance count

    - Optimize database queries

    - Use asynchronous tasks

    - Implement proper caching

    $1

    Google App Engine provides a robust platform for deploying and scaling applications. Key takeaways:

  • Choose the right environment (Standard vs Flexible)
  • Implement proper scaling strategies
  • Use built-in services effectively
  • Monitor performance and costs
  • Follow security best practices
  • For more information, refer to the [official App Engine documentation](https://cloud.google.com/appengine/docs).

    Why This Matters

    Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

    Trade-offs to Consider

    Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

    When NOT to Use This

    Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

    Decision Framework

    Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.