0

I have the following lines of python code

import os
def hello_world():
  r=os.system("curl ipinfo.io/ip")
  print (r)
hello_world()

Shows the desired output when executed from command line in Google Cloud Shell but seems there is a 0 at the end of IP Address output

 $ python3 main2.py
   34.X.X.2490

When I deployed the same code in Google CLoud function it is showing OK as output

I have to replace the first line of code in GCF as follows to make it deploy.

   def hello_world(self):

Any suggestion so that GCF displays the desired output which is the output of curl command?

4
  • Just for my curiosity - why would you like to know the IP address from which the container (where the cloud function instance is invoked) makes a request to an external world? What are you going to do with it? Commented Mar 10, 2021 at 13:55
  • It is for testing purpose . Just want to see the desired output of curl command and was checking with this URL Commented Mar 10, 2021 at 14:06
  • 1
    I am not sure that OS level commands are going to work in a way you might expect, as, from the best of my knowledge, the cloud function does not have a direct access to the underlined OS. You might try some python package with the desired functionality, which is deployed in the run time environment - either as a standard library, or some additional package, you explicitly state in the 'requirements.txt' file - for example - 'requests' or some other... Commented Mar 10, 2021 at 14:38
  • just to add - any two arbitrary cloud function invocations (of the same deployed cloud function) might have different exposed (to the external world) IP address in a general case. Commented Mar 10, 2021 at 14:40

1 Answer 1

1

Your function won't work for 2 reasons:

  1. Firstly, you don't respect the HTTP Cloud Function Python function signature:
def hello_world(request):
  ....
  1. Secondly, you can't use system call. In fact not exactly, you can perform system call, but, because you don't know which package/binaries are installed, you can't rely on this. It's serverless, you don't manage the underlying infrastructure and runtime environment.

Here you made the assumption that CURL is installed on the runtime image. Maybe yes, maybe not, maybe it was, maybe it will be remove in future!! You can't rely on that!!


If you want to manage you runtime environment, you can use Cloud Run. You will manage your runtime environment, and you can install what you want on it and then you are sure of what you can do.


Last remarks:

note: instead of performing a CURL, you can perform a http get request to the same URL to get the IP

Why do you want to know the outgoing IP? It's serverless, you also don't manage the network. You will reach the internet through a Google IPs. It can change everytime, and other cloud functions (or cloud run), from your projects or project from others (like me), are able to use the same IPs. It's Google IPs, not yours! If it's your requirement, let me know, there are solutions for that!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.