Skip to content

Commit

Permalink
Add availability zone support to Subnets created via CloudFormation
Browse files Browse the repository at this point in the history
  • Loading branch information
hltbra committed Apr 8, 2015
1 parent 65d51a5 commit 5160fac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion moto/ec2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,10 +1707,13 @@ def create_from_cloudformation_json(cls, resource_name, cloudformation_json, reg
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

Expand Down
29 changes: 29 additions & 0 deletions tests/test_cloudformation/test_cloudformation_stack_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 5160fac

Please sign in to comment.