Repository: getmoto/moto

Issue:
ELBv2: Can't create listener
Part of my code:
```
@mock_ec2
@mock_elbv2
class TestElasticLoadBalancingV2(unittest.TestCase):

    def setUp(self):
        self.tags = [
            {
                'Key': 'test_key',
                'Value': 'test_value'
            }
        ]
        self.region = 'us-east-1'
        self.credentials = {
            "aws_access_key_id": "",
            "aws_secret_access_key": ""
        }

        self.client = boto3.client(
            'elbv2',
            self.region,
            **self.credentials
        )


    def create_resources(self):
        self.elb_name = 'test_elb'
        subnets_ids = self.subnets_ids()
        lb_arn = self.create_load_balancer(self.elb_name, subnets_ids)
        tg_arn = self.create_target_group()
        self.create_listener(lb_arn, tg_arn)

    def create_load_balancer(self, elb_name, subnets):
        response = self.client.create_load_balancer(
            Name=elb_name,
            Tags=self.tags,
            Subnets=subnets
        )
        return response['LoadBalancers'][0]['LoadBalancerArn']

    def create_target_group(self):
        response = self.client.create_target_group(
            Name='test-target-group',
            Protocol='HTTPS',
            Tags=self.tags,
            Port = 6081,
            TargetType='instance'
        )
        return response['TargetGroups'][0]['TargetGroupArn']

    def create_listener(self, lb_arn, tg_arn):
        self.client.create_listener(
            LoadBalancerArn=lb_arn,
            DefaultActions=[
                {
                    'Type': 'fixed-response',
                    'FixedResponseConfig': {
                        'StatusCode': '200',
                        'ContentType': 'application/json'
                    }
                }
            ],
            Tags=self.tags
        )

    def subnets_ids(self):
        ec2_client = boto3.client(
            'ec2',
            self.region,
            **self.credentials
        )
        response = ec2_client.describe_subnets()
        subnets_ids = [subnet['SubnetId'] for subnet in response['Subnets']]
        return subnets_ids


    def test_elbv2(self):
        self.create_resources()
```
Traceback:
```
 File "/home/geek/try_moto/tests.py", line 646, in create_listener
    self.client.create_listener(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/client.py", line 691, in _make_api_call
    http, parsed_response = self._make_request(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/client.py", line 711, in _make_request
    return self._endpoint.make_request(operation_model, request_dict)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/endpoint.py", line 102, in make_request
    return self._send_request(request_dict, operation_model)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/endpoint.py", line 134, in _send_request
    success_response, exception = self._get_response(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/endpoint.py", line 166, in _get_response
    success_response, exception = self._do_get_response(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/endpoint.py", line 217, in _do_get_response
    parsed_response = parser.parse(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 245, in parse
    parsed = self._do_parse(response, shape)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 523, in _do_parse
    return self._parse_body_as_xml(response, shape, inject_metadata=True)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 535, in _parse_body_as_xml
    parsed = self._parse_shape(shape, start)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 312, in _parse_shape
    return handler(shape, node)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 388, in _handle_structure
    parsed[member_name] = self._parse_shape(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 312, in _parse_shape
    return handler(shape, node)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 370, in _handle_list
    return super(BaseXMLResponseParser, self)._handle_list(shape, node)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 320, in _handle_list
    parsed.append(self._parse_shape(member_shape, item))
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 312, in _parse_shape
    return handler(shape, node)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 388, in _handle_structure
    parsed[member_name] = self._parse_shape(
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 312, in _parse_shape
    return handler(shape, node)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 174, in _get_text_content
    return func(self, shape, text)
  File "/home/geek/.local/share/virtualenvs/aws_automation-MKiSmjjO/lib/python3.8/site-packages/botocore/parsers.py", line 486, in _handle_integer
    return int(text)
ValueError: invalid literal for int() with base 10: 'None'

```


Discussion:

Resolving patch:
diff --git a/moto/elbv2/responses.py b/moto/elbv2/responses.py
--- a/moto/elbv2/responses.py
+++ b/moto/elbv2/responses.py
@@ -872,7 +872,9 @@ def _add_tags(self, resource):
           {% endfor %}
         </Certificates>
         {% endif %}
