-
Notifications
You must be signed in to change notification settings - Fork 4
/
security.tf
135 lines (106 loc) · 3.22 KB
/
security.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// ==========================
// == SECURITY GROUP RULES ==
// ==========================
// == HTTP ==
resource "aws_security_group_rule" "allow_http_inbound" {
type = "ingress"
from_port = var.http_port_from
to_port = var.http_port_to
protocol = "tcp"
cidr_blocks = [var.whitelist_ip]
security_group_id = aws_security_group.nomadstack.id
}
// == RPC ==
resource "aws_security_group_rule" "allow_rpc_inbound" {
type = "ingress"
from_port = var.rpc_port
to_port = var.rpc_port
protocol = "tcp"
cidr_blocks = [var.whitelist_ip]
security_group_id = aws_security_group.nomadstack.id
}
// == TCP ==
resource "aws_security_group_rule" "allow_serf_tcp_inbound" {
type = "ingress"
from_port = var.serf_port
to_port = var.serf_port
protocol = "tcp"
cidr_blocks = [var.whitelist_ip]
security_group_id = aws_security_group.nomadstack.id
}
// == UDP ==
resource "aws_security_group_rule" "allow_serf_udp_inbound" {
type = "ingress"
from_port = var.serf_port
to_port = var.serf_port
protocol = "udp"
cidr_blocks = [var.whitelist_ip]
security_group_id = aws_security_group.nomadstack.id
}
// == SSH ==
resource "aws_security_group_rule" "allow_ssh_inbound" {
type = "ingress"
from_port = var.ssh_port
to_port = var.ssh_port
protocol = "tcp"
cidr_blocks = [var.whitelist_ip]
security_group_id = aws_security_group.nomadstack.id
}
// == OUTBOUND ==
resource "aws_security_group_rule" "allow_all_outbound" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.nomadstack.id
}
// =====================
// == SECURITY GROUPS ==
// =====================
resource "aws_security_group" "nomadstack" {
name_prefix = var.cluster_name
description = "Security group for the ${var.cluster_name} launch configuration"
// if this is empty, does it set it up on the parent vpc
vpc_id = aws_vpc.nomadstack.id
}
// =================
// == PERMISSIONS ==
// =================
// Allow nomad auto-join
data "aws_iam_policy_document" "describe-instances" {
statement {
effect = "Allow"
actions = ["ec2:DescribeInstances"]
resources = ["*"]
}
}
data "aws_iam_policy_document" "assume-role" {
statement {
sid = ""
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "auto-join" {
name = "auto-join-${var.region}-${var.cluster_name}"
description = "Allows Nomad nodes to describe instances for joining."
policy = data.aws_iam_policy_document.describe-instances.json
}
resource "aws_iam_role" "auto-join" {
name = "auto-join-${var.region}-${var.cluster_name}"
assume_role_policy = data.aws_iam_policy_document.assume-role.json
}
resource "aws_iam_policy_attachment" "auto-join" {
name = "auto-join-${var.region}-${var.cluster_name}"
roles = [aws_iam_role.auto-join.name]
policy_arn = aws_iam_policy.auto-join.arn
}
resource "aws_iam_instance_profile" "auto-join" {
name = "auto-join-${var.region}-${var.cluster_name}"
role = aws_iam_role.auto-join.name
}