{"record":{"id":"f4dcd687a42b926a","repo":"kubernetes/kops","slug":"found-instance-but-instanceid-was-nil","errorCode":null,"errorMessage":"found instance, but InstanceId was nil","messagePattern":"found instance, but InstanceId was nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/cloudup/awstasks/instance.go","lineNumber":110,"sourceCode":"\tif response != nil {\n\t\tfor _, reservation := range response.Reservations {\n\t\t\tinstances = append(instances, reservation.Instances...)\n\t\t}\n\t}\n\n\tif len(instances) == 0 {\n\t\treturn nil, nil\n\t}\n\n\tif len(instances) != 1 {\n\t\treturn nil, fmt.Errorf(\"found multiple Instances with name: %s\", *e.Name)\n\t}\n\n\tklog.V(2).Info(\"found existing instance\")\n\ti := instances[0]\n\n\tif i.InstanceId == nil {\n\t\treturn nil, fmt.Errorf(\"found instance, but InstanceId was nil\")\n\t}\n\n\tactual := &Instance{\n\t\tID:               i.InstanceId,\n\t\tPrivateIPAddress: i.PrivateIpAddress,\n\t\tInstanceType:     i.InstanceType,\n\t\tImageID:          i.ImageId,\n\t\tName:             findNameTag(i.Tags),\n\t}\n\n\t// Fetch instance UserData\n\t{\n\t\trequest := &ec2.DescribeInstanceAttributeInput{}\n\t\trequest.InstanceId = i.InstanceId\n\t\trequest.Attribute = ec2types.InstanceAttributeNameUserData\n\t\tresponse, err := cloud.EC2().DescribeInstanceAttribute(ctx, request)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error querying EC2 for user metadata for instance %q: %v\", *i.InstanceId, err)","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/awstasks/instance.go#L92-L128","documentation":"Find() found exactly one matching instance, but its InstanceId field was nil, which should never happen for an instance returned by DescribeInstances. Rather than dereference a nil pointer, kOps returns this defensive error. It usually indicates an unexpected/SDK-level anomaly or a mocked response missing the ID.","triggerScenarios":"instances[0].InstanceId is nil when building the actual state — only realistically seen with stubbed/malformed AWS responses or severe SDK deserialization anomalies.","commonSituations":"Running against mocked/fake AWS clients in tests or custom tooling that omit InstanceId; corrupted API responses.","solutions":["Re-run the operation to rule out a transient bad response.","If using a fake/mock EC2 client, ensure DescribeInstances results always set InstanceId.","Update the AWS SDK / kops version if a deserialization bug is suspected; report to kops if reproducible against real AWS."],"exampleFix":"// mock fix\n// before\nec2types.Instance{State: ...} // InstanceId omitted\n// after\nec2types.Instance{InstanceId: aws.String(\"i-123\"), State: ...}","handlingStrategy":"type-guard","validationCode":"// caller-side check when inspecting instance state\nif i.InstanceId == nil { return errors.New(\"instance response missing InstanceId\") }","typeGuard":"func hasID(i ec2types.Instance) bool { return i.InstanceId != nil && aws.ToString(i.InstanceId) != \"\" }","tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"InstanceId was nil\") {\n  // treat as transient/mocked-data bug: retry once, then report\n  return retryOnceOrReport(err)\n}","preventionTips":["If using fake/mock EC2 clients (e.g. in tests or forks), always populate InstanceId.","Keep the AWS SDK and kops versions current to rule out deserialization bugs.","Report reproducible occurrences against real AWS to kops with the DescribeInstances response shape."],"tags":["aws","ec2","nil-pointer","defensive-check"],"backgroundTag":"unexpected-nil-field","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}