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]