Allow usage of predefined Internet Gateway

Some organisations already have predefined Internet Gateways. Allow
to use them instead of creating new one.

Feature: Issue 13151
Change-Id: Ib822d977808db3c766c159d06c85bb4b2d50335a
diff --git a/Configuration.md b/Configuration.md
index 28efac2..4b12526 100644
--- a/Configuration.md
+++ b/Configuration.md
@@ -39,6 +39,8 @@
 * `JGIT_CACHE_SIZE`: Maximum number of bytes to load and cache in memory from pack files.
   See [Gerrit documentation](https://gerrit-review.googlesource.com/Documentation/config-gerrit.html#core.packedGitLimit)
   for more details. `12g` by default.
+* `INTERNET_GATEWAY_ID`: Optional. Id of the existing Internet Gateway.
+  If not set, create a new Internet Gateway
 
 #### LDAP
 
diff --git a/common-templates/cf-gerrit-network-stack.yml b/common-templates/cf-gerrit-network-stack.yml
index 4ea867b..2812ca1 100644
--- a/common-templates/cf-gerrit-network-stack.yml
+++ b/common-templates/cf-gerrit-network-stack.yml
@@ -1,6 +1,12 @@
 AWSTemplateFormatVersion: '2010-09-09'
 Description: Deploy a service into an ECS cluster behind a public load balancer.
 
+Parameters:
+  InternetGatewayIdProp:
+    Type: String
+    Default: ""
+    Description: Internet Gateway id. If empty Internet Gateway will be created
+
 Mappings:
   # Hard values for the subnet masks. These masks define
   # the range of internal IP addresses that can be assigned.
@@ -13,6 +19,9 @@
       CIDR: '10.0.0.0/16'
     PublicOne:
       CIDR: '10.0.0.0/24'
+
+Conditions:
+  CreateInternetGateway: !Equals [!Ref InternetGatewayIdProp, ""]
 Resources:
   VPC:
     Type: AWS::EC2::VPC
@@ -37,12 +46,13 @@
   # in the public subnets have public IP addresses and the routing table
   # sends network traffic via the internet gateway.
   InternetGateway:
+    Condition: CreateInternetGateway
     Type: AWS::EC2::InternetGateway
   GatewayAttachement:
     Type: AWS::EC2::VPCGatewayAttachment
     Properties:
       VpcId: !Ref 'VPC'
-      InternetGatewayId: !Ref 'InternetGateway'
+      InternetGatewayId: !If [CreateInternetGateway, !Ref 'InternetGateway', !Ref 'InternetGatewayIdProp' ]
   PublicRouteTable:
     Type: AWS::EC2::RouteTable
     Properties:
@@ -53,7 +63,7 @@
     Properties:
       RouteTableId: !Ref 'PublicRouteTable'
       DestinationCidrBlock: '0.0.0.0/0'
-      GatewayId: !Ref 'InternetGateway'
+      GatewayId: !If [CreateInternetGateway, !Ref 'InternetGateway', !Ref 'InternetGatewayIdProp' ]
   PublicSubnetOneRouteTableAssociation:
     Type: AWS::EC2::SubnetRouteTableAssociation
     Properties:
diff --git a/dual-master/Makefile b/dual-master/Makefile
index eda7031..2657b13 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -38,6 +38,7 @@
 		--parameters \
 		ParameterKey=DesiredCapacity,ParameterValue=$(CLUSTER_DESIRED_CAPACITY) \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
+		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
 		$(OPTIONAL_PARAMS)
 
 service-master-1:
diff --git a/dual-master/README.md b/dual-master/README.md
index f07b040..52bf9ff 100644
--- a/dual-master/README.md
+++ b/dual-master/README.md
@@ -148,4 +148,4 @@
 
 ### Docker
 
-Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
\ No newline at end of file
+Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
diff --git a/dual-master/cf-cluster.yml b/dual-master/cf-cluster.yml
index f5f07db..7de16c1 100644
--- a/dual-master/cf-cluster.yml
+++ b/dual-master/cf-cluster.yml
@@ -25,6 +25,10 @@
       c3.2xlarge, c3.4xlarge, c3.8xlarge, r3.large, r3.xlarge, r3.2xlarge, r3.4xlarge,
       r3.8xlarge, i2.xlarge, i2.2xlarge, i2.4xlarge, i2.8xlarge]
     ConstraintDescription: Please choose a valid instance type.
