Home » Fix “Source url of disk is missing” when attaching disk to instance in GCP using Python

Fix “Source url of disk is missing” when attaching disk to instance in GCP using Python

  • by
  • 2 min read
Tags:

I would like to briefly elaborate on an error I ran into, which is due to limited documentation on the Google Cloud Platform API. With this article, I hope to save you the time and effort to find the solution.

I’ve been following an infrastructure-as-code approach to deploying GCP resources lately, and I ran into the following error when deploying a compute instance with a disk attached.

googleapiclient.errors.HttpError:<HttpError 400 when requesting https://compute.googleapis.com/compute/v1/projects/<PROJECT_ID>/zones/<ZONE>/instances?alt=json returned “Invalid value for field ‘resource.disks[1].source’: ”. Source url of disk is missing.”. Details: “[{‘message’: “Invalid value for field ‘resource.disks[1].source’: ”. Source url of disk is missing.”, ‘domain’: ‘global’, ‘reason’: ‘invalid’}]”>

I was simply following the Python samples as described on the GCP GitHub repository over here, in combination with the API documentation over here. The problem is that the examples don’t attach an extra disk on top of the boot disk.

Basically, what causes the error is that the source parameter of a disk within your instance config is missing or faulty.

instance_config = {
  'name': instance_name,
  'disks': [
    {
      'deviceName': <DEVICE_NAME>
      'boot': True,
      'initializeParams': {
        'sourceImage': <SOURCE_IMAGE>
      }
      ...
    },
    {
      'deviceName': <DEVICE_NAME>,
      'name': <NAME>
      # source parameter is missing
      ...
    }
  ]

By specifying the source URL of the disk you’re trying to attach, you will prevent the error. Just inject your variables in the following link, and use it as the value for your source parameter.

'source': f'https://www.googleapis.com/compute/v1/projects/{<PROJECT_ID>}/zones/{<ZONE>}/disks/{<DISK_NAME>}'

Makes perfect sense, but you have to know it. Here’s the full solution:

instance_config = {
  'name': instance_name,
  'disks': [
    {
      'deviceName': <DEVICE_NAME>
      'boot': True,
      'initializeParams': {
        'sourceImage': <SOURCE_IMAGE>
      }
      ...
    },
    {
      'deviceName': <DEVICE_NAME>,
      'name': <NAME>,
      'source': f'https://www.googleapis.com/compute/v1/projects/{<PROJECT_ID>}/zones/{<ZONE>}/disks/{<DISK_NAME>}'
      ...
    }
  ]

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

Leave a Reply

Your email address will not be published. Required fields are marked *