2017-12-27 19:17:59 +00:00
|
|
|
import boto3
|
2022-01-18 15:18:57 +00:00
|
|
|
from moto import mock_ec2
|
2017-12-27 19:17:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock_ec2
|
2021-10-20 17:49:23 +00:00
|
|
|
def setup_networking(region_name="us-east-1"):
|
|
|
|
ec2 = boto3.resource("ec2", region_name=region_name)
|
2019-10-31 15:44:26 +00:00
|
|
|
vpc = ec2.create_vpc(CidrBlock="10.11.0.0/16")
|
2017-12-27 19:17:59 +00:00
|
|
|
subnet1 = ec2.create_subnet(
|
2021-10-20 17:49:23 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="10.11.1.0/24", AvailabilityZone=f"{region_name}a"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
2017-12-27 19:17:59 +00:00
|
|
|
subnet2 = ec2.create_subnet(
|
2021-10-20 17:49:23 +00:00
|
|
|
VpcId=vpc.id, CidrBlock="10.11.2.0/24", AvailabilityZone=f"{region_name}b"
|
2019-10-31 15:44:26 +00:00
|
|
|
)
|
|
|
|
return {"vpc": vpc.id, "subnet1": subnet1.id, "subnet2": subnet2.id}
|
|
|
|
|
2017-12-29 03:00:53 +00:00
|
|
|
|
2019-07-17 18:15:59 +00:00
|
|
|
@mock_ec2
|
|
|
|
def setup_instance_with_networking(image_id, instance_type):
|
|
|
|
mock_data = setup_networking()
|
2019-10-31 15:44:26 +00:00
|
|
|
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
2019-07-17 18:15:59 +00:00
|
|
|
instances = ec2.create_instances(
|
|
|
|
ImageId=image_id,
|
|
|
|
InstanceType=instance_type,
|
|
|
|
MaxCount=1,
|
|
|
|
MinCount=1,
|
2019-10-31 15:44:26 +00:00
|
|
|
SubnetId=mock_data["subnet1"],
|
2019-07-17 18:15:59 +00:00
|
|
|
)
|
2019-10-31 15:44:26 +00:00
|
|
|
mock_data["instance"] = instances[0].id
|
2019-07-17 18:15:59 +00:00
|
|
|
return mock_data
|