+        {% if listener.port %}
         <Port>{{ listener.port }}</Port>
+        {% endif %}
         <SslPolicy>{{ listener.ssl_policy }}</SslPolicy>
         <ListenerArn>{{ listener.arn }}</ListenerArn>
         <DefaultActions>


Test patch:
diff --git a/tests/test_elbv2/test_elbv2.py b/tests/test_elbv2/test_elbv2.py
--- a/tests/test_elbv2/test_elbv2.py
+++ b/tests/test_elbv2/test_elbv2.py
@@ -16,30 +16,10 @@
 @mock_elbv2
 @mock_ec2
 def test_create_load_balancer():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     lb = response.get("LoadBalancers")[0]
-
+    VPCID = vpc.id
     lb.get("DNSName").should.equal("my-lb-1.us-east-1.elb.amazonaws.com")
     lb.get("LoadBalancerArn").should.equal(
         "arn:aws:elasticloadbalancing:us-east-1:1:loadbalancer/my-lb/50dc6c495c0c9188"
@@ -60,9 +40,7 @@ def test_create_load_balancer():
     tags.should.equal({"key_name": "a_value"})
 
 
-@mock_elbv2
-@mock_ec2
-def test_describe_load_balancers():
+def create_load_balancer():
     conn = boto3.client("elbv2", region_name="us-east-1")
     ec2 = boto3.resource("ec2", region_name="us-east-1")
 
@@ -77,13 +55,20 @@ def test_describe_load_balancers():
         VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
     )
 
-    conn.create_load_balancer(
+    response = conn.create_load_balancer(
         Name="my-lb",
         Subnets=[subnet1.id, subnet2.id],
         SecurityGroups=[security_group.id],
         Scheme="internal",
         Tags=[{"Key": "key_name", "Value": "a_value"}],
     )
+    return response, vpc, security_group, subnet1, subnet2, conn
+
+
+@mock_elbv2
+@mock_ec2
+def test_describe_load_balancers():
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     response = conn.describe_load_balancers()
 
@@ -109,28 +94,7 @@ def test_describe_load_balancers():
 @mock_elbv2
 @mock_ec2
 def test_add_remove_tags():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1b"
-    )
-
-    conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     lbs = conn.describe_load_balancers()["LoadBalancers"]
     lbs.should.have.length_of(1)
@@ -252,29 +216,44 @@ def test_create_elb_in_multiple_region():
 
 @mock_elbv2
 @mock_ec2
-def test_create_target_group_and_listeners():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
+def test_create_listeners_without_port():
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
+    load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
+    response = conn.create_target_group(
+        Name="a-target",
+        Protocol="HTTP",
+        Port=8080,
+        VpcId=vpc.id,
+        HealthCheckProtocol="HTTP",
+        HealthCheckPort="8080",
+        HealthCheckPath="/",
+        HealthCheckIntervalSeconds=5,
+        HealthCheckTimeoutSeconds=5,
+        HealthyThresholdCount=5,
+        UnhealthyThresholdCount=2,
+        Matcher={"HttpCode": "200"},
     )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
+    target_group = response.get("TargetGroups")[0]
+    target_group_arn = target_group["TargetGroupArn"]
+    response = conn.create_listener(
+        LoadBalancerArn=load_balancer_arn,
+        Protocol="HTTP",
+        DefaultActions=[{"Type": "forward", "TargetGroupArn": target_group_arn}],
     )
 
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
+    listener = response.get("Listeners")[0]
+    listener.get("Port").should.equal(None)
+    listener.get("Protocol").should.equal("HTTP")
+    listener.get("DefaultActions").should.equal(
+        [{"TargetGroupArn": target_group_arn, "Type": "forward"}]
     )
 
+
+@mock_elbv2
+@mock_ec2
+def test_create_target_group_and_listeners():
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
+
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     # Can't create a target group with an invalid protocol
@@ -439,27 +418,7 @@ def test_create_target_group_and_listeners():
 @mock_elbv2
 @mock_ec2
 def test_create_target_group_without_non_required_parameters():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     # request without HealthCheckIntervalSeconds parameter
     # which is default to 30 seconds
