-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
96 lines (71 loc) · 2.37 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
#-----------------------
# EKS CLUSTER DEFINITION
#-----------------------
resource "aws_eks_cluster" "eksdemo" {
name = "${var.eks_cluster}"
role_arn = aws_iam_role.eksiamrole.arn
vpc_config {
subnet_ids = [aws_subnet.subnet-1.id, aws_subnet.subnet-2.id]
}
depends_on = [
aws_iam_role_policy_attachment.eksrole-AmazonEKSClusterPolicy,
aws_iam_role_policy_attachment.eksrole-AmazonEKSVPCResourceController,
]
}
output "endpoint" {
value = aws_eks_cluster.eksdemo.endpoint
}
# output "kubeconfig-certificate-authority-data" {
# value = aws_eks_cluster.eksdemo.certificate_authority[0].data
# }
#------------------------
# IAM role for EKS Cluster
#-------------------------
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["eks.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
resource "aws_iam_role" "eksiamrole" {
name = "eks-cluster-role"
assume_role_policy = data.aws_iam_policy_document.assume_role.json
}
resource "aws_iam_role_policy_attachment" "eksrole-AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.eksiamrole.name
}
resource "aws_iam_role_policy_attachment" "eksrole-AmazonEKSVPCResourceController" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSVPCResourceController"
role = aws_iam_role.eksiamrole.name
}
#--------------------------------------
# Enabling IAM role for service Account
#--------------------------------------
data "tls_certificate" "ekstls" {
url = aws_eks_cluster.eksdemo.identity[0].oidc[0].issuer
}
resource "aws_iam_openid_connect_provider" "eksopidc" {
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = data.tls_certificate.ekstls.certificates[*].sha1_fingerprint
url = data.tls_certificate.ekstls.url
}
data "aws_iam_policy_document" "eksdoc_assume_role_policy" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringEquals"
variable = "${replace(aws_iam_openid_connect_provider.eksopidc.url, "https://", "")}:sub"
values = ["system:serviceaccount:kube-system:aws-node"]
}
principals {
identifiers = [aws_iam_openid_connect_provider.eksopidc.arn]
type = "Federated"
}
}
}