首页 > 解决方案 > Boto3/AWS:创建实例时“ImageId 不存在”

问题描述

我已经制作了 AMI 的副本,我正在尝试使用以下代码运行它:

import boto3

instance_id=("i-0e2bbdf4fc43bf6db")

client = boto3.client("ec2",region_name="us-west-2")
ec2 = boto3.resource("ec2")

ec2.create_instances(ImageId="ami-9d623ee5",MinCount=1,MaxCount=1)

返回一个 ClientError:

ClientError: An error occurred (InvalidAMIID.NotFound) when calling the RunInstances operation: The image id '[ami-9d623ee5]' does not exist

可能是什么问题?谢谢!

标签: pythonamazon-web-servicesboto3amazon-ami

解决方案


假设AMI在同一个区域...

您的代码错误:更改为类似于以下内容:

import boto3

instance_id=("i-0e2bbdf4fc43bf6db")

session = boto3.Session("ec2",region_name="us-west-2")
ec2 = session.resource("ec2")

OR

ec2 = boto3.resource('ec2', region_name='us-west-2')

ec2.create_instances(ImageId="ami-9d623ee5",MinCount=1,MaxCount=1)

推荐阅读