正確設定 IAM 角色,是確保 Cloud Run 服務與其他服務安全互動的重要步驟。 Google Cloud建立專屬服務帳戶,並授予下列各節列出的特定權限,確保作業安全和效率。
建立 Cloud Run 服務帳戶
執行任何 gcloud 指令前,請務必先完成驗證。執行下列指令,登入 Google Cloud 帳戶:
gcloudauthlogin
為已部署的 A2A 服務執行個體建立專用服務帳戶。使用 gcloud iam service-accounts create 指令。
gcloudiamservice-accountscreateA2A_SERVICE_ACCOUNT_NAME\--description="Service account for A2A Cloud Run service"\--display-name="A2A Cloud Run Service Account"
本節說明將 A2A 代理程式部署至 Cloud Run 時所需的設定,確保雲端作業安全、有效率且可擴充。
設定 Cloud Run 服務的密鑰
使用安全機制,將所有機密憑證 (例如 API 金鑰和資料庫密碼) 提供給 A2A 伺服器。Cloud Run 支援以環境變數或動態掛接磁碟區的形式提供密鑰。詳情請參閱「在 Cloud Run 中設定密鑰」。
舉例來說,您可以使用 gcloud CLI 在 Google Secret Manager 中建立及管理資料庫使用者和密碼密鑰。詳情請參閱「建立密鑰」。
gcloudsecretscreatealloy_db_user--replication-policy="automatic"# Create a file user.txt with contents of secret value
gcloudsecretsversionsaddalloy_db_user--data-file="user.txt"
gcloudsecretscreatealloy_db_pass--replication-policy="automatic"# Create a file pass.txt with contents of secret value
gcloudsecretsversionsaddalloy_db_pass--data-file="pass.txt"
建立 Dockerfile 以進行容器化
Cloud Run 可以從已代管的容器映像檔部署服務,也可以直接從原始碼部署服務。從原始碼進行部署作業時,如果專案的根目錄中存在 Dockerfile,Cloud Run 就會自動建構容器映像檔。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Host A2A agents on Cloud Run overview\n\nThis guide provides an overview of how to prepare and configure Agent2Agent\n(A2A) agents for deployment on Cloud Run. It covers essential steps\nsuch as setting up a cloud environment, configuring required\nIdentity and Access Management (IAM) roles, and preparing your agent for deployment.\n\nBefore you begin\n----------------\n\nBefore you start developing and deploying your A2A agent, familiarize yourself\nwith the following concepts and resources:\n\n- Review the official [A2A specification](https://a2aproject.github.io/A2A/latest/specification/) to understand the protocol architecture and [core concepts](https://a2aproject.github.io/A2A/latest/topics/key-concepts/) for agent communication.\n- Explore existing [sample agents](https://github.com/a2aproject/a2a-samples) to gain practical insights and accelerate your A2A agent development process. Specifically, review the [Google Cloud Run deployable sample](https://github.com/a2aproject/a2a-samples/tree/main/samples/python/agents/adk_cloud_run) that uses the [Agent Development Kit (ADK)](https://google.github.io/adk-docs/).\n\nA2A agent deployment roadmap\n----------------------------\n\nTo deploy your agent, complete the following steps:\n\n- Understand the [A2A specification](https://a2aproject.github.io/A2A/latest/specification/) and use [sample agents](https://github.com/a2aproject/a2a-samples) to accelerate development.\n- Establish secure [IAM roles](#iam-roles) for your Cloud Run service.\n- Configure your cloud environment by setting up necessary [secrets](#configure-secrets) and creating a [Dockerfile](#create-dockerfile).\n- Execute the [Cloud Run deployment](/run/docs/deploy-a2a-agents) command.\n- [Test and monitor](/run/docs/verify-deployment-a2a-agents) agent performance after deployment.\n\nHigh-level architecture\n-----------------------\n\nThe A2A Agent's core is a serving and orchestration layer, such as\nCloud Run. This layer manages interactions with AI models like Gemini\nand Vertex AI, memory storages like AlloyDB and A2A TaskStore, and external tools\nthrough APIs. Clients interact with the agent by sending requests, such as \"Get\nAgent Card\" or \"send message,\" and receive task updates.\n\nThe following diagram illustrates the architecture of an A2A Agent system,\nshowing an A2A Client (user or agent) interacting with the A2A Agent.\nA2A agent architecture\n\nFor information about A2A request lifecycle, see the\n[A2A Request Lifecycle](https://a2a-protocol.org/latest/topics/what-is-a2a/#a2a-request-lifecycle)\nsection.\n\nIAM roles and permissions for Cloud Run A2A agents\n--------------------------------------------------\n\nProperly configured IAM roles are important for\nyour Cloud Run service to interact securely with other Google Cloud\nservices. Create a dedicated service account and grant the specific permissions\nlisted in the following sections to ensure operational security and efficiency.\n\n### Create a Cloud Run service account\n\nBefore you run any `gcloud` commands, ensure you are authenticated. Run the\nfollowing command to sign in to your Google Cloud account: \n\n gcloud auth login\n\nCreate a service account specifically for your deployed A2A service instance.\nUse the `gcloud iam service-accounts create` command. \n\n gcloud iam service-accounts create \u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e \\\n --description=\"Service account for A2A Cloud Run service\" \\\n --display-name=\"A2A Cloud Run Service Account\"\n\nReplace \u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e with the name of the service account.\n\n### Configure IAM roles for A2A agent\n\nAssign the following IAM roles to your service account based on the Google Cloud\nservices your A2A agent interacts with:\n\n#### Secret Manager access for secure credentials\n\n- **Role:** `Secret Manager Secret Accessor` (`roles/secretmanager.secretAccessor`)\n- **Purpose:** Allows the Cloud Run service account to securely\n fetch secrets, such as database credentials, from Secret Manager.\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e \\\n --member=\"serviceAccount:\u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e@\u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com\" \\\n --role=\"roles/secretmanager.secretAccessor\"\n\n#### Vertex AI model access for AI capabilities\n\n- **Role:** `Vertex AI User` (`roles/aiplatform.user`)\n- **Purpose:** Required for the Cloud Run service account to\n invoke prediction APIs on Vertex AI models.\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e \\\n --member=\"serviceAccount:\u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e@\u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com\" \\\n --role=\"roles/aiplatform.user\"\n\n#### Alloy DB instance access for persistent storage (if applicable)\n\n- **Roles:** `AlloyDB Client` (`roles/alloydb.client`) and `Service Usage\n Consumer` (`roles/serviceusage.serviceUsageConsumer`)\n- **Purpose:** Enables the Cloud Run service identity to interact\n with the AlloyDB cluster for persistent task storage, which is crucial\n for production A2A agents.\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e \\\n --member=\"serviceAccount:\u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e@\u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com\" \\\n --role=\"roles/alloydb.client\"\n\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e \\\n --member=\"serviceAccount:\u003cvar translate=\"no\"\u003eA2A_SERVICE_ACCOUNT_NAME\u003c/var\u003e@\u003cvar translate=\"no\"\u003eYOUR_PROJECT_ID\u003c/var\u003e.iam.gserviceaccount.com\" \\\n --role=\"roles/serviceusage.serviceUsageConsumer\"\n\nPrepare the A2A agent for Cloud Run deployment\n----------------------------------------------\n\nThis section describes the configurations required to prepare your A2A agent for\ndeployment to Cloud Run, ensuring secure, efficient, and scalable\noperation in the cloud.\n\n### Configure secrets for Cloud Run services\n\nProvide all sensitive credentials, such as API keys and database passwords, to\nyour A2A server using a secure mechanism. Cloud Run supports providing\nsecrets as environment variables or dynamically mounted volumes. For more\ninformation, see [Configuring secrets in Cloud Run](/run/docs/configuring/secrets).\n\nFor example, create and manage database user and password secrets within Google\nSecret Manager using the `gcloud` CLI. For more information, see\n[Create a secret](/secret-manager/docs/creating-and-accessing-secrets#create). \n\n gcloud secrets create alloy_db_user --replication-policy=\"automatic\"\n # Create a file user.txt with contents of secret value\n gcloud secrets versions add alloy_db_user --data-file=\"user.txt\"\n\n gcloud secrets create alloy_db_pass --replication-policy=\"automatic\"\n # Create a file pass.txt with contents of secret value\n gcloud secrets versions add alloy_db_pass --data-file=\"pass.txt\"\n\n### Create Dockerfile for containerization\n\nCloud Run can deploy services either from already hosted container\nimages or directly from your source code. When deploying from source code,\nCloud Run automatically builds a container image if a Dockerfile is\npresent in your project's root directory.\n\nThe following is a sample Dockerfile for A2A agent deployment: \n\n FROM python:3.13-slim\n COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/\n EXPOSE 8080\n WORKDIR /app\n COPY . ./\n RUN uv sync\n ENTRYPOINT [\"uv\", \"run\", \".\", \"--host\", \"0.0.0.0\", \"--port\", \"8080\"]\n\n#### Deploy from source code without a Dockerfile\n\nFor source code repositories without a `Dockerfile`, Cloud Run offers\nin-built support for certain popular programming languages, simplifying the\ncontainerization process.\n\n- **Python applications on Cloud Run:** Cloud Run typically looks for a [`main.py`](https://github.com/a2aproject/a2a-samples/blob/50b7363f11477f400520affef4ac748e5117fee2/samples/python/agents/adk_cloud_run/__main__.py) file to build and deploy Python services. For more information, see [Deploy Python Service Quickstart on Cloud Run](/run/docs/quickstarts/build-and-deploy/python).\n- **Node.js applications on Cloud Run:** See [Deploy Node.js Service Quickstart on Cloud Run](/run/docs/quickstarts/build-and-deploy/nodejs).\n\nWhat's next\n-----------\n\n- [Deploy A2A agents to Cloud Run](/run/docs/deploy-a2a-agents).\n- [Test and monitor A2A agents](/run/docs/verify-deployment-a2a-agents)."]]