使用 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?
发布评论
评论(1)
调用
descrip_images
确实给出了所有可用imageId的列表。以下测试可针对Moto的当前开发分支:
预加载图像来自此文件:
如果您想替换自己的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:
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.