【问题标题】:GCP Cloud Function to create VM instance does not recognise my custom image用于创建 VM 实例的 GCP 云功能无法识别我的自定义映像
【发布时间】:2021-04-06 20:31:36
【问题描述】:

我正在尝试在 Python 中创建一个云函数,该函数使用我之前创建的自定义映像构建 VM 实例。

源图像出现在图像部分:

但是,当我运行 Cloud Function 时,它找不到我的镜像来构建实例并返回以下错误:

Details: "Invalid value for field 'resource.disks[0].initializeParams.sourceImage': 'projects/<<project id>>/global/images/pandora-pagespeed-image'. The referenced image resource cannot be found.">

此外,如果我在控制台中手动创建 VM 实例,图像也不会出现在此处:

更奇怪的是,正在出现的图像(pandora-image)是昨天删除的旧图像。

这里可能发生了什么?

我的云函数代码是:

import os
from googleapiclient import discovery
from google.oauth2 import service_account

scopes = ['https://www.googleapis.com/auth/cloud-platform']
sa_file = <<credentials file>>
zone = 'europe-west2-c'
project_id = <<project id>> # Project ID, not Project Name

credentials = service_account.Credentials.from_service_account_file(sa_file, scopes=scopes)

# Create the Cloud Compute Engine service object
service = discovery.build('compute', 'v1', credentials=credentials)

def create_instance(compute, project, zone, name):
    # Get the latest Debian Jessie image.
    image_response = (
        compute.images()
        .getFromFamily(project="debian-cloud", family="debian-9")
        .execute()
    )
    source_disk_image = image_response["selfLink"]

    # Configure the machine
    machine_type = "zones/%s/machineTypes/n1-standard-1" % zone
    config = {
        "name": name,
        "machineType": machine_type,
        # Specify the boot disk and the image to use as a source.
        "disks": [
            {
                "kind": "compute#attachedDisk",
                "type": "PERSISTENT",
                "boot": True,
                "mode": "READ_WRITE",
                "autoDelete": True,
                "deviceName": "instance-1",
                "initializeParams": {
                    "sourceImage": "projects/<<project id>>/global/images/pandora-pagespeed-image",
                    "diskType": "projects/<<project id>>/zones/us-central1-a/diskTypes/pd-standard",
                    "diskSizeGb": "10",
                },
                "diskEncryptionKey": {},
            }
        ],
        "metadata": {
            "kind": "compute#metadata",
    "items": [
      {
        "key": "startup-script",
        "value": "sudo apt-get -y install python3-pip\npip3 install -r /home/tommyp/pandora/requirements.txt\ncd /home/tommyp/pandora\npython3 /home/tommyp/pandora/main.py"
      }
    ]
        },
        "networkInterfaces": [
            {
                "network": "global/networks/default",
                "accessConfigs": [{"type": "ONE_TO_ONE_NAT", "name": "External NAT"}],
            }
        ],
        "tags": {"items": ["http-server", "https-server"]},
    }

    return compute.instances().insert(project=project, zone=zone, body=config).execute()

def run(data, context):
    create_instance(service, project_id, zone, "vm-instance")

【问题讨论】:

  • 你的第一张截图是图片还是快照截图?
  • 图片截图。

标签: python google-api google-cloud-functions google-compute-engine


【解决方案1】:

我现在意识到我做错了什么。

我在“machine images”部分而不是“图像”部分创建了我的图像。因此,为什么找不到我的图像...DOH!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2018-01-22
    • 2015-02-03
    • 1970-01-01
    • 2017-05-07
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多