FastAPI on Cloud Run

Google Cloud TechAbout 4 min readJun 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

Cloud Run, FastAPI, Uvicorn, Pydantic, Cloud Build, CI/CD pipelines, Docker, Google Cloud Storage, BigQuery, Domain-Driven Design, Functional Programming, 4D pipe, API development, Microservices.

Real-World Cloud Run Service with FastAPI

Main Topics and Key Points:

  • Mazlum Tosun, a Google Developer Expert and CDO at GroupBees, demonstrates how to build a real-world Cloud Run service using FastAPI.
  • The service reads football statistics from a file in Cloud Storage, applies business rules, and writes the results to a BigQuery table.
  • The example emphasizes a realistic project structure with multiple Python files, contrasting with simple "Hello World" examples.
  • The service uses FastAPI for API creation, Uvicorn for serving API calls, Pydantic for request validation, and Cloud Build for building and deploying the application.

Important Examples, Case Studies, or Real-World Applications Discussed:

  • The football statistics service serves as a practical example, mimicking the structure and complexity of APIs built for GroupBees' clients.
  • The use case highlights the need for a more structured approach than single-file examples, especially when dealing with business logic and data processing.

Step-by-Step Processes, Methodologies, or Frameworks Explained:

  • API Interaction: The deployed Cloud Run service is accessed via its URL, with interactive documentation available by appending /docs. The FastAPI-based API allows users to "Try It Out" by inputting football team slogans and executing the API call.
  • Data Processing: The API loads a JSON file from Cloud Storage containing football statistics, combines it with the team slogan from the HTTP request, calculates statistics (total goals, top scorer, best passer), and writes the result to BigQuery.
  • Code Organization: The directory structure includes a team_league directory for the Cloud Run service, containing a service directory with the Dockerfile and main.py (entry point).
  • Deployment: Two methods are described:
    • Local Deployment: Using Docker commands to build the container and deploy it to Cloud Run.
    • CI/CD Pipeline: Using Cloud Build configured via a YAML file, triggered manually or automatically upon code commit.

Key Arguments or Perspectives Presented, with Their Supporting Evidence:

  • Realistic API Development: Mazlum argues for a more structured approach to API development, moving beyond simple examples to reflect real-world complexity. The football statistics service demonstrates this with its multi-file structure and business logic.
  • Domain-Driven Design: The separation of domain logic (data classes and business rules) from HTTP handlers, influenced by domain-driven design, improves code readability and maintainability, especially in applications with many business rules.
  • Functional Programming: Using a pipeline of operations with the 4D pipe library makes the data transformation code concise and expressive.

Notable Quotes or Significant Statements with Proper Attribution:

  • Mazlum Tosun: "In my job, I have built many APIs in Cloud Run. And these APIs are called by other services, and they move and process data."
  • Mazlum Tosun: "By breaking out data classes under business rules, it's easier to read and understand them."
  • Mazlum Tosun: "FastAPI makes it a lot easier to build APIs. And it's easy to deploy these APIs on Cloud Run."

Technical Terms, Concepts, or Specialized Vocabulary with Brief Explanations:

  • Cloud Run: A fully managed compute platform that automatically scales stateless containers.
  • FastAPI: A modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.
  • Uvicorn: An ASGI (Asynchronous Server Gateway Interface) web server implementation for Python.
  • Pydantic: A Python library for data validation and settings management using type annotations.
  • Cloud Build: A Google Cloud service that executes your builds on Google Cloud infrastructure.
  • CI/CD Pipeline: A process that automates the building, testing, and deployment of software.
  • Docker: A platform for developing, shipping, and running applications in containers.
  • Google Cloud Storage: A scalable and durable object storage service.
  • BigQuery: A fully managed, serverless data warehouse that enables scalable analysis over petabytes of data.
  • Domain-Driven Design (DDD): A software development approach that focuses on modeling the software to match a domain, according to input from that domain's experts.
  • Functional Programming: A programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data.
  • 4D pipe: A Python library that facilitates functional programming by allowing operations to be chained together in a pipeline.

Logical Connections Between Different Sections and Ideas:

The video progresses logically from introducing the problem (building real-world Cloud Run services) to presenting a solution (using FastAPI and related tools), demonstrating the solution with a practical example, explaining the code structure and deployment process, and highlighting the benefits of domain-driven design and functional programming.

Data, Research Findings, or Statistics Mentioned:

  • The video doesn't explicitly mention specific research findings or statistics, but it implicitly relies on the established benefits of using FastAPI, Cloud Run, and CI/CD pipelines for efficient API development and deployment.

Conclusion

The video provides a comprehensive guide to building real-world Cloud Run services using FastAPI, emphasizing the importance of structured code organization, domain-driven design, and automated deployment pipelines. The football statistics service serves as a practical example, demonstrating how to integrate various Google Cloud services and leverage functional programming techniques for efficient data processing. The key takeaway is that by adopting these practices, developers can create scalable, maintainable, and robust APIs on Cloud Run.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.