I am trying to connect to an FTP server using a Google Cloud Function. My Cloud Function uses a VPC Connector to access resources in my VPC network. The Cloud Function successfully connects to the FTP server when using the default VPC network, but it fails to connect when using a custom VPC network, even though the settings are an exact replica of the default VPC network.
Here's what I have tried so far:
- I have set up a VPC Connector associated with the custom VPC network and configured the Cloud Function to use this VPC Connector.
- I have copied the firewall rules from the default VPC network to the custom VPC network, ensuring that they allow the same traffic (including the rule for default-allow-internal with the custom subnet's IP range).
- I have verified that the custom VPC network is configured with a Cloud NAT gateway to allow instances (and the VPC Connector) to access the internet.
- My Cloud Function can access the internet using the custom VPC network (e.g., by sending an HTTP request to https://api.ipify.org?format=json) and retrieve the static IP configured on the cloud-nat, but it fails to connect to the FTP server only when using the custom VPC network.
Below is the Cloud Function code I'm using to connect to the FTP server:
import os
import sys
import argparse
from ftplib import FTP
def test_ftp_connection(server, username, password):
try:
ftp = FTP(server)
ftp.login(user=username, passwd=password)
print(f'Successfully connected to {server} as {username}')
ftp.quit()
return True
except Exception as e:
print(f'Error connecting to {server} as {username}: {e}')
return False
def main(request):
request_json = request.get_json(silent=True)
request_args = request.args
username = 'username'
password = 'password'
server = 'example.com'
if request_json and 'username' in request_json:
username = request_json['username']
elif request_args and 'username' in request_args:
username = request_args['username']
if request_json and 'password' in request_json:
password = request_json['password']
elif request_args and 'password' in request_args:
password = request_args['password']
if request_json and 'server' in request_json:
server = request_json['server']
elif request_args and 'server' in request_args:
server = request_args['server']
if not test_ftp_connection(server, username, password):
return 'Failed to connect to FTP server', 500
else:
return 'Successfully connected to FTP server', 200
The Cloud Function works as expected and connects to the FTP server when using the default VPC network. However, it times out and fails to connect when using the custom VPC network.
Any suggestions on how to troubleshoot this issue and get the Cloud Function to connect to the FTP server using the custom VPC network?