使用moto如何創建 EC2 實體,因為沒有可用于啟動實體的 AMI。這個 repo 似乎是“預加載” AMI,稍后在測驗中使用,但我不確定這些是如何創建的 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
但是,我嘗試呼叫.describe_images(Owners=["amazon"]),在 run_instances 呼叫中使用時回傳的所有 AMI 都會出現以下錯誤。
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
此問題還與未解決的問題有關如何使用 moto 創建具有特定影像 ID 的 ami?
uj5u.com熱心網友回復:
呼叫describe_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)
預加載的影像來自這個檔案: https ://github.com/spulec/moto/blob/master/moto/ec2/resources/amis.json
如果您想替換自己的 AMI,可以使用以下環境變數:
MOTO_AMIS_PATH=/full/path/to/amis.json
此 JSON 檔案必須與上面鏈接的格式相同。請注意,必須在初始化 Moto 之前設定環境變數 - 這些 AMI 會在您呼叫時加載from moto import mock_ec2,因此必須在匯入之前設定環境變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477579.html
標籤:亚马逊网络服务 单元测试 亚马逊-ec2 博托3 摩托
