Moto可以使用SecurityGroup模拟EC2

发布于 2025-02-12 14:20:26 字数 2312 浏览 1 评论 0原文

我最近问了这个问题:

moto单元ec2-'非型“对象”没有属性“ ID”

我现在至少发现了为什么发生错误的原因。

我调用ec2.run_instances()带有多个参数,例如:

ec2.run_instances(
        ImageId=IMAGE_ID,
        MinCount=count,
        MaxCount=count,
        InstanceType=INSTANCE_TYPE,
        KeyName=KEY_NAME,
        SecurityGroupIds=[SECURITY_GROUP_ID]
    )

尝试模拟此问题时,我会发现一个错误,因为它找不到指定的SecurityGroupID。

当我评论SecurityGrouoids之类的时:

...
        InstanceType=INSTANCE_TYPE,
        KeyName=KEY_NAME,
        #SecurityGroupIds=[SECURITY_GROUP_ID]
    )
...

moto测试正常运行。

如何将SecurityGroup添加到实例中?我试图使用该解决方案 这个问题为我。

有人知道如何模拟 /添加SecurityGroup中的EC2模拟吗?

预先感谢

编辑:

测试本身看起来像这样:

 @mock_ec2
    def test_create_instances_with_decorator(self):
        instance_count = 1
        image_id = 'ami-0df635fd7dea02388'
        client = boto3.client('ec2', region_name='eu-central-1')
        client.create_security_group(Description="my-test-group",GroupName="launch-wizard-1")
        mngr = aws_manager()
        mngr.launchInstances(2)
        instances = client.describe_instances()['Reservations'][0]['Instances']
        print(instances)
        assert len(instances) == 2
        assert instances[0]['ImageId'] == image_id

要测试的代码看起来像这样:

SECURITY_GROUP_ID = "sg-0dc0a634bc153adc8"
REGION_NAME = "eu-central-1"
INSTANCE_TYPE = "t2.micro"
class aws_manager:

    def __init__(self) -> None:
        self.ec2 = boto3.client('ec2', region_name=REGION_NAME)
        self.running_instances = []

    def launchInstances(self, count: int):

            instances = self.ec2.run_instances(
                ImageId=IMAGE_ID,
                MinCount=count,
                MaxCount=count,
                InstanceType=INSTANCE_TYPE,
                KeyName=KEY_NAME,
                SecurityGroupIds=[SECURITY_GROUP_ID]
            )["Instances"][0]



I recently asked this question:

moto unit test EC2 - 'NoneType' object has no attribute 'id'

I now have at least found out about why this error occurs.

I call ec2.run_instances() with multiple parameters, like this:

ec2.run_instances(
        ImageId=IMAGE_ID,
        MinCount=count,
        MaxCount=count,
        InstanceType=INSTANCE_TYPE,
        KeyName=KEY_NAME,
        SecurityGroupIds=[SECURITY_GROUP_ID]
    )

When trying to mock this like shown in my other question, I get an error because it can't find the specified securityGroupId.

When I comment out the SecurityGrouoIds like so:

...
        InstanceType=INSTANCE_TYPE,
        KeyName=KEY_NAME,
        #SecurityGroupIds=[SECURITY_GROUP_ID]
    )
...

the moto test runs fine.

How can I add the securityGroup to the instance? I have tried to use the solution from
this question, but it didn't work out for me.

Does anyone have an Idea about how to mock / add the securityGroup to the ec2 mock?

Thanks in advance

Edit:

The test itself looks like this:

 @mock_ec2
    def test_create_instances_with_decorator(self):
        instance_count = 1
        image_id = 'ami-0df635fd7dea02388'
        client = boto3.client('ec2', region_name='eu-central-1')
        client.create_security_group(Description="my-test-group",GroupName="launch-wizard-1")
        mngr = aws_manager()
        mngr.launchInstances(2)
        instances = client.describe_instances()['Reservations'][0]['Instances']
        print(instances)
        assert len(instances) == 2
        assert instances[0]['ImageId'] == image_id

And the code to be tested looks like this:

SECURITY_GROUP_ID = "sg-0dc0a634bc153adc8"
REGION_NAME = "eu-central-1"
INSTANCE_TYPE = "t2.micro"
class aws_manager:

    def __init__(self) -> None:
        self.ec2 = boto3.client('ec2', region_name=REGION_NAME)
        self.running_instances = []

    def launchInstances(self, count: int):

            instances = self.ec2.run_instances(
                ImageId=IMAGE_ID,
                MinCount=count,
                MaxCount=count,
                InstanceType=INSTANCE_TYPE,
                KeyName=KEY_NAME,
                SecurityGroupIds=[SECURITY_GROUP_ID]
            )["Instances"][0]



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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文