Google compute engine 使用来自另一个Google项目的服务帐户创建Google Compute实例

Google compute engine 使用来自另一个Google项目的服务帐户创建Google Compute实例,google-compute-engine,gcloud,service-accounts,Google Compute Engine,Gcloud,Service Accounts,我想知道是否可以将在my-project-a中创建的服务帐户附加到例如my-project-b中的Google计算引擎实例 以下命令: 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-platfor

我想知道是否可以将在my-project-a中创建的服务帐户附加到例如my-project-b中的Google计算引擎实例

以下命令:

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中),没有从任何其他项目添加服务帐户的选项。我希望能够添加帐户我的服务-account@my-project-a.iam.gserviceaccount.com


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

  • 在my-project-a中为计算引擎创建一个具有适当角色的服务帐户
  • 下载JSON文件
  • 复制my-project-a新服务帐户电子邮件
  • 在my-project-b上,使用上一步复制的电子邮件
  • 通过SSH连接到my-project-b中的实例
  • 在my-project-b实例上复制步骤2中的JSON文件
  • 运行以下命令以执行以下操作:

    gcloud auth activate service account--key file=YOUR_JSON_文件

  • 使用以下命令进行验证:

    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验证列表


  • 谢谢@F10,这很有道理。然而,我希望通过创建命令行步骤来设置它。我将在几天前给出答案,然后将此答案标记为已接受答案。