Skip to content

Commit 18081d1

Browse files
authored
fix: Permit RunInstances permission for Karpenter when request contains karpenter.sh/discovery tag key (#209)
1 parent eada402 commit 18081d1

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

examples/iam-role-for-service-accounts-eks/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ module "karpenter_controller_irsa_role" {
153153
role_name = "karpenter_controller"
154154
attach_karpenter_controller_policy = true
155155

156-
karpenter_controller_cluster_ids = [module.eks.cluster_id]
156+
karpenter_controller_cluster_id = module.eks.cluster_id
157157
karpenter_controller_node_iam_role_arns = [module.eks.eks_managed_node_groups["default"].iam_role_arn]
158158

159159
oidc_providers = {

modules/iam-role-for-service-accounts-eks/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ No modules.
124124
| [aws_iam_role_policy_attachment.node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
125125
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
126126
| [aws_iam_role_policy_attachment.vpc_cni](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
127+
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
127128
| [aws_iam_policy_document.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
128129
| [aws_iam_policy_document.ebs_csi](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
129130
| [aws_iam_policy_document.external_dns](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -151,7 +152,7 @@ No modules.
151152
| <a name="input_ebs_csi_kms_cmk_ids"></a> [ebs\_csi\_kms\_cmk\_ids](#input\_ebs\_csi\_kms\_cmk\_ids) | KMS CMK IDs to allow EBS CSI to manage encrypted volumes | `list(string)` | `[]` | no |
152153
| <a name="input_external_dns_hosted_zone_arns"></a> [external\_dns\_hosted\_zone\_arns](#input\_external\_dns\_hosted\_zone\_arns) | Route53 hosted zone ARNs to allow external DNS to manage records | `list(string)` | <pre>[<br> "arn:aws:route53:::hostedzone/*"<br>]</pre> | no |
153154
| <a name="input_force_detach_policies"></a> [force\_detach\_policies](#input\_force\_detach\_policies) | Whether policies should be detached from this role when destroying | `bool` | `true` | no |
154-
| <a name="input_karpenter_controller_cluster_ids"></a> [karpenter\_controller\_cluster\_ids](#input\_karpenter\_controller\_cluster\_ids) | List of cluster IDs to appropriately scope EC2 permissions within the Karpenter Controller policy | `list(string)` | `[]` | no |
155+
| <a name="input_karpenter_controller_cluster_id"></a> [karpenter\_controller\_cluster\_id](#input\_karpenter\_controller\_cluster\_id) | Cluster ID where the Karpenter controller is provisioned/managing | `string` | `"*"` | no |
155156
| <a name="input_karpenter_controller_node_iam_role_arns"></a> [karpenter\_controller\_node\_iam\_role\_arns](#input\_karpenter\_controller\_node\_iam\_role\_arns) | List of node IAM role ARNs Karpenter can use to launch nodes | `list(string)` | <pre>[<br> "*"<br>]</pre> | no |
156157
| <a name="input_karpenter_controller_ssm_parameter_arns"></a> [karpenter\_controller\_ssm\_parameter\_arns](#input\_karpenter\_controller\_ssm\_parameter\_arns) | List of SSM Parameter ARNs that contain AMI IDs launched by Karpenter | `list(string)` | <pre>[<br> "arn:aws:ssm:*:*:parameter/aws/service/*"<br>]</pre> | no |
157158
| <a name="input_max_session_duration"></a> [max\_session\_duration](#input\_max\_session\_duration) | Maximum CLI/API session duration in seconds between 3600 and 43200 | `number` | `null` | no |

modules/iam-role-for-service-accounts-eks/policies.tf

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
data "aws_partition" "current" {}
2+
data "aws_caller_identity" "current" {}
23

34
locals {
5+
account_id = data.aws_caller_identity.current.account_id
46
partition = data.aws_partition.current.partition
57
dns_suffix = data.aws_partition.current.dns_suffix
68
}
@@ -442,25 +444,46 @@ data "aws_iam_policy_document" "karpenter_controller" {
442444
resources = ["*"]
443445
}
444446

445-
dynamic "statement" {
446-
for_each = toset(var.karpenter_controller_cluster_ids)
447-
content {
448-
actions = [
449-
"ec2:RunInstances",
450-
"ec2:TerminateInstances",
451-
"ec2:DeleteLaunchTemplate",
452-
]
447+
statement {
448+
actions = [
449+
"ec2:TerminateInstances",
450+
"ec2:DeleteLaunchTemplate",
451+
]
453452

454-
resources = ["*"]
453+
resources = ["*"]
455454

456-
condition {
457-
test = "StringEquals"
458-
variable = "ec2:ResourceTag/karpenter.sh/discovery"
459-
values = [statement.value]
460-
}
455+
condition {
456+
test = "StringEquals"
457+
variable = "ec2:ResourceTag/karpenter.sh/discovery"
458+
values = [var.karpenter_controller_cluster_id]
461459
}
462460
}
463461

462+
statement {
463+
actions = ["ec2:RunInstances"]
464+
resources = [
465+
"arn:aws:ec2:*:${local.account_id}:launch-template/*",
466+
"arn:aws:ec2:*:${local.account_id}:security-group/*",
467+
"arn:aws:ec2:*:${local.account_id}:subnet/*",
468+
]
469+
470+
condition {
471+
test = "StringEquals"
472+
variable = "ec2:ResourceTag/karpenter.sh/discovery"
473+
values = [var.karpenter_controller_cluster_id]
474+
}
475+
}
476+
477+
statement {
478+
actions = ["ec2:RunInstances"]
479+
resources = [
480+
"arn:aws:ec2:*::image/*",
481+
"arn:aws:ec2:*:${local.account_id}:instance/*",
482+
"arn:aws:ec2:*:${local.account_id}:volume/*",
483+
"arn:aws:ec2:*:${local.account_id}:network-interface/*",
484+
]
485+
}
486+
464487
statement {
465488
actions = ["ssm:GetParameter"]
466489
resources = var.karpenter_controller_ssm_parameter_arns

modules/iam-role-for-service-accounts-eks/variables.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ variable "attach_karpenter_controller_policy" {
152152
default = false
153153
}
154154

155-
variable "karpenter_controller_cluster_ids" {
156-
description = "List of cluster IDs to appropriately scope EC2 permissions within the Karpenter Controller policy"
157-
type = list(string)
158-
default = []
155+
variable "karpenter_controller_cluster_id" {
156+
description = "Cluster ID where the Karpenter controller is provisioned/managing"
157+
type = string
158+
default = "*"
159159
}
160160

161161
variable "karpenter_controller_ssm_parameter_arns" {

0 commit comments

Comments
 (0)