How to Configure app.yaml in GCP for App Engine Deployment
To configure
app.yaml in GCP, create a YAML file that defines your app's runtime, handlers, and environment settings for App Engine. This file tells GCP how to run your app and what resources it needs.Syntax
The app.yaml file uses YAML format to specify your app's configuration. Key parts include:
- runtime: Defines the programming language environment (e.g., python39, nodejs16).
- handlers: Lists URL patterns and how to serve them (static files or scripts).
- env_variables: Sets environment variables your app can use.
- instance_class: (Optional) Specifies the instance size for your app.
yaml
runtime: python39 handlers: - url: /static static_dir: static/ - url: /.* script: auto env_variables: ENV: production instance_class: F2
Example
This example shows a simple app.yaml for a Python 3.9 App Engine app that serves static files from a folder and runs dynamic requests with the default script handler.
yaml
runtime: python39 handlers: - url: /static static_dir: static/ - url: /.* script: auto env_variables: ENV: production instance_class: F2
Output
When deployed, requests to /static/* serve files from the static folder, and other requests run your Python app automatically.
Common Pitfalls
Common mistakes when configuring app.yaml include:
- Using incorrect indentation or syntax, which breaks YAML parsing.
- Missing the
runtimefield, causing deployment errors. - Not specifying handlers correctly, leading to 404 errors for static files or dynamic routes.
- Forgetting to set environment variables needed by your app.
Always validate your YAML file and test locally if possible.
yaml
runtime: python39 handlers: - url: /static static_dir: static/ - url: /.* script: auto # Wrong indentation example (will cause error): # handlers: # - url: /static # static_dir: static/ # Correct indentation example: handlers: - url: /static static_dir: static/
Quick Reference
| Field | Description | Example |
|---|---|---|
| runtime | Specifies the language runtime | python39, nodejs16 |
| handlers | Defines URL patterns and how to serve them | - url: /static\n static_dir: static/ |
| env_variables | Sets environment variables for the app | ENV: production |
| instance_class | Sets the instance size (optional) | F1, F2, B1 |
| automatic_scaling | Configures scaling behavior (optional) | min_instances: 1 |
Key Takeaways
Always specify the runtime to tell GCP which language environment to use.
Define handlers carefully to route URLs to static files or your app code.
Use proper YAML indentation to avoid syntax errors.
Set environment variables in app.yaml to configure your app without code changes.
Test your app.yaml locally or with gcloud commands before deploying.