首页 > 解决方案 > Moto 警告:使用 ec2_backend.describe_images() 为您的测试找到合适的图像

问题描述

我正在尝试测试一个使用 boto3 中的 create_stack() 上传云形成的函数。

对于测试,我使用的是框架 moto。

对于测试,我使用 pytest 夹具在 yaml 中创建了一个 template_data:

@pytest.fixture(scope='function')
def template_body_data():
    'The Cloud Formation template'
    template_data = {
        'Resources': {
            'MyInstance': {
                'Type': 'AWS::EC2::Instance',
                'Properties': {'ImageId': 'ami-a4c7edb2', 'InstanceType': 't2.micro'},
            }
        }
    }
    return template_data

我所有的测试都有效。

问题是我收到此警告:

/home/myprofile/.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/moto/ec2/models.py:517: PendingDeprecationWarning: Could not find AMI with image-id:ami-a4c7edb2, in the near future this will cause an error.
  Use ec2_backend.describe_images() to find suitable image for your test

我怎样才能解决这个问题?

标签: pythonpython-3.xpytestboto3moto

解决方案


使用下面第三步中的图像 ID,它将解决警告

client = boto3.client('ec2', aws_region)
image_response = client.describe_images()
image_id = image_response['Images'][0]['ImageId']

推荐阅读