0

I want to create a Instance using a InstanceTemplate via the java google-api-client. After executing the operation the new instance is displayed, beein created, in the Compute Engine frontend of GCP. After 10-15s the instance disappears. GCP-Compute Engine VM-Instance Overview

Following the Reference Manual i cant get my head around why my code is not working.
https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert

Compute.Instances.Insert insert = compute
                    .instances()
                    .insert("{{project-id}}","europe-west1-c",instance)
                    .setSourceInstanceTemplate("/compute/v1/projects/{{project-id}}/global/instanceTemplates/instance-template-1")
                    .setZone("europe-west1-c")
                    .setProject("{{project-id}}");
            Operation op = insert.execute();

The instance Object looks like that:

Instance instance = new Instance();
instance.setName(instanceName);
instance.setMachineType("zones/europe-west1-c/machineTypes/g1-small");

Gradle Dependencies

compile 'com.google.api-client:google-api-client:1.31.2'  
compile group: 'com.google.apis', name: 'google-api-services-compute', version: 'v1-rev235-1.25.0'

Log from the GCP:

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 3,
      "message": "INVALID_PARAMETER"
    },
    "authenticationInfo": {
      "principalEmail": "compute-dev-me@{{project-id}}.iam.gserviceaccount.com"
    },
    "requestMetadata": {
      "callerIp": "12.34.56.78",
      "callerSuppliedUserAgent": "redacted/0.1 Google-API-Java-Client/1.31.2 Google-HTTP-Java-Client/1.37.0 (gzip),gzip(gfe)"
    },
    "serviceName": "compute.googleapis.com",
    "methodName": "v1.compute.instances.insert",
    "resourceName": "projects/{{project-id}}/zones/europe-west1-c/instances/test-mit-richtig",
    "request": {
      "@type": "type.googleapis.com/compute.instances.insert"
    }
  },
  "insertId": "-duwg4fde7a2",
  "resource": {
    "type": "gce_instance",
    "labels": {
      "instance_id": "redacted-number",
      "zone": "europe-west1-c",
      "project_id": "{{project-id}}"
    }
  },
  "timestamp": "2021-02-xxTxx:xx:xx.565227Z",
  "severity": "ERROR",
  "logName": "projects/{{project-id}}/logs/cloudaudit.googleapis.com%2Factivity",
  "operation": {
    "id": "operation-1613xxxx41-xxxxxxxx-xxxxxxxx-xxxxxxx",
    "producer": "compute.googleapis.com",
    "last": true
  },
  "receiveTimestamp": "2021-02-xxTxx:xx:xx.569578819Z"
}
Mawiguk0
  • 33
  • 5

1 Answers1

1

After reviewing the log provided you are getting an Invalid Parameter error code. In your parameters you set the instance name as follows:

instance.setName(instanceName);

GCE instances have a naming convention, where the name has to start with a lower case letter, followed by a string of characters or hyphens, and can't end with a hyphen. In your case you have an uppercase n in your code (instanceName). Setting it as follows should correct the issue:

instance.setName(instancename);

Another thing I noticed in your log is that the project id is showing up as {{project-id}}, if this hasn't been redacted by you (if it has it's good practice, but you should specify redacted parts of your code to help understand the log).

----- edit after comments -----

After checking the activity tab on Google Cloud Platform we saw it was caused by Service Account permissions.

In the following link you can find information on creating and enabling Service Accounts.

Dylan
  • 76
  • 8
  • Thank you Dylan. I was redacting the project id and some other parts in the log. I will do my best to improve log postings. My problem still occurs after checking the naming. The workaround you mentioned is not working because instanceName is a java String with the value: "test-withbeer". So the naming convention, which I don't use to know was already fulfilled. What does the error in the log message relate to? – Mawiguk0 Feb 12 '21 at 10:11
  • Another way to check what the parameter causing the GCE instance to fail other than the logs would be checking the Activity tab on the Google Cloud Platform console, or in the logs setting the query to: protoPayload.methodName= "v1.compute.instances.insert" – Dylan Feb 12 '21 at 10:19
  • " Instanz erstellen test-withbeer compute-dev-me@anon-anon-######.iam.gserviceaccount.com Start: 12.02.2021, 11:06:41 Ende: 12.02.2021, 11:07:48 SERVICE_ACCOUNT_ACCESS_DENIED " I think that will solve my problem! I basically never found a place where to look at what GCP is doing. Thanks Dylan! – Mawiguk0 Feb 12 '21 at 10:26
  • If it provides any useful information, please edit your question to include it, thanks. – Dylan Feb 12 '21 at 10:26
  • It seems to be caused by a permissions problem, I will edit my answer to provide steps necessary to create and enable service accounts. – Dylan Feb 12 '21 at 10:28
  • Thanks again Dylan, I lost track of your changes in your comment. Great Job! – Mawiguk0 Apr 18 '21 at 13:29