在GCP云功能中添加跨项目服务帐户
在 GCP 中从项目 A
向项目 B
添加了一个 IAM 服务帐户
,并具有云函数管理员权限
我们已 现在尝试使用相同的服务帐户
在项目B
中创建云函数。
但在创建云函数
时,下拉菜单中未列出服务帐户
。只有本地创建的服务帐户
在下拉菜单中可见。
对于在云功能
中使用跨服务帐户
有什么建议吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用外部项目中的服务帐户作为云功能的运行时 SA,这就是您在下拉菜单中看不到服务帐户的原因。
当您向
项目 B
中的项目 A
的服务帐户授予权限时,您只需执行此操作即可授予权限。请记住,SA 仍然是项目 A
的一部分,而不是项目 B
。这与Cloud Scheduler的工作方式相同:
此处的步骤建议执行此操作可能的方法是使用
项目 B
的服务帐户来模拟项目 A
的服务帐户,但您会注意到,实际上您附加了来自同一项目的帐户,功能是。如果您需要从
项目 B
访问项目 A
中的资源,请在您的函数中使用同一项目中的 SA,并在中添加该 SA 所需的权限项目 A
或使用模拟方法(如果您愿意)。You cannot use Service Accounts from external projects as a runtime SA for a Cloud Function, this is why you don't see the service account in the dropdown menu.
When you grant permissions to the Service Account from
Project A
inProject B
you only do that, grant permissions. Keep in mind that the SA continues being part ofproject A
and notproject B
.This works in the same way as Cloud Scheduler:
What the steps here suggest to make this possible is to impersonate a the Service Account from
project A
using a service account ofproject B
but you will notice that actually you attach an account from the same project were the function is.If you need to access to resources in
project A
fromproject B
then use in your function a SA from the same project and add the needed permissions for that SA inproject A
or use the impersonation approach if you prefer.我很困惑:-(
我使用
gcloud
尝试过此操作,但无法使其工作。我尝试在所需的(远程)服务帐户,但我继续得到:
也许远程帐户也需要云功能部署权限?
I'm flummoxed :-(
I tried this using
gcloud
and am unable to get it work.I tried granting the Cloud Functions project's Compute Engine service account
roles/serviceAccountUser
on the desired (remote) Service Account but I continue to get:Perhaps the remote account need Cloud Functions deploy permissions too?
是的,可以跨项目使用服务帐户来部署云功能。
请点击下面提到的链接来授予必要的权限。
https://cloud.google.com/functions/docs/secure/ function-identity#gcloud_2
如果服务帐号和函数在不同的项目中,则从服务帐号所在的项目中:
配置服务帐号跨项目工作。
将服务帐户令牌创建者角色 (roles/iam.serviceAccountTokenCreator) 授予以下两个 Google 管理的服务帐户:
App Engine 默认服务帐户 ([电子邮件受保护])
Cloud Functions 服务代理 ([电子邮件受保护])
这些 Google 帐户管理您的服务帐户的跨项目访问。
授予服务帐户对资源的访问权限。执行此操作的方法取决于资源类型。
将服务帐户与您的功能连接起来。您可以在部署时或通过更新以前部署的函数来执行此操作。
使用上述指导,我们仍然无法跨项目查看新的 SA,但能够通过 CLI 部署相同的 SA。
示例 CLI 如下
gcloud 函数部署 FUNCTION_NAME
--trigger-topic TOPIC_NAME --region REGION_NAME --runtime RUNTIME --entry-point ENTRY_POINT --service-account SERVICE_ACCOUNT_EMAIL
请注意:使用 CLI 部署 CF 时,您需要位于 main.py 和其他文件所在的工作目录中位于。
Yes it possible to use service account across projects to deploy Cloud Function.
Please follow below mentioned link to grant the necessary permissions.
https://cloud.google.com/functions/docs/securing/function-identity#gcloud_2
If the service account and the function are in different projects, from the project where the service account is located:
Configure the service account to work across projects.
Grant the Service Account Token Creator role (roles/iam.serviceAccountTokenCreator) to both of these Google-managed service accounts:
App Engine default service account ([email protected])
Cloud Functions Service Agent ([email protected])
These Google accounts manage cross-project access for your service account.
Grant the service account access to the resource. The method for doing this depends on the resource type.
Connect the service account with your function. You can do this either at deployment time or by updating a previously deployed function.
Using the above guidance we are still not able to view the new SA across the projects but able to deploy the same via CLI.
Sample CLI is as below
gcloud functions deploy FUNCTION_NAME
--trigger-topic TOPIC_NAME --region REGION_NAME --runtime RUNTIME --entry-point ENTRY_POINT --service-account SERVICE_ACCOUNT_EMAIL
Please note: While deploying the CF using CLI, you need to be in working directory where main.py and other files are located.