pizza
pizza

Reputation: 732

how to attach a static IP address programmatically (Python) in Google Cloud Platform

I am trying to programatically(Python) create a virtual machine with a static IP address that I have. My code is based on https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/compute/api/create_instance.py

I am specifically looking at line 74 to 79. Is there a way to attach my static IP address like by referencing, say, the name of the static IP address or something? Now I am just literally hardcoding my static IP address into accessConfigs on line 77. I feel like there should be a better/proper way to do it.

Again, I am not trying to create a VM with a default IP and replace it with a static IP. I am trying to create a VM with a static IP address.

Thank you!

Upvotes: 1

Views: 656

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75810

You have to pass the IP into the natIP field. You can:

  • hardcode this value. That is bad
  • Pass the IP value in param of your script
  • Perform a request to get the IP of an existing static IP, extract the IP value and pass it to the script

Upvotes: 3

Related Questions