3

I've created a few instances in google compute engine by using the web interface. I want to start doing it at the command line instead so that I can create more and automate the process. The web interface tells me what command I could use when creating an instance. I request a permanent public IP address, and I can see the command which works, but how do I know what addresses are available to request? For instance, let's stay I created serverA with the command "gcloud compute ... instances create "serverA" .. --address 1.2.3.4 ..." I reused an address I had been previously assigned, but I think I got lucky in that it worked, and I can't guess what other IP addresses to use when I want to create more instances on the command line. Is there a way to query for available addresses? Thank you.

Tom

Tom Reingold
  • 31
  • 1
  • 2
  • reply to: So how can I programmatically create VMs with permanent IP addresses? Just be sure, not to run out of Static IP addresses quota. You can always ask for more. – nelasx Apr 26 '16 at 13:34

2 Answers2

7

UPDATE

A second approach to do this is by using the deployment manager. You can have a configuration file where you create the IP resource. In the same file you specify to deploy the VM with that IP assigned.

i.e. running

gcloud deployment-manager deployments create testdeploy --config=myconf.yaml

where myconf.yaml file content is

resources:
- name: test-rabbitmq-ip
  type: compute.v1.address
  properties:
    region: us-central1
- type: compute.v1.instance
  name: vm-my-first-deployment1
  properties:
    zone: us-central1-f
    machineType: https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/machineTypes/f1-micro
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20160923
    networkInterfaces:
    - network: https://www.googleapis.com/compute/v1/projects/myproject/global/networks/default
      # Access Config required to give the instance a public IP address
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
        natIP: $(ref.test-rabbitmq-ip.address)
Carlos
  • 1,395
  • 9
  • 15
1

You can reserve static IPs and use the gcloud command to create instances with the --address flag pointing to the static IP reserved, as there is no way to query for available addresses. Keep in mind that reserved static IP that is not attached to any instance will occur charges.

UPDATE: 1. You need to reserve a static IP by running the following command:

gcloud compute --project "YOUR_PROJECT_ID" addresses create "STATIC_IP_NAME" --region "YOUR_REGION"
  1. Run the following command to create a new instance with the static IP that you just reserved:

gcloud compute --project "YOUR_PROJECT_ID" instances create "INSTANCE_NAME" --zone "YOUR_ZONE" --machine-type "n1-standard-1" --network "YOUR_NETWORK_NAME" --address STATIC_IP_NAME --can-ip-forward --maintenance-policy "MIGRATE" --scopes "https://www.googleapis.com/auth/cloud-platform" --image "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20151104" --boot-disk-size "10" --boot-disk-type "pd-standard" --boot-disk-device-name "DISK_NAME"

in this case I chose debian-8 image and an n1-standard-1 machine type which of course you can change depending on your needs.

George
  • 639
  • 3
  • 6
  • Thank you, George. So how can I programmatically create VMs with permanent IP addresses? I'm OK with charges. I just want to know what addresses to put in my script. Thanks again. – Tom Reingold Dec 11 '15 at 19:04
  • @TomReingold I updated my answer – George Dec 11 '15 at 19:30
  • @TomReingold, After you reserved and named the static IP addresses, you can use `gcloud compute addresses list | grep -m 1 RESERVED | awk '{ print $1 }'` command to get the first reserved IP address name. Populate a variable with that value and use it with --address flag to create your VM instance – Kamran Dec 12 '15 at 19:25