可以获取 Google Cloud Functions 的静态 IP 地址吗?

Possible to get static IP address for Google Cloud Functions?

我想开发一个 Google Cloud Function,它将订阅 Google Cloud Storage 存储桶中的文件更改并将文件上传到第三方 FTP 站点。此 FTP 站点需要客户端的允许列表 IP 地址。

因此,可以为 Google Cloud Functions 容器获取静态 IP 地址吗?

查看下面的答案——花了很多年,但现在支持了。

https://cloud.google.com/functions/docs/networking/network-settings#associate-static-ip

无法为 Google Cloud Functions 分配静态 IP,因为它与 'serverless' 架构的性质非常正交,即按需分配和解除分配服务器。

但是,您可以利用 HTTP 代理来实现类似的效果。设置一个 Google Compute Engine 实例,为其分配一个静态 IP 并安装一个代理库,例如 https://www.npmjs.com/package/http-proxy。然后,您可以通过此代理路由所有外部 API 呼叫等。

但是,这可能会降低规模和灵活性,但这可能是一种解决方法。

更新:此功能现已在 GCP 中可用 https://cloud.google.com/functions/docs/networking/network-settings#associate-static-ip


首先这不是一个无理的要求,不要生气。 AWS Lambdas 已经支持此功能并且已经有一段时间了。如果您对此功能感兴趣,请为该功能请求加注星标:https://issuetracker.google.com/issues/112629904

其次,我们得出了一个 work-around,我也将其发布到那个问题上,也许这对你也有用:

  • 设置 VPC 连接器
  • 在 VPC 上创建 Cloud NAT
  • 创建一个没有 public IP 的代理主机,因此出口流量通过 Cloud NAT 路由
  • 配置使用 VPC 连接器的 Cloud Functions,并配置为对所有出站流量使用代理服务器

对这种方法的警告:

  • 我们想将代理放在托管实例组中并放在 GCP 内部 LB 后面,以便动态扩展,但 GCP 支持已确认这是不可能的,因为 GCP ILB 基本上 allow-lists 子网,并且 Cloud Functions CIDR 在该子网之外

希望对您有所帮助。

更新:就在前几天,他们宣布了这个确切功能的 early-access 测试版!!

“这里是 Cloud Functions PM。如果您想测试一下,我们实际上有一个 early-access 预览此功能。

请填写此表格,以便我们添加您..."

表格可以在上面链接的 Issue 中找到。

此功能现在是 Google Cloud Functions (see here)

的原生部分

根据 the GCF docs,这是一个两步过程:

Associating function egress with a static IP address In some cases, you might want traffic originating from your function to be associated with a static IP address. For example, this is useful if you are calling an external service that only allows requests from whitelisted IP addresses.

Route your function's egress through your VPC network. See the previous section, Routing function egress through your VPC network.

Set up Cloud NAT and specify a static IP address. Follow the guides at Specify subnet ranges for NAT and Specify IP addresses for NAT to set up Cloud NAT for the subnet associated with your function's Serverless VPC Access connector.

参考下面link: https://cloud.google.com/functions/docs/networking/network-settings#associate-static-ip 根据 Google,该功能已发布查看整个线程 https://issuetracker.google.com/issues/112629904

对于那些想要将云功能与静态 IP 地址相关联以便将 API 或类似内容的 IP 列入白名单的用户,我建议您查看此分步指南,它对我有很大帮助: https://dev.to/alvardev/gcp-cloud-functions-with-a-static-ip-3fe9.

我还想说明此解决方案适用于 Google Cloud Functions 和 Firebase Functions(因为它基于 GCP)。