-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.tf
204 lines (175 loc) · 5.88 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
locals {
// Because of the issue https://github.com/hashicorp/terraform/issues/12570, the consumers cannot use a dynamic list for count
// and therefore are force to implicitly assume that the list is of aws_lb_target_group_arns_length - 1, in case there is no api_external
target_group_arns_length = var.publish_strategy == "External" ? var.target_group_arns_length : var.target_group_arns_length - 1
description = "Created By OpenShift Installer"
}
data "aws_partition" "current" {}
data "aws_ebs_default_kms_key" "current" {}
resource "aws_iam_instance_profile" "master" {
name = "${var.cluster_id}-master-profile"
role = var.iam_role_name != "" ? var.iam_role_name : aws_iam_role.master_role[0].name
tags = merge(
{
"Name" = "${var.cluster_id}-master-profile"
},
var.tags,
)
}
resource "aws_iam_role" "master_role" {
count = var.iam_role_name == "" ? 1 : 0
name = "${var.cluster_id}-master-role"
path = "/"
description = local.description
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.${data.aws_partition.current.dns_suffix}"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = merge(
{
"Name" = "${var.cluster_id}-master-role"
},
var.tags,
)
}
resource "aws_iam_role_policy" "master_policy" {
// List curated from https://github.com/kubernetes/cloud-provider-aws#readme, minus entries specific to EKS
// integrations.
// This list should not be updated any further without an operator owning migrating changes here for existing
// clusters.
// Please see: docs/dev/aws/iam_permissions.md
count = var.iam_role_name == "" ? 1 : 0
name = "${var.cluster_id}-master-policy"
role = aws_iam_role.master_role[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CreateSecurityGroup",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:DeleteSecurityGroup",
"ec2:DeleteVolume",
"ec2:Describe*",
"ec2:DetachVolume",
"ec2:ModifyInstanceAttribute",
"ec2:ModifyVolume",
"ec2:RevokeSecurityGroupIngress",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:AttachLoadBalancerToSubnets",
"elasticloadbalancing:ApplySecurityGroupsToLoadBalancer",
"elasticloadbalancing:CreateListener",
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:CreateLoadBalancerPolicy",
"elasticloadbalancing:CreateLoadBalancerListeners",
"elasticloadbalancing:CreateTargetGroup",
"elasticloadbalancing:ConfigureHealthCheck",
"elasticloadbalancing:DeleteListener",
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:DeleteLoadBalancerListeners",
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:Describe*",
"elasticloadbalancing:DetachLoadBalancerFromSubnets",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:ModifyLoadBalancerAttributes",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:ModifyTargetGroupAttributes",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:SetLoadBalancerPoliciesForBackendServer",
"elasticloadbalancing:SetLoadBalancerPoliciesOfListener",
"kms:DescribeKey"
],
"Resource": "*",
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_network_interface" "master" {
count = var.instance_count
description = local.description
subnet_id = var.az_to_subnet_id[var.availability_zones[count.index]]
security_groups = var.master_sg_ids
tags = merge(
{
"Name" = "${var.cluster_id}-master-${count.index}"
},
var.tags,
)
}
resource "aws_instance" "master" {
count = var.instance_count
ami = var.ec2_ami
iam_instance_profile = aws_iam_instance_profile.master.name
instance_type = var.instance_type
user_data = var.user_data_ign
network_interface {
network_interface_id = aws_network_interface.master[count.index].id
device_index = 0
}
dynamic "instance_market_options" {
for_each = var.use_spot_instance ? [1] : []
content {
market_type = "spot"
spot_options {
spot_instance_type = "one-time"
}
}
}
lifecycle {
# Ignore changes in the AMI which force recreation of the resource. This
# avoids accidental deletion of nodes whenever a new CoreOS Release comes
# out.
ignore_changes = [ami]
}
metadata_options {
http_endpoint = "enabled"
http_tokens = var.instance_metadata_authentication
}
tags = merge(
{
"Name" = "${var.cluster_id}-master-${count.index}"
},
var.tags,
)
root_block_device {
volume_type = var.root_volume_type
volume_size = var.root_volume_size
iops = var.root_volume_iops
encrypted = var.root_volume_encrypted
kms_key_id = var.root_volume_kms_key_id == "" ? data.aws_ebs_default_kms_key.current.key_arn : var.root_volume_kms_key_id
}
volume_tags = merge(
{
"Name" = "${var.cluster_id}-master-${count.index}-vol"
},
var.tags,
)
depends_on = [
aws_network_interface.master
]
}
resource "aws_lb_target_group_attachment" "master" {
count = var.instance_count * local.target_group_arns_length
target_group_arn = var.target_group_arns[count.index % local.target_group_arns_length]
target_id = aws_instance.master[floor(count.index / local.target_group_arns_length)].private_ip
}