@@ -30,6 +30,11 @@ resource "aws_iam_role_policy_attachment" "node-AmazonEC2ContainerRegistryReadOn
3030 role = aws_iam_role. node . name
3131}
3232
33+ resource "aws_iam_role_policy_attachment" "node_AmazonEKSWorkerNodeMinimalPolicy" {
34+ policy_arn = " arn:aws:iam::aws:policy/AmazonEKSWorkerNodeMinimalPolicy"
35+ role = aws_iam_role. node . name
36+ }
37+
3338resource "aws_iam_role_policy_attachment" "node_role_policies" {
3439 count = length (var. node_role_policies )
3540 policy_arn = var. node_role_policies [count . index ]
@@ -74,3 +79,207 @@ resource "aws_iam_instance_profile" "node" {
7479 role = aws_iam_role. node . name
7580 tags = local. tags
7681}
82+
83+ resource "aws_iam_role" "eks_auto" {
84+ count = var. eks_auto_mode_enabled ? 1 : 0
85+ name = " ${ var . environment_name } -eks-auto-mode-node"
86+
87+ assume_role_policy = jsonencode ({
88+ " Version" : " 2012-10-17" ,
89+ " Statement" : [
90+ {
91+ " Sid" : " EKSAutoNodeAssumeRole" ,
92+ " Effect" : " Allow" ,
93+ " Principal" : {
94+ " Service" : " ec2.amazonaws.com"
95+ },
96+ " Action" : [
97+ " sts:TagSession" ,
98+ " sts:AssumeRole"
99+ ]
100+ }
101+ ]
102+ })
103+
104+ tags = local. tags
105+ }
106+
107+ locals {
108+ iam_role_policy_prefix = " arn:${ local . partition } :iam::aws:policy"
109+ # EKS cluster with EKS auto mode enabled
110+ eks_auto_mode_iam_role_policies = { for k , v in {
111+ AmazonEKSClusterPolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSClusterPolicy"
112+ AmazonEKSComputePolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSComputePolicy"
113+ AmazonEKSBlockStoragePolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSBlockStoragePolicy"
114+ AmazonEKSLoadBalancingPolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSLoadBalancingPolicy"
115+ AmazonEKSNetworkingPolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSNetworkingPolicy"
116+ } : k => v if var . eks_auto_mode_enabled }
117+ }
118+
119+ resource "aws_iam_role_policy_attachment" "this" {
120+ for_each = { for k , v in {
121+ AmazonEKSWorkerNodeMinimalPolicy = " ${ local . iam_role_policy_prefix } /AmazonEKSWorkerNodeMinimalPolicy" ,
122+ AmazonEC2ContainerRegistryPullOnly = " ${ local . iam_role_policy_prefix } /AmazonEC2ContainerRegistryPullOnly" ,
123+ } : k => v if var . eks_auto_mode_enabled }
124+
125+ policy_arn = each. value
126+ role = aws_iam_role. eks_auto [0 ]. name
127+ }
128+
129+ resource "aws_iam_role_policy_attachment" "cluster" {
130+ for_each = { for k , v in merge (
131+ local. eks_auto_mode_iam_role_policies ,
132+ ) : k => v if var . eks_auto_mode_enabled }
133+
134+ policy_arn = each. value
135+ role = aws_iam_role. cluster . name
136+ }
137+
138+ data "aws_iam_policy_document" "custom" {
139+ count = var. eks_auto_mode_enabled ? 1 : 0
140+
141+ dynamic "statement" {
142+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
143+
144+ content {
145+ sid = " Compute"
146+ actions = [
147+ " ec2:CreateFleet" ,
148+ " ec2:RunInstances" ,
149+ " ec2:CreateLaunchTemplate" ,
150+ ]
151+ resources = [" *" ]
152+
153+ condition {
154+ test = " StringEquals"
155+ variable = " aws:RequestTag/eks:eks-cluster-name"
156+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
157+ }
158+
159+ condition {
160+ test = " StringLike"
161+ variable = " aws:RequestTag/eks:kubernetes-node-class-name"
162+ values = [" *" ]
163+ }
164+
165+ condition {
166+ test = " StringLike"
167+ variable = " aws:RequestTag/eks:kubernetes-node-pool-name"
168+ values = [" *" ]
169+ }
170+ }
171+ }
172+
173+ dynamic "statement" {
174+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
175+
176+ content {
177+ sid = " Storage"
178+ actions = [
179+ " ec2:CreateVolume" ,
180+ " ec2:CreateSnapshot" ,
181+ ]
182+ resources = [
183+ " arn:${ local . partition } :ec2:*:*:volume/*" ,
184+ " arn:${ local . partition } :ec2:*:*:snapshot/*" ,
185+ ]
186+
187+ condition {
188+ test = " StringEquals"
189+ variable = " aws:RequestTag/eks:eks-cluster-name"
190+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
191+ }
192+ }
193+ }
194+
195+ dynamic "statement" {
196+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
197+
198+ content {
199+ sid = " Networking"
200+ actions = [" ec2:CreateNetworkInterface" ]
201+ resources = [" *" ]
202+
203+ condition {
204+ test = " StringEquals"
205+ variable = " aws:RequestTag/eks:eks-cluster-name"
206+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
207+ }
208+
209+ condition {
210+ test = " StringEquals"
211+ variable = " aws:RequestTag/eks:kubernetes-cni-node-name"
212+ values = [" *" ]
213+ }
214+ }
215+ }
216+
217+ dynamic "statement" {
218+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
219+
220+ content {
221+ sid = " LoadBalancer"
222+ actions = [
223+ " elasticloadbalancing:CreateLoadBalancer" ,
224+ " elasticloadbalancing:CreateTargetGroup" ,
225+ " elasticloadbalancing:CreateListener" ,
226+ " elasticloadbalancing:CreateRule" ,
227+ " ec2:CreateSecurityGroup" ,
228+ ]
229+ resources = [" *" ]
230+
231+ condition {
232+ test = " StringEquals"
233+ variable = " aws:RequestTag/eks:eks-cluster-name"
234+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
235+ }
236+ }
237+ }
238+
239+ dynamic "statement" {
240+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
241+
242+ content {
243+ sid = " ShieldProtection"
244+ actions = [" shield:CreateProtection" ]
245+ resources = [" *" ]
246+
247+ condition {
248+ test = " StringEquals"
249+ variable = " aws:RequestTag/eks:eks-cluster-name"
250+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
251+ }
252+ }
253+ }
254+
255+ dynamic "statement" {
256+ for_each = var. eks_auto_mode_enabled ? [1 ] : []
257+
258+ content {
259+ sid = " ShieldTagResource"
260+ actions = [" shield:TagResource" ]
261+ resources = [" arn:${ local . partition } :shield::*:protection/*" ]
262+
263+ condition {
264+ test = " StringEquals"
265+ variable = " aws:RequestTag/eks:eks-cluster-name"
266+ values = [" $${aws:PrincipalTag/eks:eks-cluster-name}" ]
267+ }
268+ }
269+ }
270+ }
271+
272+ resource "aws_iam_policy" "custom" {
273+ count = var. eks_auto_mode_enabled ? 1 : 0
274+
275+ name = " ${ var . environment_name } -eks-auto-mode-cluster"
276+ policy = data. aws_iam_policy_document . custom [0 ]. json
277+
278+ }
279+
280+ resource "aws_iam_role_policy_attachment" "custom" {
281+ count = var. eks_auto_mode_enabled ? 1 : 0
282+
283+ policy_arn = aws_iam_policy. custom [0 ]. arn
284+ role = aws_iam_role. cluster . name
285+ }
0 commit comments