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.
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"
}
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.