Python 用于创建VM实例的GCP Cloud函数无法识别我的自定义映像
我正在尝试用Python创建一个云函数,该函数使用我以前创建的自定义映像构建一个VM实例 源图像显示在图像部分: 但是,当我运行云函数时,它无法找到我的映像来构建实例,并返回以下错误:Python 用于创建VM实例的GCP Cloud函数无法识别我的自定义映像,python,google-api,google-cloud-functions,google-compute-engine,Python,Google Api,Google Cloud Functions,Google Compute Engine,我正在尝试用Python创建一个云函数,该函数使用我以前创建的自定义映像构建一个VM实例 源图像显示在图像部分: 但是,当我运行云函数时,它无法找到我的映像来构建实例,并返回以下错误: Details: "Invalid value for field 'resource.disks[0].initializeParams.sourceImage': 'projects/<<project id>>/global/images/pandora-pagespee
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.">
Details:“resource.disks[0]字段的值无效。initializeParams.sourceImage':“projects//global/images/pandora pagespeed image”。找不到引用的图像资源。“>
此外,如果我在控制台中手动创建VM实例,图像也不会出现在这里:
更奇怪的是,出现的图像(潘多拉图像)是昨天删除的旧图像
这里可能发生了什么
我的云功能代码是:
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")
导入操作系统
从GoogleAppClient导入发现
从google.oauth2导入服务_帐户
作用域=['https://www.googleapis.com/auth/cloud-platform']
sa_文件=
区域=‘欧洲西部2-c’
项目id=#项目id,而不是项目名称
凭据=服务\帐户。凭据。来自\服务\帐户\文件(sa\文件,作用域=作用域)
#创建云计算引擎服务对象
service=discovery.build('compute','v1',credentials=credentials)
def create_实例(计算、项目、区域、名称):
#获取最新的Debian Jessie图像。
图像\u响应=(
compute.images()
.getFromFamily(project=“debian cloud”,family=“debian-9”)
.execute()
)
source\u disk\u image=image\u响应[“selfLink”]
#配置机器
machine_type=“zones/%s/machineTypes/n1-standard-1”%zone
配置={
“姓名”:姓名,
“机器类型”:机器类型,
#指定要用作源的启动磁盘和映像。
“磁盘”:[
{
“种类”:“计算#附加任务”,
“类型”:“持久性”,
“靴子”:没错,
“模式”:“读写”,
“自动删除”:True,
“deviceName”:“实例1”,
“初始化图”:{
“sourceImage”:“projects//global/images/pandora pagespeed image”,
“diskType”:“projects//zones/us-central1-a/diskTypes/pd standard”,
“diskSizeGb”:“10”,
},
“diskEncryptionKey”:{},
}
],
“元数据”:{
“种类”:“计算元数据”,
“项目”:[
{
“键”:“启动脚本”,
“值”:“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”
}
]
},
“网络接口”:[
{
“网络”:“全局/网络/默认值”,
“accessConfigs”:[{“类型”:“一对一NAT”,“名称”:“外部NAT”}],
}
],
“标记”:{“项”:[“http服务器”、“https服务器”]},
}
返回compute.instances().insert(project=project,zone=zone,body=config).execute()
def运行(数据、上下文):
创建\u实例(服务、项目\u id、区域,“vm实例”)
我现在意识到我做错了什么
我在“”部分创建了图像,而不是在“图像”部分。因此,为什么我的形象找不到…啊 您的第一个屏幕截图是图像还是快照屏幕截图?它是图像的屏幕截图。