Hello! Welcome to the final lesson of your FastAPI course.
In our previous lessons, we have gone through the entire lifecycle of building and deploying a web service. You've built a containerized FastAPI application, pushed it to Google Artifact Registry, and deployed it to Google Cloud Run, complete with secure configuration using environment variables and Google Secret Manager. Your application is now live and accessible on the internet.
Today, we'll focus on the final, crucial step: testing the publicly accessible API endpoint after deployment. This lesson will confirm that your deployed service is fully functional and behaving as expected. We will use the interactive Swagger UI, command-line tools like curl, and the monitoring features within Google Cloud Run to systematically verify your live API.
This final check is what validates all our previous work, ensuring that the service is not just running, but is ready to be integrated with a frontend application or used by other services.
1. Locating Your Deployed Service URL
After a successful deployment, Google Cloud Run assigns a unique, permanent URL to your service. You'll need this URL to send requests to your API. There are a few ways to find it:
- From the deployment command output: The
gcloud run deploycommand prints the service URL upon successful completion. - From the Google Cloud Console: Navigate to Cloud Run in your project, click on your service, and the URL will be displayed prominently at the top of the service details page.
- Using the
gcloudCLI: This is a reliable and scriptable way to retrieve the URL at any time.
The following resource provides a concise guide on deploying and then retrieving the service URL.
Hello Cloud Run with Python (FastAPI) - Google Codelabs
The 'Hello Cloud Run with Python (FastAPI)' codelab from Google demonstrates the full deployment workflow. We will focus on the final part, where it shows how to retrieve the service URL and test it.
Read the section titled 'Deploy to Cloud Run'. Focus specifically on the part after the deployment command succeeds, starting from 'On success, the command line displays the service URL' to the end of the section. Note the gcloud run services describe command, which is a very useful way to programmatically fetch the URL.
To get your service's URL, you can run a similar command, replacing helloworld-fastapi with your service name and specifying your region:
gcloud run services describe [YOUR_SERVICE_NAME] \
--region [YOUR_REGION] \
--format="value(status.address.url)"
Store this URL, as we will use it for all the tests in this lesson.
2. Testing with the Interactive API Docs (Swagger UI)
One of the best features of FastAPI is its automatically generated interactive documentation. Just like you did during local development, you can use the live Swagger UI to test your endpoints. This is often the quickest way to perform a sanity check.
- Open a web browser and navigate to
[YOUR_SERVICE_URL]/docs. - You should see the familiar Swagger UI, listing all the endpoints you've created.
- You can expand an endpoint, click "Try it out", fill in any required parameters, and click "Execute".
The following video demonstrates this exact process on a deployed Cloud Run service.
This clip from 'FastAPI on Cloud Run' by Google Cloud Tech shows how to access the live documentation and make a test call directly from the browser.
Watch from 02:47 to 04:21. Observe how the presenter navigates to the /docs URL, uses the 'Try it out' button, provides a sample request body, and inspects the response from the live API.
Try this now with the /info endpoint we created in the last lesson. You should receive a 200 OK response with the body {"environment":"production"}, confirming that your app is running and correctly reading the environment variable you configured.
3. Testing from the Command Line with curl
While the Swagger UI is great for interactive testing, curl is an indispensable tool for more rigorous, scriptable, and non-browser-based testing. It allows you to craft and send any kind of HTTP request directly from your terminal.
Testing a GET Endpoint
This is the simplest test. Let's call your /info endpoint.
# Replace [YOUR_SERVICE_URL] with the URL you retrieved earlier
curl [YOUR_SERVICE_URL]/info
You should see the JSON response printed directly to your terminal: {"environment":"production"}.
Testing an Endpoint with a Request Body
If you have a POST or PUT endpoint that expects a JSON body (like the ones you built in the database modules), curl can send that data using the -X flag for the method, the -H flag for the Content-Type header, and the -d flag for the data payload.
For example, if you had an endpoint at /items that creates a new item, the command would look like this:
curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "My New Item", "price": 19.99}' \
[YOUR_SERVICE_URL]/items
This simulates exactly what a client application would do when sending data to your API.
4. Verifying with Logs and Metrics
A successful response is a good sign, but what happens if something goes wrong? Or if you want to confirm what happened on the server side? The final piece of the testing puzzle is to inspect the service's logs and metrics in Google Cloud.
Deploy Python Applications - Google Cloud Run with Docker
This short clip from 'Deploy Python Applications - Google Cloud Run with Docker' shows the result of a deployment and interacting with the live application, which is the final outcome we are aiming for.
Watch from 14:39 to 15:29. The video shows the final deployed application being tested in the browser. This reinforces the idea that what you tested locally is now publicly available and functional.
Now, let's go beyond what the user sees and look at the server's perspective.
- Navigate to the Cloud Run dashboard in the Google Cloud Console and select your service.
- Click on the "LOGS" tab.
Here you will see a real-time stream of logs generated by your service. After making a few test requests with your browser or curl, you should see entries corresponding to those requests, like:
INFO: 169.254.1.1:0 - "GET /docs HTTP/1.1" 200 OK
INFO: 169.254.1.1:0 - "GET /info HTTP/1.1" 200 OK
If an error occurs (e.g., a bug in your code causes a 500 Internal Server Error), the full Python traceback will appear here. This makes the logs your most important tool for debugging issues in your deployed application.
Next, click on the "METRICS" tab. Here you can see graphs for:
- Request Count: How many requests your service is receiving.
- Request Latency: How long your service takes to respond.
- Container CPU and Memory Utilization: How much resources your application is using.
These metrics are essential for monitoring your application's health and performance over time.
Shipping Fast with FastAPI and Cloud Run - Real Kinetic Blog
The blog post 'Shipping Fast with FastAPI and Cloud Run' briefly mentions this final verification step, which puts our actions into a professional context.
Read the section titled 'The Finale'. Notice how the author describes the testing process: using the /docs page, checking the database (if applicable), and importantly, heading over to the Cloud Run console to view Metrics and Logs. This aligns perfectly with our verification workflow.
Course Conclusion
Congratulations! You have reached the end of the course and successfully built, containerized, configured, deployed, and tested a production-ready FastAPI application on Google Cloud Run.
Key Takeaways from this Lesson:
- You can test a live API using the same tools as local development: the interactive Swagger UI for quick checks and
curlfor command-line testing. - The URL for your deployed service can be found in the
gcloudcommand output, the Cloud Console, or by usinggcloud run services describe. - Cloud Run Logs are your primary tool for debugging live applications, providing request information and error tracebacks.
- Cloud Run Metrics give you insight into your service's performance and resource consumption.
Over this entire course, you have journeyed from the fundamentals of HTTP to deploying a sophisticated, scalable service. You now have a complete, end-to-end skill set for backend development with FastAPI, a foundation that is directly applicable to your goal of deploying AI/ML models.
What's Next?
Your journey doesn't end here. With this solid foundation, you can now explore more advanced topics:
- Full CI/CD Pipeline: Automate the entire build and deploy process using Google Cloud Build, as hinted at in some of our resources.
- Advanced FastAPI: Dive deeper into features like WebSockets, dependency overrides for complex scenarios, and custom middleware.
- Integrating a Real ML Model: Adapt the ML model serving patterns from Module 8 to load and serve one of your own pre-trained models.
- Advanced Cloud Architecture: Explore other GCP services like Cloud Storage for handling file uploads or Pub/Sub for asynchronous background tasks.
You have the skills and the practical experience. Keep building, keep learning, and thank you for taking this course