Setting external IP address in google cloud to a static address i have reserved

495 Views Asked by At

I recently started working with google cloud and built a node app to automatically deploy instances based on an image I have setup.

The script works well, the only issue I am running into is that I cannot seem to assign my static IP address I reserved using my config, I am new at node and google cloud, and am not sure what I am doing wrong.

any help or guidance at all would be much appreciated

my config looks like:

const config = {
        machineType: 'e2-micro',
        http:true,
        https:true,
        networkInterfaces: [
            {
            accessConfigs: [
                {
                  type: 'ONE_TO_ONE_NAT',
                  name: 'External NAT',
                  natIP: '0.0.0.0',// this is an ip address that is passed into this as a variable in that format(it is not a scope issue)
                  setPublicPtr: false,
                  networkTier: 'PREMIUM', //ip address is reserved using this network tier as well
                }
              ],
              
 
      kind: 'compute#networkInterface'

      }],
        
        
        disks: [
          {
            boot: true,
            initializeParams: {
              sourceImage:
                'https://www.googleapis.com/compute/v1/projects' +
                '/myproject/global/images/devimage'
            }
          }
        ],
        metadata: {
           
            items: [
              {
                key: 'startup-script',
                value: 'sudo node index.js'
              }
            ],
            kind: 'compute#metadata'
          },
        
        };
1

There are 1 best solutions below

0
On

Please note that as of now, there are particularly no means to assign a static IP to an existing Compute Engine instance via the Google API.

In order to do so for an existing Compute Engine instance you would need to utilize either the Google Cloud console or the Cloud SDK.

However to assign a static IP to a new Compute Engine instance, you indeed would be able to do so via the API through a request similar to the following:

{
"name": "INSTANCE_NAME",
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "networkInterfaces": [{
    "accessConfigs": [{
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT",
      "natIP": "IP_ADDRESS"
     }],
    "network": "global/networks/default"
  }],
  "disks": [{
     "autoDelete": "true",
     "boot": "true",
     "type": "PERSISTENT",
     "initializeParams": {
        "sourceImage": "projects/debian-cloud/global/images/v20150818"
     }
   }]
 }

In this case it is possible that the name of the instance was not provided, when creating a new instance and assigning the IP address.