From 5160fac9b77a92ffddee3ef1ddb97466c39343f6 Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Wed, 8 Apr 2015 16:48:02 -0400 Subject: [PATCH] Add availability zone support to Subnets created via CloudFormation --- moto/ec2/models.py | 5 +++- .../test_cloudformation_stack_integration.py | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 3549c29fe..cdb891487 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1707,10 +1707,13 @@ class Subnet(TaggedEC2Resource): properties = cloudformation_json['Properties'] vpc_id = properties['VpcId'] + cidr_block = properties['CidrBlock'] + availability_zone = properties.get('AvailabilityZone') ec2_backend = ec2_backends[region_name] subnet = ec2_backend.create_subnet( vpc_id=vpc_id, - cidr_block=properties['CidrBlock'] + cidr_block=cidr_block, + availability_zone=availability_zone, ) return subnet diff --git a/tests/test_cloudformation/test_cloudformation_stack_integration.py b/tests/test_cloudformation/test_cloudformation_stack_integration.py index 170a5226c..7db6c22e4 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_integration.py +++ b/tests/test_cloudformation/test_cloudformation_stack_integration.py @@ -1189,3 +1189,32 @@ def test_security_group_ingress_separate_from_security_group_by_id_using_vpc(): security_group1.rules[0].ip_protocol.should.equal('tcp') security_group1.rules[0].from_port.should.equal('80') security_group1.rules[0].to_port.should.equal('8080') + + +@mock_cloudformation +@mock_ec2 +def test_subnets_should_be_created_with_availability_zone(): + vpc_conn = boto.vpc.connect_to_region('us-west-1') + vpc = vpc_conn.create_vpc("10.0.0.0/16") + + subnet_template = { + "AWSTemplateFormatVersion" : "2010-09-09", + "Resources" : { + "testSubnet" : { + "Type" : "AWS::EC2::Subnet", + "Properties" : { + "VpcId" : vpc.id, + "CidrBlock" : "10.0.0.0/24", + "AvailabilityZone" : "us-west-1b", + } + } + } + } + cf_conn = boto.cloudformation.connect_to_region("us-west-1") + template_json = json.dumps(subnet_template) + cf_conn.create_stack( + "test_stack", + template_body=template_json, + ) + subnet = vpc_conn.get_all_subnets(filters={'cidrBlock': '10.0.0.0/24'})[0] + subnet.availability_zone.should.equal('us-west-1b')