Skip to content

Commit bd387d6

Browse files
feat: Add optional list of policy ARNs for attachment to Karpenter IRSA (#2537)
Co-authored-by: Bryant Biggs <[email protected]>
1 parent c013f7b commit bd387d6

File tree

4 files changed

+51
-20
lines changed

4 files changed

+51
-20
lines changed

examples/karpenter/main.tf

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ module "eks" {
8484
coredns = {
8585
configuration_values = jsonencode({
8686
computeType = "Fargate"
87+
# Ensure that the we fully utilize the minimum amount of resources that are supplied by
88+
# Fargate https://docs.aws.amazon.com/eks/latest/userguide/fargate-pod-configuration.html
89+
# Fargate adds 256 MB to each pod's memory reservation for the required Kubernetes
90+
# components (kubelet, kube-proxy, and containerd). Fargate rounds up to the following
91+
# compute configuration that most closely matches the sum of vCPU and memory requests in
92+
# order to ensure pods always have the resources that they need to run.
93+
resources = {
94+
limits = {
95+
cpu = "0.25"
96+
# We are targetting the smallest Task size of 512Mb, so we subtract 256Mb from the
97+
# request/limit to ensure we can fit within that task
98+
memory = "256M"
99+
}
100+
requests = {
101+
cpu = "0.25"
102+
# We are targetting the smallest Task size of 512Mb, so we subtract 256Mb from the
103+
# request/limit to ensure we can fit within that task
104+
memory = "256M"
105+
}
106+
}
87107
})
88108
}
89109
}
@@ -109,26 +129,18 @@ module "eks" {
109129
},
110130
]
111131

112-
fargate_profiles = merge(
113-
{ for i in range(3) :
114-
"kube-system-${element(split("-", local.azs[i]), 2)}" => {
115-
selectors = [
116-
{ namespace = "kube-system" }
117-
]
118-
# We want to create a profile per AZ for high availability
119-
subnet_ids = [element(module.vpc.private_subnets, i)]
120-
}
121-
},
122-
{ for i in range(3) :
123-
"karpenter-${element(split("-", local.azs[i]), 2)}" => {
124-
selectors = [
125-
{ namespace = "karpenter" }
126-
]
127-
# We want to create a profile per AZ for high availability
128-
subnet_ids = [element(module.vpc.private_subnets, i)]
129-
}
130-
},
131-
)
132+
fargate_profiles = {
133+
karpenter = {
134+
selectors = [
135+
{ namespace = "karpenter" }
136+
]
137+
}
138+
kube-system = {
139+
selectors = [
140+
{ namespace = "kube-system" }
141+
]
142+
}
143+
}
132144

133145
tags = merge(local.tags, {
134146
# NOTE - if creating multiple security groups with this module, only tag the
@@ -148,6 +160,10 @@ module "karpenter" {
148160
cluster_name = module.eks.cluster_name
149161
irsa_oidc_provider_arn = module.eks.oidc_provider_arn
150162

163+
policies = {
164+
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
165+
}
166+
151167
tags = local.tags
152168
}
153169

modules/karpenter/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ No modules.
124124
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
125125
| [aws_iam_role_policy_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
126126
| [aws_iam_role_policy_attachment.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
127+
| [aws_iam_role_policy_attachment.irsa_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
127128
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
128129
| [aws_sqs_queue.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
129130
| [aws_sqs_queue_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
@@ -169,6 +170,7 @@ No modules.
169170
| <a name="input_irsa_tag_key"></a> [irsa\_tag\_key](#input\_irsa\_tag\_key) | Tag key (`{key = value}`) applied to resources launched by Karpenter through the Karpenter provisioner | `string` | `"karpenter.sh/discovery"` | no |
170171
| <a name="input_irsa_tags"></a> [irsa\_tags](#input\_irsa\_tags) | A map of additional tags to add the the IAM role for service accounts | `map(any)` | `{}` | no |
171172
| <a name="input_irsa_use_name_prefix"></a> [irsa\_use\_name\_prefix](#input\_irsa\_use\_name\_prefix) | Determines whether the IAM role for service accounts name (`irsa_name`) is used as a prefix | `bool` | `true` | no |
173+
| <a name="input_policies"></a> [policies](#input\_policies) | Policies to attach to the IAM role in `{'static_name' = 'policy_arn'}` format | `map(string)` | `{}` | no |
172174
| <a name="input_queue_kms_data_key_reuse_period_seconds"></a> [queue\_kms\_data\_key\_reuse\_period\_seconds](#input\_queue\_kms\_data\_key\_reuse\_period\_seconds) | The length of time, in seconds, for which Amazon SQS can reuse a data key to encrypt or decrypt messages before calling AWS KMS again | `number` | `null` | no |
173175
| <a name="input_queue_kms_master_key_id"></a> [queue\_kms\_master\_key\_id](#input\_queue\_kms\_master\_key\_id) | The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK | `string` | `null` | no |
174176
| <a name="input_queue_managed_sse_enabled"></a> [queue\_managed\_sse\_enabled](#input\_queue\_managed\_sse\_enabled) | Boolean to enable server-side encryption (SSE) of message content with SQS-owned encryption keys | `bool` | `true` | no |

modules/karpenter/main.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ resource "aws_iam_role_policy_attachment" "irsa" {
175175
policy_arn = aws_iam_policy.irsa[0].arn
176176
}
177177

178+
resource "aws_iam_role_policy_attachment" "irsa_additional" {
179+
for_each = { for k, v in var.policies : k => v if local.create_irsa }
180+
181+
role = aws_iam_role.irsa[0].name
182+
policy_arn = each.value
183+
}
184+
178185
################################################################################
179186
# Node Termination Queue
180187
################################################################################

modules/karpenter/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ variable "irsa_tags" {
7474
default = {}
7575
}
7676

77+
variable "policies" {
78+
description = "Policies to attach to the IAM role in `{'static_name' = 'policy_arn'}` format"
79+
type = map(string)
80+
default = {}
81+
}
82+
7783
variable "irsa_tag_key" {
7884
description = "Tag key (`{key = value}`) applied to resources launched by Karpenter through the Karpenter provisioner"
7985
type = string

0 commit comments

Comments
 (0)