正确配置 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 服务配置 Secret
使用安全机制向 A2A 服务器提供所有敏感凭证,例如 API 密钥和数据库密码。Cloud Run 支持将 Secret 作为环境变量提供或动态装载为卷。如需了解详情,请参阅在 Cloud Run 中配置 Secret。
例如,使用 gcloud CLI 在 Google Secret Manager 中创建和管理数据库用户和密码 Secret。如需了解详情,请参阅创建 Secret。
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"]],["最后更新时间 (UTC):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)."]]