From 1546635fcd7f4b42a7f6375491111d8cb65de84a Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Wed, 11 Feb 2015 18:19:40 -0500 Subject: [PATCH] Attach internet gateway to VPC when it is created through CloudFormation --- moto/ec2/models.py | 4 +- .../test_cloudformation_stack_integration.py | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index b4d491264..7fb897445 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -2031,10 +2031,12 @@ class VPCGatewayAttachment(object): properties = cloudformation_json['Properties'] ec2_backend = ec2_backends[region_name] - return ec2_backend.create_vpc_gateway_attachment( + attachment = ec2_backend.create_vpc_gateway_attachment( gateway_id=properties['InternetGatewayId'], vpc_id=properties['VpcId'], ) + ec2_backend.attach_internet_gateway(properties['InternetGatewayId'], properties['VpcId']) + return attachment @property def physical_resource_id(self): diff --git a/tests/test_cloudformation/test_cloudformation_stack_integration.py b/tests/test_cloudformation/test_cloudformation_stack_integration.py index c1f356bca..031ed464a 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_integration.py +++ b/tests/test_cloudformation/test_cloudformation_stack_integration.py @@ -936,3 +936,51 @@ def test_sns_topic(): topic_name_output.value.should.equal("my_topics") topic_arn_output = [x for x in stack.outputs if x.key == 'topic_arn'][0] topic_arn_output.value.should.equal(topic_arn) + + + +@mock_cloudformation +def test_vpc_gateway_attachment_creation_should_attach_itself_to_vpc(): + template = { + "AWSTemplateFormatVersion": "2010-09-09", + "Resources": { + "internetgateway": { + "Type": "AWS::EC2::InternetGateway" + }, + "testvpc": { + "Type": "AWS::EC2::VPC", + "Properties": { + "CidrBlock": "10.0.0.0/16", + "EnableDnsHostnames": "true", + "EnableDnsSupport": "true", + "InstanceTenancy": "default" + }, + }, + "vpcgatewayattachment": { + "Type": "AWS::EC2::VPCGatewayAttachment", + "Properties": { + "InternetGatewayId": { + "Ref": "internetgateway" + }, + "VpcId": { + "Ref": "testvpc" + } + }, + }, + } + } + + template_json = json.dumps(template) + cf_conn = boto.cloudformation.connect_to_region("us-west-1") + cf_conn.create_stack( + "test_stack", + template_body=template_json, + ) + + vpc_conn = boto.vpc.connect_to_region("us-west-1") + vpc = vpc_conn.get_all_vpcs()[0] + igws = vpc_conn.get_all_internet_gateways( + filters={'attachment.vpc-id': vpc.id} + ) + + igws.should.have.length_of(1)