Search code examples
google-cloud-functionsvirtual-machinessh-keys

Use Google Cloud Functions to ssh[keys] into VM


Is it possible to use Google Cloud Functions to ssh into a on-prem vm? specifically can I give the Cloud Functions a private key to use?

I am thinking of running a timer job that will get data from a vm not in GCP with a python script.

  1. is it possible?
  2. is there a reason i shouldn't do this?

I have not been able to test this because I don't have access to GCP yet and I have not been able to find any documentation that mentions this. I do realize Functions might not be able to do this which is why I am curious if anyone has tried this in the past.


Solution

  • From the best of my knowledge - it is possible. Your code in the cloud function - just an ordinary code. If you use python, for example, you can use 'requests' or 'paramico' or 'pysftp' or any other library. No (to be precise - very little) restrictions.

    You can use a private key for that purpose. I would suggest to store the private key in the Secret Manager, so it is retrieved in the runtime (you need to write code for that retrieval).

    Be aware, however, that the cloud function are restricted by maximum 2Gb of memory (shared between RAM and 'fake' local drive, so you can use '/tmp' directory as if you have a local drive); and restricted by 540 seconds (9 minutes) timeout. Thus you need to 'push' all you would like to do into those boundaries.

    In addition, access to an external IP address might be whitelisted by the external party. You may need to use some additional network configuration so that all 'calls' from your function are originated from one dedicated IP address. That is possible as well.

    For a timer - you may use a Cloud Scheduler, which can send a message into a Pub/Sub topic according to your cron timetable. The cloud function is to be on the other side of the Pub/Sub.