Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit dd5c419

Browse files
committed
Split out Firewall Rules into a separate module.
This was necessary in order to support the Nomad and Consul servers being colocated on the same cluster.
1 parent 1edb07a commit dd5c419

File tree

7 files changed

+269
-70
lines changed

7 files changed

+269
-70
lines changed

main.tf

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ module "nomad_and_consul_servers" {
5454
allowed_inbound_tags_dns = ["${var.nomad_client_cluster_name}"]
5555
}
5656

57+
# Enable Firewall Rules to open up Nomad-specific ports
58+
module "nomad_firewall_rules" {
59+
source = "modules/nomad-firewall-rules"
60+
61+
gcp_zone = "${var.gcp_zone}"
62+
cluster_name = "${var.nomad_consul_server_cluster_name}"
63+
cluster_tag_name = "${var.nomad_consul_server_cluster_name}"
64+
65+
http_port = 4646
66+
rpc_port = 4647
67+
serf_port = 4648
68+
69+
allowed_inbound_cidr_blocks_http = ["0.0.0.0/0"]
70+
}
71+
5772
# Render the Startup Script that will run on each Nomad Instance on boot. This script will configure and start Nomad.
5873
data "template_file" "startup_script_nomad_consul_server" {
5974
template = "${file("${path.module}/examples/root-example/startup-script-nomad-consul-server.sh")}"

modules/nomad-cluster/main.tf

+8-64
Original file line numberDiff line numberDiff line change
@@ -137,74 +137,18 @@ resource "google_compute_instance_template" "nomad_private" {
137137

138138
# ---------------------------------------------------------------------------------------------------------------------
139139
# CREATE FIREWALL RULES
140-
# - These Firewall Rules may be redundant depending on the settings of your VPC Network, but if your Network is locked
141-
# down, these Rules will open up the appropriate ports.
142-
# - Note that public access to your Nomad cluster will only be permitted if var.assign_public_ip_addresses is true.
143-
# - Each Firewall Rule is only created if at least one source tag or source CIDR block for that Firewall Rule is specified.
144140
# ---------------------------------------------------------------------------------------------------------------------
145141

146-
# Specify which traffic is allowed into the Nomad cluster for inbound HTTP requests
147-
resource "google_compute_firewall" "allow_inbound_http" {
148-
count = "${length(var.allowed_inbound_cidr_blocks_http) + length(var.allowed_inbound_tags_http) > 0 ? 1 : 0}"
142+
module "firewall_rules" {
143+
source = "../nomad-firewall-rules"
149144

150-
name = "${var.cluster_name}-rule-external-http-access"
151-
network = "${var.network_name}"
145+
gcp_zone = "${var.gcp_zone}"
146+
cluster_name = "${var.cluster_name}"
147+
cluster_tag_name = "${var.cluster_tag_name}"
152148

153-
allow {
154-
protocol = "tcp"
155-
ports = [
156-
"${var.http_port}",
157-
]
158-
}
159-
160-
source_ranges = "${var.allowed_inbound_cidr_blocks_http}"
161-
source_tags = "${var.allowed_inbound_tags_http}"
162-
target_tags = ["${var.cluster_tag_name}"]
163-
}
164-
165-
# Specify which traffic is allowed into the Nomad cluster for inbound RPC requests
166-
resource "google_compute_firewall" "allow_inbound_rpc" {
167-
count = "${length(var.allowed_inbound_cidr_blocks_rpc) + length(var.allowed_inbound_tags_rpc) > 0 ? 1 : 0}"
168-
169-
name = "${var.cluster_name}-rule-external-rpc-access"
170-
network = "${var.network_name}"
171-
172-
allow {
173-
protocol = "tcp"
174-
ports = [
175-
"${var.rpc_port}",
176-
]
177-
}
178-
179-
source_ranges = "${var.allowed_inbound_cidr_blocks_rpc}"
180-
source_tags = "${var.allowed_inbound_tags_rpc}"
181-
target_tags = ["${var.cluster_tag_name}"]
182-
}
183-
184-
# Specify which traffic is allowed into the Nomad cluster for inbound serf requests
185-
resource "google_compute_firewall" "allow_inbound_serf" {
186-
count = "${length(var.allowed_inbound_cidr_blocks_serf) + length(var.allowed_inbound_tags_serf) > 0 ? 1 : 0}"
187-
188-
name = "${var.cluster_name}-rule-external-serf-access"
189-
network = "${var.network_name}"
190-
191-
allow {
192-
protocol = "tcp"
193-
ports = [
194-
"${var.serf_port}",
195-
]
196-
}
197-
198-
allow {
199-
protocol = "udp"
200-
ports = [
201-
"${var.serf_port}",
202-
]
203-
}
204-
205-
source_ranges = "${var.allowed_inbound_cidr_blocks_serf}"
206-
source_tags = "${var.allowed_inbound_tags_serf}"
207-
target_tags = ["${var.cluster_tag_name}"]
149+
http_port = 4646
150+
rpc_port = 4647
151+
serf_port = 4648
208152
}
209153

210154
# ---------------------------------------------------------------------------------------------------------------------

modules/nomad-cluster/outputs.tf

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
output "cluster_name" {
2+
value = "${var.cluster_name}"
3+
}
4+
15
output "cluster_tag_name" {
26
value = "${var.cluster_name}"
37
}
@@ -15,25 +19,25 @@ output "instance_template_url" {
1519
}
1620

1721
output "firewall_rule_allow_inbound_http_url" {
18-
value = "${google_compute_firewall.allow_inbound_http.self_link}"
22+
value = "${module.firewall_rules.firewall_rule_allow_inbound_http_url}"
1923
}
2024

2125
output "firewall_rule_allow_inbound_http_id" {
22-
value = "${google_compute_firewall.allow_inbound_http.id}"
26+
value = "${module.firewall_rules.firewall_rule_allow_inbound_http_id}"
2327
}
2428

2529
output "firewall_rule_allow_inbound_rpc_url" {
26-
value = "${google_compute_firewall.allow_inbound_rpc.self_link}"
30+
value = "${module.firewall_rules.firewall_rule_allow_inbound_rpc_url}"
2731
}
2832

2933
output "firewall_rule_allow_inbound_rpc_id" {
30-
value = "${google_compute_firewall.allow_inbound_rpc.id}"
34+
value = "${module.firewall_rules.firewall_rule_allow_inbound_rpc_id}"
3135
}
3236

3337
output "firewall_rule_allow_inbound_serf_url" {
34-
value = "${google_compute_firewall.allow_inbound_serf.self_link}"
38+
value = "${module.firewall_rules.firewall_rule_allow_inbound_serf_url}"
3539
}
3640

3741
output "firewall_rule_allow_inbound_serf_id" {
38-
value = "${google_compute_firewall.allow_inbound_serf.id}"
42+
value = "${module.firewall_rules.firewall_rule_allow_inbound_serf_id}"
3943
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Nomad Firewall Rules Module
2+
3+
This folder contains a [Terraform](https://www.terraform.io/) module that defines the Firewall Rules used by a
4+
[Nomad](https://www.nomadproject.io/) cluster to control the traffic that is allowed to go in and out of the cluster.
5+
6+
Normally, you'd get these rules by default if you're using the [nomad-cluster module](
7+
https://github.com/hashicorp/terraform-google-nomad/tree/master/examples/nomad-cluster), but if
8+
you're running Nomad on top of a different cluster, then you can use this module to add the necessary Firewall Rules
9+
rules that cluster needs. For example, imagine you were using the [consul-cluster
10+
module](https://github.com/hashicorp/terraform-google-consul/tree/master/modules/consul-cluster) to run a cluster of
11+
servers that have both Nomad and Consul on each node:
12+
13+
```hcl
14+
module "consul_servers" {
15+
source = "git::git@github.com:hashicorp/terraform-google-consul.git//modules/consul-cluster?ref=v0.0.1"
16+
17+
# This Image has both Nomad and Consul installed
18+
source_image = "nomad-consul-xyz123"
19+
}
20+
```
21+
22+
The `consul-cluster` module will provide the Firewall Rules for Consul, but not for Nomad. To ensure those
23+
servers have the necessary ports open for using Nomad, you can use this module as follows:
24+
25+
26+
```hcl
27+
module "security_group_rules" {
28+
source = "git::git@github.com:hashicorp/terraform-google-nomad.git//modules/nomad-firewall-rules?ref=v0.0.1"
29+
30+
cluster_name = "${module.consul_servers.cluster_name}"
31+
cluster_tag_name = "${module.consul_servers.cluster_tag_name}"
32+
33+
# ... (other params omitted) ...
34+
}
35+
```
36+
37+
Note the following parameters:
38+
39+
* `source`: Use this parameter to specify the URL of this module. The double slash (`//`) is intentional
40+
and required. Terraform uses it to specify subfolders within a Git repo (see [module
41+
sources](https://www.terraform.io/docs/modules/sources.html)). The `ref` parameter specifies a specific Git tag in
42+
this repo. That way, instead of using the latest version of this module from the `master` branch, which
43+
will change every time you run Terraform, you're using a fixed version of the repo.
44+
45+
* `cluster_name`: Use this parameter to specify the name of the cluster for which these Firewall Rules will apply; this
46+
allows us to name these resources in an intuitive way.
47+
48+
* `cluster_tag_name`: Use this parameter to indicate the cluster to which these Firewall Rules should apply.
49+
50+
You can find the other parameters in [variables.tf](variables.tf).
51+
52+
Check out the [nomad-consul-colocated-cluster example](
53+
https://github.com/hashicorp/terraform-google-nomad/tree/master/examples/root-example) for working sample code.

modules/nomad-firewall-rules/main.tf

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# THESE TEMPLATES REQUIRE TERRAFORM VERSION 0.10.3 AND ABOVE
3+
# This way we can take advantage of Terraform GCP functionality as a separate provider via
4+
# https://github.com/terraform-providers/terraform-provider-google
5+
# ---------------------------------------------------------------------------------------------------------------------
6+
7+
terraform {
8+
required_version = ">= 0.10.3"
9+
}
10+
11+
# ---------------------------------------------------------------------------------------------------------------------
12+
# CREATE FIREWALL RULES
13+
# - These Firewall Rules may be redundant depending on the settings of your VPC Network, but if your Network is locked
14+
# down, these Rules will open up the appropriate ports.
15+
# - Note that public access to your Nomad cluster will only be permitted if var.assign_public_ip_addresses is true.
16+
# - Each Firewall Rule is only created if at least one source tag or source CIDR block for that Firewall Rule is specified.
17+
# ---------------------------------------------------------------------------------------------------------------------
18+
19+
# Specify which traffic is allowed into the Nomad cluster for inbound HTTP requests
20+
resource "google_compute_firewall" "allow_inbound_http" {
21+
count = "${length(var.allowed_inbound_cidr_blocks_http) + length(var.allowed_inbound_tags_http) > 0 ? 1 : 0}"
22+
23+
name = "${var.cluster_name}-rule-external-http-access"
24+
network = "${var.network_name}"
25+
26+
allow {
27+
protocol = "tcp"
28+
ports = [
29+
"${var.http_port}",
30+
]
31+
}
32+
33+
source_ranges = "${var.allowed_inbound_cidr_blocks_http}"
34+
source_tags = "${var.allowed_inbound_tags_http}"
35+
target_tags = ["${var.cluster_tag_name}"]
36+
}
37+
38+
# Specify which traffic is allowed into the Nomad cluster for inbound RPC requests
39+
resource "google_compute_firewall" "allow_inbound_rpc" {
40+
count = "${length(var.allowed_inbound_cidr_blocks_rpc) + length(var.allowed_inbound_tags_rpc) > 0 ? 1 : 0}"
41+
42+
name = "${var.cluster_name}-rule-external-rpc-access"
43+
network = "${var.network_name}"
44+
45+
allow {
46+
protocol = "tcp"
47+
ports = [
48+
"${var.rpc_port}",
49+
]
50+
}
51+
52+
source_ranges = "${var.allowed_inbound_cidr_blocks_rpc}"
53+
source_tags = "${var.allowed_inbound_tags_rpc}"
54+
target_tags = ["${var.cluster_tag_name}"]
55+
}
56+
57+
# Specify which traffic is allowed into the Nomad cluster for inbound serf requests
58+
resource "google_compute_firewall" "allow_inbound_serf" {
59+
count = "${length(var.allowed_inbound_cidr_blocks_serf) + length(var.allowed_inbound_tags_serf) > 0 ? 1 : 0}"
60+
61+
name = "${var.cluster_name}-rule-external-serf-access"
62+
network = "${var.network_name}"
63+
64+
allow {
65+
protocol = "tcp"
66+
ports = [
67+
"${var.serf_port}",
68+
]
69+
}
70+
71+
allow {
72+
protocol = "udp"
73+
ports = [
74+
"${var.serf_port}",
75+
]
76+
}
77+
78+
source_ranges = "${var.allowed_inbound_cidr_blocks_serf}"
79+
source_tags = "${var.allowed_inbound_tags_serf}"
80+
target_tags = ["${var.cluster_tag_name}"]
81+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
output "firewall_rule_allow_inbound_http_url" {
2+
value = "${google_compute_firewall.allow_inbound_http.self_link}"
3+
}
4+
5+
output "firewall_rule_allow_inbound_http_id" {
6+
value = "${google_compute_firewall.allow_inbound_http.id}"
7+
}
8+
9+
output "firewall_rule_allow_inbound_rpc_url" {
10+
value = "${google_compute_firewall.allow_inbound_rpc.self_link}"
11+
}
12+
13+
output "firewall_rule_allow_inbound_rpc_id" {
14+
value = "${google_compute_firewall.allow_inbound_rpc.id}"
15+
}
16+
17+
output "firewall_rule_allow_inbound_serf_url" {
18+
value = "${google_compute_firewall.allow_inbound_serf.self_link}"
19+
}
20+
21+
output "firewall_rule_allow_inbound_serf_id" {
22+
value = "${google_compute_firewall.allow_inbound_serf.id}"
23+
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# ---------------------------------------------------------------------------------------------------------------------
2+
# REQUIRED PARAMETERS
3+
# You must provide a value for each of these parameters.
4+
# ---------------------------------------------------------------------------------------------------------------------
5+
6+
variable "gcp_zone" {
7+
description = "All GCP resources will be launched in this Zone."
8+
}
9+
10+
variable "cluster_name" {
11+
description = "The name of the Nomad cluster (e.g. nomad-stage). This variable is used to namespace all resources created by this module."
12+
}
13+
14+
variable "cluster_tag_name" {
15+
description = "The tag name the Compute Instances will look for to automatically discover each other and form a cluster. TIP: If running more than one Nomad cluster, each cluster should have its own unique tag name."
16+
}
17+
18+
# ---------------------------------------------------------------------------------------------------------------------
19+
# OPTIONAL PARAMETERS
20+
# These parameters have reasonable defaults.
21+
# ---------------------------------------------------------------------------------------------------------------------
22+
23+
variable "network_name" {
24+
description = "The name of the VPC Network where all resources should be created."
25+
default = "default"
26+
}
27+
28+
# Firewall Ports
29+
30+
variable "http_port" {
31+
description = "The port used by Nomad to handle incoming HTPT (API) requests."
32+
default = 4646
33+
}
34+
35+
variable "rpc_port" {
36+
description = "The port used by Nomad to handle incoming RPC requests."
37+
default = 4647
38+
}
39+
40+
variable "serf_port" {
41+
description = "The port used by Nomad to handle incoming serf requests."
42+
default = 4648
43+
}
44+
45+
variable "allowed_inbound_cidr_blocks_http" {
46+
description = "A list of CIDR-formatted IP address ranges from which the Compute Instances will allow connections to Nomad on the port specified by var.http_port."
47+
type = "list"
48+
default = []
49+
}
50+
51+
variable "allowed_inbound_tags_http" {
52+
description = "A list of tags from which the Compute Instances will allow connections to Nomad on the port specified by var.http_port."
53+
type = "list"
54+
default = []
55+
}
56+
57+
variable "allowed_inbound_cidr_blocks_rpc" {
58+
description = "A list of CIDR-formatted IP address ranges from which the Compute Instances will allow connections to Nomad on the port specified by var.rpc_port."
59+
type = "list"
60+
default = []
61+
}
62+
63+
variable "allowed_inbound_tags_rpc" {
64+
description = "A list of tags from which the Compute Instances will allow connections to Nomad on the port specified by var.rpc_port."
65+
type = "list"
66+
default = []
67+
}
68+
69+
variable "allowed_inbound_cidr_blocks_serf" {
70+
description = "A list of CIDR-formatted IP address ranges from which the Compute Instances will allow connections to Nomad on the port specified by var.serf_port."
71+
type = "list"
72+
default = []
73+
}
74+
75+
variable "allowed_inbound_tags_serf" {
76+
description = "A list of tags from which the Compute Instances will allow connections to Nomad on the port specified by var.serf_port."
77+
type = "list"
78+
default = []
79+
}

0 commit comments

Comments
 (0)