Moto如何创建EC2实例-NVALIDAMIID.NOTFOUND

发布于 2025-01-29 03:12:08 字数 1172 浏览 4 评论 0 原文

使用 moto 如何创建一个EC2实例。 此存储库似乎是“预载” AMI,后来在测试中使用,但我不确定如何创建这些AMI

我已经尝试调用 .describe_images(anders = [“ Amazon”]) ,但是,所有AMIS在run_instances呼叫给以下错误。

from moto import mock_ec2


@mock_ec2
def Test_create_ec2():
    boto3.client('ec2').run_instances(ImageId="ami-1234abcd", MinCount=1, MaxCount=1)

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

这个问题还与未解决的问题有关如何创建AMI使用Moto的特定图像ID?

Using moto how can you create an EC2 instance since theres no AMIs available to launch an instance with.
This repo seems to be "pre-loading" AMIs that are later used within tests but im not sure how these are being created
https://github.com/spulec/moto/blob/master/tests/test_ec2/test_instances.py#L25
https://github.com/spulec/moto/blob/master/moto/ec2/resources/amis.json

I've tried calling .describe_images(Owners=["amazon"]) however, all the AMIs returned when used in the run_instances call give the below error.

from moto import mock_ec2


@mock_ec2
def Test_create_ec2():
    boto3.client('ec2').run_instances(ImageId="ami-1234abcd", MinCount=1, MaxCount=1)

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

This issue also relates to the unresolved question How to create ami with specific image-id using moto?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2025-02-05 03:12:08

调用 descrip_images 确实给出了所有可用imageId的列表。
以下测试可针对Moto的当前开发分支:

@mock_ec2
def test_all_images():
    client = boto3.client("ec2", region_name="us-east-1")
    images = client.describe_images(Owners=["amazon"])["Images"]
    for image in images:
        client.run_instances(ImageId=image["ImageId"], MinCount=1, MaxCount=1)

预加载图像来自此文件:

如果您想替换自己的AMI,则可以使用以下环境变量:
moto_amis_path =/fult/path/to/amis.json

此JSON文件必须与上面链接的格式相同。
请注意,必须在初始化Moto之前设置环境变量 - 从Moto import Mock_ec2 调用的那一刻,这些AMI就会加载,因此必须在此次导入之前设置环境变量。

The call to describe_images does give a list of all available ImageId's.
The following test works against the current development branch of Moto:

@mock_ec2
def test_all_images():
    client = boto3.client("ec2", region_name="us-east-1")
    images = client.describe_images(Owners=["amazon"])["Images"]
    for image in images:
        client.run_instances(ImageId=image["ImageId"], MinCount=1, MaxCount=1)

The preloaded images come from this file:
https://github.com/spulec/moto/blob/master/moto/ec2/resources/amis.json

If you want to substitute your own AMI's, you can use the following environment variable:
MOTO_AMIS_PATH=/full/path/to/amis.json

This JSON-file has to be in the same format as the one linked above.
Note that the environment variable has to be set before Moto is initialized - these AMI's are loaded the moment you call from moto import mock_ec2, so the environment variable has to be set before that import takes place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文