@@ -574,27 +533,7 @@ def test_describe_paginated_balancers():
 @mock_elbv2
 @mock_ec2
 def test_delete_load_balancer():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     response.get("LoadBalancers").should.have.length_of(1)
     lb = response.get("LoadBalancers")[0]
@@ -842,27 +781,7 @@ def test_terminated_instance_target():
 @mock_ec2
 @mock_elbv2
 def test_target_group_attributes():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     response = conn.create_target_group(
         Name="a-target",
@@ -955,27 +874,7 @@ def test_create_target_group_invalid_protocol():
 @mock_elbv2
 @mock_ec2
 def test_create_rule_priority_in_use():
-    elbv2 = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = elbv2.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, elbv2 = create_load_balancer()
 
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
@@ -1002,27 +901,7 @@ def test_create_rule_priority_in_use():
 @mock_elbv2
 @mock_ec2
 def test_handle_listener_rules():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
@@ -1369,27 +1248,7 @@ def test_handle_listener_rules():
 @mock_elbv2
 @mock_ec2
 def test_describe_invalid_target_group():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
@@ -1416,27 +1275,7 @@ def test_describe_invalid_target_group():
 @mock_elbv2
 @mock_ec2
 def test_describe_target_groups_no_arguments():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
 
     response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
@@ -1483,27 +1322,7 @@ def test_describe_ssl_policies():
 @mock_elbv2
 @mock_ec2
 def test_set_ip_address_type():
-    client = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = client.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, client = create_load_balancer()
     arn = response["LoadBalancers"][0]["LoadBalancerArn"]
 
     # Internal LBs cant be dualstack yet
@@ -1617,27 +1436,7 @@ def test_set_subnets_errors():
 @mock_elbv2
 @mock_ec2
 def test_modify_load_balancer_attributes_idle_timeout():
-    client = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = client.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, client = create_load_balancer()
     arn = response["LoadBalancers"][0]["LoadBalancerArn"]
 
     client.modify_load_balancer_attributes(
@@ -1824,28 +1623,7 @@ def test_modify_listener_http_to_https():
 @mock_elbv2
 @mock_ec2
 def test_redirect_action_listener_rule():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.128/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
-
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     action = {
@@ -1903,27 +1681,7 @@ def test_redirect_action_listener_rule():
 @mock_elbv2
 @mock_ec2
 def test_cognito_action_listener_rule():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.128/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     action = {
@@ -1966,27 +1724,7 @@ def test_cognito_action_listener_rule():
 @mock_elbv2
 @mock_ec2
 def test_fixed_response_action_listener_rule():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.128/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     action = {
@@ -2027,27 +1765,7 @@ def test_fixed_response_action_listener_rule():
 @mock_elbv2
 @mock_ec2
 def test_fixed_response_action_listener_rule_validates_status_code():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.128/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     invalid_status_code_action = {
@@ -2075,27 +1793,7 @@ def test_fixed_response_action_listener_rule_validates_status_code():
 @mock_elbv2
 @mock_ec2
 def test_fixed_response_action_listener_rule_validates_content_type():
-    conn = boto3.client("elbv2", region_name="us-east-1")
-    ec2 = boto3.resource("ec2", region_name="us-east-1")
-
-    security_group = ec2.create_security_group(
-        GroupName="a-security-group", Description="First One"
-    )
-    vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
-    subnet1 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
-    )
-    subnet2 = ec2.create_subnet(
-        VpcId=vpc.id, CidrBlock="172.28.7.128/26", AvailabilityZone="us-east-1b"
-    )
-
-    response = conn.create_load_balancer(
-        Name="my-lb",
-        Subnets=[subnet1.id, subnet2.id],
-        SecurityGroups=[security_group.id],
-        Scheme="internal",
-        Tags=[{"Key": "key_name", "Value": "a_value"}],
-    )
+    response, vpc, security_group, subnet1, subnet2, conn = create_load_balancer()
     load_balancer_arn = response.get("LoadBalancers")[0].get("LoadBalancerArn")
 
     invalid_content_type_action = {