Skip to content

Commit 0659a8d

Browse files
feat: Allow using inline policy for Karpenter controller role to mitigate policy size LimitExceeded error (#3563)
* feat: Allow using inline policy for controller role (#3512) Signed-off-by: Alexis Sellier <3765063+alexissellier@users.noreply.github.com> * run linter Signed-off-by: Alexis Sellier <3765063+alexissellier@users.noreply.github.com> * Update modules/karpenter/main.tf Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com> * Update modules/karpenter/variables.tf Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com> * Update modules/karpenter/variables.tf Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com> * change variable name Signed-off-by: Alexis Sellier <3765063+alexissellier@users.noreply.github.com> * rename variable Signed-off-by: Alexis Sellier <3765063+alexissellier@users.noreply.github.com> --------- Signed-off-by: Alexis Sellier <3765063+alexissellier@users.noreply.github.com> Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com>
1 parent 0778bbb commit 0659a8d

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

modules/karpenter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ No modules.
110110
| [aws_iam_policy.controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
111111
| [aws_iam_role.controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
112112
| [aws_iam_role.node](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
113+
| [aws_iam_role_policy.controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
113114
| [aws_iam_role_policy_attachment.controller](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
114115
| [aws_iam_role_policy_attachment.controller_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
115116
| [aws_iam_role_policy_attachment.node](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
@@ -139,6 +140,7 @@ No modules.
139140
| <a name="input_create_instance_profile"></a> [create\_instance\_profile](#input\_create\_instance\_profile) | Whether to create an IAM instance profile | `bool` | `false` | no |
140141
| <a name="input_create_node_iam_role"></a> [create\_node\_iam\_role](#input\_create\_node\_iam\_role) | Determines whether an IAM role is created or to use an existing IAM role | `bool` | `true` | no |
141142
| <a name="input_create_pod_identity_association"></a> [create\_pod\_identity\_association](#input\_create\_pod\_identity\_association) | Determines whether to create pod identity association | `bool` | `true` | no |
143+
| <a name="input_enable_inline_policy"></a> [enable\_inline\_policy](#input\_enable\_inline\_policy) | Determines whether the controller policy is created as a standard IAM policy or inline IAM policy. This can be enabled when the error `LimitExceeded: Cannot exceed quota for PolicySize: 6144` is received since standard IAM policies have a limit of 6,144 characters versus an inline role policy's limit of 10,240 ([Reference](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html)) | `bool` | `false` | no |
142144
| <a name="input_enable_spot_termination"></a> [enable\_spot\_termination](#input\_enable\_spot\_termination) | Determines whether to enable native spot termination handling | `bool` | `true` | no |
143145
| <a name="input_iam_policy_description"></a> [iam\_policy\_description](#input\_iam\_policy\_description) | IAM policy description | `string` | `"Karpenter controller IAM policy"` | no |
144146
| <a name="input_iam_policy_name"></a> [iam\_policy\_name](#input\_iam\_policy\_name) | Name of the IAM policy | `string` | `"KarpenterController"` | no |

modules/karpenter/main.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,17 @@ resource "aws_iam_role" "controller" {
7070
tags = merge(var.tags, var.iam_role_tags)
7171
}
7272

73+
resource "aws_iam_role_policy" "controller" {
74+
count = local.create_iam_role && var.enable_inline_policy ? 1 : 0
75+
76+
name = var.iam_policy_use_name_prefix ? null : var.iam_policy_name
77+
name_prefix = var.iam_policy_use_name_prefix ? "${var.iam_policy_name}-" : null
78+
role = aws_iam_role.controller[0].name
79+
policy = data.aws_iam_policy_document.controller[0].json
80+
}
81+
7382
resource "aws_iam_policy" "controller" {
74-
count = local.create_iam_role ? 1 : 0
83+
count = local.create_iam_role && !var.enable_inline_policy ? 1 : 0
7584

7685
name = var.iam_policy_use_name_prefix ? null : var.iam_policy_name
7786
name_prefix = var.iam_policy_use_name_prefix ? "${var.iam_policy_name}-" : null
@@ -83,7 +92,7 @@ resource "aws_iam_policy" "controller" {
8392
}
8493

8594
resource "aws_iam_role_policy_attachment" "controller" {
86-
count = local.create_iam_role ? 1 : 0
95+
count = local.create_iam_role && !var.enable_inline_policy ? 1 : 0
8796

8897
role = aws_iam_role.controller[0].name
8998
policy_arn = aws_iam_policy.controller[0].arn

modules/karpenter/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ variable "create_iam_role" {
3232
default = true
3333
}
3434

35+
variable "enable_inline_policy" {
36+
description = "Determines whether the controller policy is created as a standard IAM policy or inline IAM policy. This can be enabled when the error `LimitExceeded: Cannot exceed quota for PolicySize: 6144` is received since standard IAM policies have a limit of 6,144 characters versus an inline role policy's limit of 10,240 ([Reference](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_iam-quotas.html))"
37+
type = bool
38+
default = false
39+
}
40+
3541
variable "iam_role_name" {
3642
description = "Name of the IAM role"
3743
type = string

0 commit comments

Comments
 (0)