首页 > 解决方案 > 使用来自另一个 Google 项目的服务帐户创建 Google 计算实例

问题描述

我想知道是否可以将 my-project-a 中创建的服务帐户附加到my -project-b中的 Google Compute Engine 实例?

以下命令:

gcloud beta compute instances create my-instance \

--service-account=my-service-account@my-project-a.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--project=my-project-b

给我以下错误:

(gcloud.beta.compute.instances.create) Could not fetch resource:
- The user does not have access to service account 'my-service-account@my-project-a.iam.gserviceaccount.com'.  User: 'me@mysite.com'.  Ask a project owner to grant you the iam.serviceAccountUser role on the service account.  me@mysite.com is my account and I'm the owner of the org.

不确定这是否相关,但查看 UI(在my-project-b中)没有选项可以从任何其他项目添加服务帐户。我希望能够添加帐户my-service-account@my-project-a.iam.gserviceaccount.com

在此处输入图像描述

标签: google-compute-enginegcloudservice-accounts

解决方案


您可以按照以下步骤将服务帐户从 my-project-a 验证到 my-project-b 中的实例:

  1. 在 my-project-a 中创建一个具有计算引擎适当角色的服务帐户
  2. 下载 JSON 文件。
  3. 复制 my-project-a 新服务帐户电子邮件
  4. 在 my-project-b 上,使用从上一步复制的电子邮件添加团队成员
  5. 通过 SSH 连接到 my-project-b 中的实例
  6. 将步骤 2 中的 JSON 文件复制到 my-project-b 实例上
  7. 运行以下命令以激活服务帐户

    gcloud auth activate-service-account --key-file=YOUR_JSON_FILE

  8. 使用以下命令进行验证:

    gcloud 授权列表


推荐阅读