In our caller tour of Google’s Agent Development Kit (ADK), I walked you done nan 3 approaches to building AI agents pinch ADK: Python, YAML, and nan Visual Builder. While section improvement is awesome for prototyping, production-ready agents request to beryllium deployed wherever they tin standard and service existent users.
Google Cloud Run is simply a earthy fresh for ADK agents. As a afloat managed serverless platform, it handles infrastructure concerns while you attraction connected supplier logic. What makes this deployment peculiarly elegant is ADK’s built-in adk deploy cloud_run command, which packages your agent, builds a instrumentality image, pushes it to Artifact Registry, and deploys it to Cloud Run — each successful a azygous step.
In this tutorial, I’ll guideline you done deploying a upwind and clip supplier to Cloud Run, complete pinch unafraid API cardinal guidance utilizing Google Secret Manager. By nan end, you’ll person a production-ready supplier accessible via a nationalist URL pinch nan ADK web UI enabled for interactive testing.
Understanding nan Deployment Architecture
Before diving into implementation, let’s understand what happens erstwhile you deploy an ADK supplier to Cloud Run.
The adk deploy cloud_run bid automates respective analyzable operations. It analyzes your task building and limitations to make an optimized Docker image. This image is past pushed to Google Artifact Registry, a unafraid instrumentality repository wrong your Google Cloud project. Finally, Cloud Run provisions a serverless lawsuit that runs your agent; pinch automatic scaling, HTTPS, and IAM integration.
The --with_ui flag is useful because it bundles nan ADK web improvement UI pinch your agent’s API server, giving you an interactive interface for testing conversations straight successful your browser.
Prerequisites
Before we begin, guarantee you have:
- Python 3.10 aliases higher installed.
- Google Cloud SDK (gcloud) installed and configured.
- A Google Cloud task pinch billing enabled.
- The pursuing APIs enabled: Cloud Run, Artifact Registry, Secret Manager, and Vertex AI.
- A Google API cardinal from Google AI Studio (for Gemini access).
Step 1: Setting Up nan Project Structure
Let’s create nan supplier task pinch nan required directory structure. ADK expects a circumstantial layout for deployment to activity correctly.
Create a caller directory for your agent:
mkdir weather_time cd weather_time |
ADK requires 3 files wrong your supplier directory: __init__.py, agent.py, and requirements.txt. The deployment instrumentality looks for a adaptable named root_agent successful your supplier codification — this naming normal is mandatory.
Create nan __init__.py file:
This record marks nan directory arsenic a Python package and imports nan supplier module.
Step 2: Building nan Agent
Create nan agent.py record pinch nan upwind and clip tools:
The supplier defines 2 tools: get_weather and get_current_time. Notice really each usability includes broad docstrings pinch type hints — ADK uses these to thief nan LLM understand erstwhile and really to invoke each tool. The root_agent adaptable is nan introduction constituent that ADK’s deployment bid will look for.
Create nan requirements.txt file:
Your task building should now look for illustration this:
requirements.txt
weather_time/
├── __init__.py
├── agent.py
Step 3: Testing Locally
Before deploying to Cloud Run, it’s basal to verify that your supplier useful correctly successful nan section environment. Create a .env record successful your project’s guidelines directory:
GOOGLE_GENAI_USE_VERTEXAI=0 GOOGLE_API_KEY=YOUR_API_KEY_HERE |
Replace nan placeholder pinch your existent Google AI Studio API key.
Navigate to nan genitor directory and tally nan supplier locally:
cd .. adk run weather_time |
Test nan supplier pinch prompts like:
[user]: What's nan upwind successful New York?
[user]: What clip is it successful New York?
Once you’ve confirmed nan supplier responds correctly, exit nan section convention by typing exit. We’re now fresh to deploy to Cloud Run.
Step 4: Configuring Google Cloud Secrets
Production deployments should ne'er hardcode API keys. Google Secret Manager provides unafraid retention and entree power for delicate credentials. Let’s shop our API cardinal there.
First, group nan situation variables for your Google Cloud project:
export GOOGLE_CLOUD_PROJECT="your-project-id" export GOOGLE_CLOUD_LOCATION="us-central1" |
Create nan concealed from your API key:
echo $GOOGLE_API_KEY | \ gcloud secrets create GOOGLE_API_KEY \ --project=$GOOGLE_CLOUD_PROJECT \ --data-file=- |
The Cloud Run work relationship needs support to entree this secret. Grant nan Secret Accessor role:
gcloud secrets add-iam-policy-binding GOOGLE_API_KEY \ --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" \ --role="roles/secretmanager.secretAccessor" \ --project=$GOOGLE_CLOUD_PROJECT |
Replace PROJECT_NUMBER pinch your existent Google Cloud task number. You tin find this successful nan Google Cloud Console aliases by running:
gcloud projects describe $GOOGLE_CLOUD_PROJECT --format="value(projectNumber)" |
Step 5: Deploying to Cloud Run
With secrets configured, we’re fresh to deploy. Set up nan deployment variables:
export AGENT_PATH="./weather_time" export SERVICE_NAME="weather-time" export APP_NAME="weather_time_app" |
Execute nan deployment command:
adk deploy cloud_run \ --project=$GOOGLE_CLOUD_PROJECT \ --region=$GOOGLE_CLOUD_LOCATION \ --service_name=$SERVICE_NAME \ --app_name=$APP_NAME \ --with_ui \ $AGENT_PATH |
Let’s break down these parameters:
During deployment, you whitethorn beryllium prompted astir unauthenticated access. For first testing, you tin let it (y); but for accumulation deployments, requiring authentication (N) is recommended.
The deployment process takes a fewer minutes. Upon completion, you’ll spot output akin to:
Deployment successful!
Service URL: https://weather-time-xxxxx.us-central1.run.app
Step 6: Testing nan Deployed Agent
Open nan Service URL successful your browser. Because you included nan --with_ui flag, you’ll spot nan ADK developer interface. This is nan aforesaid UI you utilized during section development, now moving connected Cloud Run.
Enable “Token Streaming” successful nan upper-right to amended responsiveness. You tin now interact pinch your deployed agent:
[user]: Hello! What's nan upwind for illustration successful New York today?
The supplier should respond utilizing nan get_weather tool, returning nan upwind accusation for New York.
Try a follow-up:
[user]: And what clip is it there?
The supplier will invoke nan get_current_time instrumentality to supply nan existent clip successful New York’s timezone.
Cleaning Up
To debar incurring early charges, delete nan Cloud Run work erstwhile you’re done experimenting:
gcloud run services delete $SERVICE_NAME \ --region=$GOOGLE_CLOUD_LOCATION \ --quiet |
You whitethorn besides want to delete nan secret:
gcloud secrets delete GOOGLE_API_KEY \ --project=$GOOGLE_CLOUD_PROJECT \ --quiet |
Looking Ahead
This tutorial demonstrated nan fastest way from a section ADK supplier to a accumulation Cloud Run deployment. The adk deploy cloud_run bid abstracts distant containerization complexity, allowing you to attraction connected supplier logic while leveraging Cloud Run’s serverless scalability.
In consequent tutorials, we’ll research precocious deployment scenarios; including connecting ADK agents to MCP servers for outer instrumentality integration, implementing convention persistence pinch Cloud SQL, deploying multi-agent systems pinch Sequential and Parallel orchestration patterns, and configuring GPU-accelerated backends for moving unfastened models for illustration Gemma.
The instauration you’ve built present — knowing nan deployment workflow, managing secrets securely, and testing pinch nan web UI — will service you good arsenic we tackle progressively blase accumulation architectures. Stay tuned!
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don't miss an episode. Subscribe to our YouTube channel to watercourse each our podcasts, interviews, demos, and more.
Group Created pinch Sketch.
English (US) ·
Indonesian (ID) ·