+  InternetGatewayIdProp:
+    Type: String
+    Default: ""
+    Description: Internet Gateway id. If empty Internet Gateway will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -250,6 +254,8 @@
     Properties:
       TemplateURL: https://aws-gerrit-cf-templates.s3.amazonaws.com/cf-gerrit-network-stack.yml
       TimeoutInMinutes: '25'
+      Parameters:
+        InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
 
 Outputs:
   ClusterName:
diff --git a/master-slave/Makefile b/master-slave/Makefile
index 2413277..956282a 100644
--- a/master-slave/Makefile
+++ b/master-slave/Makefile
@@ -35,6 +35,7 @@
 		--parameters \
 		ParameterKey=DesiredCapacity,ParameterValue=$(CLUSTER_DESIRED_CAPACITY) \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
+		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
 		$(OPTIONAL_PARAMS)
 
 service-master:
diff --git a/master-slave/README.md b/master-slave/README.md
index 0f44bfa..43d9fd6 100644
--- a/master-slave/README.md
+++ b/master-slave/README.md
@@ -153,4 +153,4 @@
 
 ### Docker
 
-Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
\ No newline at end of file
+Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
diff --git a/master-slave/cf-cluster.yml b/master-slave/cf-cluster.yml
index b5767a0..1b23226 100644
--- a/master-slave/cf-cluster.yml
+++ b/master-slave/cf-cluster.yml
@@ -25,6 +25,10 @@
       c3.2xlarge, c3.4xlarge, c3.8xlarge, r3.large, r3.xlarge, r3.2xlarge, r3.4xlarge,
       r3.8xlarge, i2.xlarge, i2.2xlarge, i2.4xlarge, i2.8xlarge]
     ConstraintDescription: Please choose a valid instance type.
+  InternetGatewayIdProp:
+    Type: String
+    Default: ""
+    Description: Internet Gateway id. If empty Internet Gateway will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -194,6 +198,8 @@
     Properties:
       TemplateURL: https://aws-gerrit-cf-templates.s3.amazonaws.com/cf-gerrit-network-stack.yml
       TimeoutInMinutes: '25'
+      Parameters:
+        InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
 
 Outputs:
   ClusterName:
diff --git a/single-master/Makefile b/single-master/Makefile
index c4519a1..ef36e8c 100644
--- a/single-master/Makefile
+++ b/single-master/Makefile
@@ -31,6 +31,7 @@
 		--region $(AWS_REGION) \
 		--parameters \
 		ParameterKey=ECSKeyName,ParameterValue=$(CLUSTER_KEYS) \
+		ParameterKey=InternetGatewayIdProp,ParameterValue=$(INTERNET_GATEWAY_ID) \
 		$(OPTIONAL_PARAMS)
 
 service:
diff --git a/single-master/README.md b/single-master/README.md
index 3cd843e..eb470db 100644
--- a/single-master/README.md
+++ b/single-master/README.md
@@ -116,4 +116,4 @@
 
 ### Docker
 
-Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
\ No newline at end of file
+Refer to the [Docker](../Docker.md) section for information on how to setup docker or how to publish images
diff --git a/single-master/cf-cluster.yml b/single-master/cf-cluster.yml
index c020a07..203346b 100644
--- a/single-master/cf-cluster.yml
+++ b/single-master/cf-cluster.yml
@@ -25,6 +25,10 @@
       c3.2xlarge, c3.4xlarge, c3.8xlarge, r3.large, r3.xlarge, r3.2xlarge, r3.4xlarge,
       r3.8xlarge, i2.xlarge, i2.2xlarge, i2.4xlarge, i2.8xlarge]
     ConstraintDescription: Please choose a valid instance type.
+  InternetGatewayIdProp:
+    Type: String
+    Default: ""
+    Description: Internet Gateway id. If empty Internet Gateway will be created
   ECSKeyName:
     Type: String
     Default: gerrit-cluster-keys
@@ -170,6 +174,8 @@
     Properties:
       TemplateURL: https://aws-gerrit-cf-templates.s3.amazonaws.com/cf-gerrit-network-stack.yml
       TimeoutInMinutes: '25'
+      Parameters:
+        InternetGatewayIdProp: !Ref 'InternetGatewayIdProp'
 
 Outputs:
   ClusterName: