mirror of
https://github.com/ysoftdevs/terraform-aws-eks.git
synced 2026-03-19 07:53:46 +01:00
feat: New Karpenter sub-module for easily enabling Karpenter on EKS (#2303)
This commit is contained in:
195
modules/karpenter/README.md
Normal file
195
modules/karpenter/README.md
Normal file
@@ -0,0 +1,195 @@
|
||||
# Karpenter Module
|
||||
|
||||
Configuration in this directory creates the AWS resources required by Karpenter
|
||||
|
||||
## Usage
|
||||
|
||||
### All Resources (Default)
|
||||
|
||||
In the following example, the Karpenter module will create:
|
||||
- An IAM role for service accounts (IRSA) with a narrowly scoped IAM policy for the Karpenter controller to utilize
|
||||
- An IAM role and instance profile for the nodes created by Karpenter to utilize
|
||||
- Note: This IAM role ARN will need to be added to the `aws-auth` configmap for nodes to join the cluster successfully
|
||||
- An SQS queue and Eventbridge event rules for Karpenter to utilize for spot termination handling, capacity rebalancing, etc.
|
||||
|
||||
This setup is great for running Karpenter on EKS Fargate:
|
||||
|
||||
```hcl
|
||||
module "eks" {
|
||||
source = "terraform-aws-modules/eks"
|
||||
|
||||
# Shown just for connection between cluster and Karpenter sub-module below
|
||||
manage_aws_auth_configmap = true
|
||||
aws_auth_roles = [
|
||||
# We need to add in the Karpenter node IAM role for nodes launched by Karpenter
|
||||
{
|
||||
rolearn = module.karpenter.role_arn
|
||||
username = "system:node:{{EC2PrivateDNSName}}"
|
||||
groups = [
|
||||
"system:bootstrappers",
|
||||
"system:nodes",
|
||||
]
|
||||
},
|
||||
]
|
||||
...
|
||||
}
|
||||
|
||||
module "karpenter" {
|
||||
source = "terraform-aws-modules/eks/aws//modules/karpenter"
|
||||
|
||||
cluster_name = module.eks.cluster_name
|
||||
|
||||
irsa_oidc_provider_arn = module.eks.oidc_provider_arn
|
||||
irsa_namespace_service_accounts = ["karpenter:karpenter"]
|
||||
|
||||
tags = {
|
||||
Environment = "dev"
|
||||
Terraform = "true"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### External Node IAM Role (Default)
|
||||
|
||||
In the following example, the Karpenter module will create:
|
||||
- An IAM role for service accounts (IRSA) with a narrowly scoped IAM policy for the Karpenter controller to utilize
|
||||
- An IAM instance profile for the nodes created by Karpenter to utilize
|
||||
- Note: This setup will utilize the existing IAM role created by the EKS Managed Node group which means the role is already populated in the `aws-auth` configmap and no further updates are required.
|
||||
- An SQS queue and Eventbridge event rules for Karpenter to utilize for spot termination handling, capacity rebalancing, etc.
|
||||
|
||||
In this scenario, Karpenter would run atop the EKS Managed Node group and scale out nodes as needed from there:
|
||||
|
||||
```hcl
|
||||
module "eks" {
|
||||
source = "terraform-aws-modules/eks"
|
||||
|
||||
# Shown just for connection between cluster and Karpenter sub-module below
|
||||
eks_managed_node_groups = {
|
||||
initial = {
|
||||
instance_types = ["t3.medium"]
|
||||
|
||||
min_size = 1
|
||||
max_size = 3
|
||||
desired_size = 1
|
||||
}
|
||||
}
|
||||
...
|
||||
}
|
||||
|
||||
module "karpenter" {
|
||||
source = "terraform-aws-modules/eks/aws//modules/karpenter"
|
||||
|
||||
cluster_name = module.eks.cluster_name
|
||||
|
||||
irsa_oidc_provider_arn = module.eks.oidc_provider_arn
|
||||
irsa_namespace_service_accounts = ["karpenter:karpenter"]
|
||||
|
||||
create_iam_role = false
|
||||
iam_role_arn = module.eks.eks_managed_node_groups["initial"].iam_role_arn
|
||||
|
||||
tags = {
|
||||
Environment = "dev"
|
||||
Terraform = "true"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
## Requirements
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.1 |
|
||||
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.72 |
|
||||
|
||||
## Providers
|
||||
|
||||
| Name | Version |
|
||||
|------|---------|
|
||||
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 3.72 |
|
||||
|
||||
## Modules
|
||||
|
||||
No modules.
|
||||
|
||||
## Resources
|
||||
|
||||
| Name | Type |
|
||||
|------|------|
|
||||
| [aws_cloudwatch_event_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
|
||||
| [aws_cloudwatch_event_target.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
|
||||
| [aws_iam_instance_profile.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_instance_profile) | resource |
|
||||
| [aws_iam_policy.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
|
||||
| [aws_iam_role.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
|
||||
| [aws_iam_role.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
|
||||
| [aws_iam_role_policy_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
|
||||
| [aws_iam_role_policy_attachment.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
|
||||
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
|
||||
| [aws_sqs_queue.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
|
||||
| [aws_sqs_queue_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_policy) | resource |
|
||||
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
|
||||
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
|
||||
| [aws_iam_policy_document.irsa](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
|
||||
| [aws_iam_policy_document.irsa_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
|
||||
| [aws_iam_policy_document.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
|
||||
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
|
||||
|
||||
## Inputs
|
||||
|
||||
| Name | Description | Type | Default | Required |
|
||||
|------|-------------|------|---------|:--------:|
|
||||
| <a name="input_cluster_ip_family"></a> [cluster\_ip\_family](#input\_cluster\_ip\_family) | The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6` | `string` | `null` | no |
|
||||
| <a name="input_cluster_name"></a> [cluster\_name](#input\_cluster\_name) | The name of the EKS cluster | `string` | `""` | no |
|
||||
| <a name="input_create"></a> [create](#input\_create) | Determines whether to create EKS managed node group or not | `bool` | `true` | no |
|
||||
| <a name="input_create_iam_role"></a> [create\_iam\_role](#input\_create\_iam\_role) | Determines whether an IAM role is created or to use an existing IAM role | `bool` | `true` | no |
|
||||
| <a name="input_create_instance_profile"></a> [create\_instance\_profile](#input\_create\_instance\_profile) | Whether to create an IAM instance profile | `bool` | `true` | no |
|
||||
| <a name="input_create_irsa"></a> [create\_irsa](#input\_create\_irsa) | Determines whether an IAM role for service accounts is created | `bool` | `true` | no |
|
||||
| <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 |
|
||||
| <a name="input_iam_role_additional_policies"></a> [iam\_role\_additional\_policies](#input\_iam\_role\_additional\_policies) | Additional policies to be added to the IAM role | `list(string)` | `[]` | no |
|
||||
| <a name="input_iam_role_arn"></a> [iam\_role\_arn](#input\_iam\_role\_arn) | Existing IAM role ARN for the IAM instance profile. Required if `create_iam_role` is set to `false` | `string` | `null` | no |
|
||||
| <a name="input_iam_role_attach_cni_policy"></a> [iam\_role\_attach\_cni\_policy](#input\_iam\_role\_attach\_cni\_policy) | Whether to attach the `AmazonEKS_CNI_Policy`/`AmazonEKS_CNI_IPv6_Policy` IAM policy to the IAM IAM role. WARNING: If set `false` the permissions must be assigned to the `aws-node` DaemonSet pods via another method or nodes will not be able to join the cluster | `bool` | `true` | no |
|
||||
| <a name="input_iam_role_description"></a> [iam\_role\_description](#input\_iam\_role\_description) | Description of the role | `string` | `null` | no |
|
||||
| <a name="input_iam_role_max_session_duration"></a> [iam\_role\_max\_session\_duration](#input\_iam\_role\_max\_session\_duration) | Maximum API session duration in seconds between 3600 and 43200 | `number` | `null` | no |
|
||||
| <a name="input_iam_role_name"></a> [iam\_role\_name](#input\_iam\_role\_name) | Name to use on IAM role created | `string` | `null` | no |
|
||||
| <a name="input_iam_role_path"></a> [iam\_role\_path](#input\_iam\_role\_path) | IAM role path | `string` | `"/"` | no |
|
||||
| <a name="input_iam_role_permissions_boundary"></a> [iam\_role\_permissions\_boundary](#input\_iam\_role\_permissions\_boundary) | ARN of the policy that is used to set the permissions boundary for the IAM role | `string` | `null` | no |
|
||||
| <a name="input_iam_role_tags"></a> [iam\_role\_tags](#input\_iam\_role\_tags) | A map of additional tags to add to the IAM role created | `map(string)` | `{}` | no |
|
||||
| <a name="input_iam_role_use_name_prefix"></a> [iam\_role\_use\_name\_prefix](#input\_iam\_role\_use\_name\_prefix) | Determines whether the IAM role name (`iam_role_name`) is used as a prefix | `bool` | `true` | no |
|
||||
| <a name="input_irsa_assume_role_condition_test"></a> [irsa\_assume\_role\_condition\_test](#input\_irsa\_assume\_role\_condition\_test) | Name of the [IAM condition operator](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html) to evaluate when assuming the role | `string` | `"StringEquals"` | no |
|
||||
| <a name="input_irsa_description"></a> [irsa\_description](#input\_irsa\_description) | IAM role for service accounts description | `string` | `"Karpenter IAM role for service account"` | no |
|
||||
| <a name="input_irsa_max_session_duration"></a> [irsa\_max\_session\_duration](#input\_irsa\_max\_session\_duration) | Maximum API session duration in seconds between 3600 and 43200 | `number` | `null` | no |
|
||||
| <a name="input_irsa_name"></a> [irsa\_name](#input\_irsa\_name) | Name of IAM role for service accounts | `string` | `null` | no |
|
||||
| <a name="input_irsa_namespace_service_accounts"></a> [irsa\_namespace\_service\_accounts](#input\_irsa\_namespace\_service\_accounts) | List of `namespace:serviceaccount`pairs to use in trust policy for IAM role for service accounts | `list(string)` | <pre>[<br> "karpenter:karpenter"<br>]</pre> | no |
|
||||
| <a name="input_irsa_oidc_provider_arn"></a> [irsa\_oidc\_provider\_arn](#input\_irsa\_oidc\_provider\_arn) | OIDC provider arn used in trust policy for IAM role for service accounts | `string` | `""` | no |
|
||||
| <a name="input_irsa_path"></a> [irsa\_path](#input\_irsa\_path) | Path of IAM role for service accounts | `string` | `"/"` | no |
|
||||
| <a name="input_irsa_permissions_boundary_arn"></a> [irsa\_permissions\_boundary\_arn](#input\_irsa\_permissions\_boundary\_arn) | Permissions boundary ARN to use for IAM role for service accounts | `string` | `null` | no |
|
||||
| <a name="input_irsa_ssm_parameter_arns"></a> [irsa\_ssm\_parameter\_arns](#input\_irsa\_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 |
|
||||
| <a name="input_irsa_subnet_account_id"></a> [irsa\_subnet\_account\_id](#input\_irsa\_subnet\_account\_id) | Account ID of where the subnets Karpenter will utilize resides. Used when subnets are shared from another account | `string` | `""` | no |
|
||||
| <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 |
|
||||
| <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 |
|
||||
| <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 |
|
||||
| <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 |
|
||||
| <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 |
|
||||
| <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 |
|
||||
| <a name="input_queue_name"></a> [queue\_name](#input\_queue\_name) | Name of the SQS queue | `string` | `null` | no |
|
||||
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
|
||||
|
||||
## Outputs
|
||||
|
||||
| Name | Description |
|
||||
|------|-------------|
|
||||
| <a name="output_event_rules"></a> [event\_rules](#output\_event\_rules) | Map of the event rules created and their attributes |
|
||||
| <a name="output_instance_profile_arn"></a> [instance\_profile\_arn](#output\_instance\_profile\_arn) | ARN assigned by AWS to the instance profile |
|
||||
| <a name="output_instance_profile_id"></a> [instance\_profile\_id](#output\_instance\_profile\_id) | Instance profile's ID |
|
||||
| <a name="output_instance_profile_name"></a> [instance\_profile\_name](#output\_instance\_profile\_name) | Name of the instance profile |
|
||||
| <a name="output_instance_profile_unique"></a> [instance\_profile\_unique](#output\_instance\_profile\_unique) | Stable and unique string identifying the IAM instance profile |
|
||||
| <a name="output_irsa_arn"></a> [irsa\_arn](#output\_irsa\_arn) | The Amazon Resource Name (ARN) specifying the IAM role for service accounts |
|
||||
| <a name="output_irsa_name"></a> [irsa\_name](#output\_irsa\_name) | The name of the IAM role for service accounts |
|
||||
| <a name="output_irsa_unique_id"></a> [irsa\_unique\_id](#output\_irsa\_unique\_id) | Stable and unique string identifying the IAM role for service accounts |
|
||||
| <a name="output_queue_arn"></a> [queue\_arn](#output\_queue\_arn) | The ARN of the SQS queue |
|
||||
| <a name="output_queue_name"></a> [queue\_name](#output\_queue\_name) | The name of the created Amazon SQS queue |
|
||||
| <a name="output_queue_url"></a> [queue\_url](#output\_queue\_url) | The URL for the created Amazon SQS queue |
|
||||
| <a name="output_role_arn"></a> [role\_arn](#output\_role\_arn) | The Amazon Resource Name (ARN) specifying the IAM role |
|
||||
| <a name="output_role_name"></a> [role\_name](#output\_role\_name) | The name of the IAM role |
|
||||
| <a name="output_role_unique_id"></a> [role\_unique\_id](#output\_role\_unique\_id) | Stable and unique string identifying the IAM role |
|
||||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
|
||||
359
modules/karpenter/main.tf
Normal file
359
modules/karpenter/main.tf
Normal file
@@ -0,0 +1,359 @@
|
||||
data "aws_partition" "current" {}
|
||||
data "aws_caller_identity" "current" {}
|
||||
|
||||
locals {
|
||||
account_id = data.aws_caller_identity.current.account_id
|
||||
partition = data.aws_partition.current.partition
|
||||
dns_suffix = data.aws_partition.current.dns_suffix
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# IAM Role for Service Account (IRSA)
|
||||
# This is used by the Karpenter controller
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
create_irsa = var.create && var.create_irsa
|
||||
irsa_name = coalesce(var.irsa_name, "KarpenterIRSA-${var.cluster_name}")
|
||||
|
||||
irsa_oidc_provider_url = replace(var.irsa_oidc_provider_arn, "/^(.*provider/)/", "")
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "irsa_assume_role" {
|
||||
count = local.create_irsa ? 1 : 0
|
||||
|
||||
statement {
|
||||
effect = "Allow"
|
||||
actions = ["sts:AssumeRoleWithWebIdentity"]
|
||||
|
||||
principals {
|
||||
type = "Federated"
|
||||
identifiers = [var.irsa_oidc_provider_arn]
|
||||
}
|
||||
|
||||
condition {
|
||||
test = var.irsa_assume_role_condition_test
|
||||
variable = "${local.irsa_oidc_provider_url}:sub"
|
||||
values = [for sa in var.irsa_namespace_service_accounts : "system:serviceaccount:${sa}"]
|
||||
}
|
||||
|
||||
# https://aws.amazon.com/premiumsupport/knowledge-center/eks-troubleshoot-oidc-and-irsa/?nc1=h_ls
|
||||
condition {
|
||||
test = var.irsa_assume_role_condition_test
|
||||
variable = "${local.irsa_oidc_provider_url}:aud"
|
||||
values = ["sts.amazonaws.com"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "irsa" {
|
||||
count = local.create_irsa ? 1 : 0
|
||||
|
||||
name = var.irsa_use_name_prefix ? null : local.irsa_name
|
||||
name_prefix = var.irsa_use_name_prefix ? "${local.irsa_name}-" : null
|
||||
path = var.irsa_path
|
||||
description = var.irsa_description
|
||||
|
||||
assume_role_policy = data.aws_iam_policy_document.irsa_assume_role[0].json
|
||||
max_session_duration = var.irsa_max_session_duration
|
||||
permissions_boundary = var.irsa_permissions_boundary_arn
|
||||
force_detach_policies = true
|
||||
|
||||
tags = merge(var.tags, var.irsa_tags)
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "irsa" {
|
||||
count = local.create_irsa ? 1 : 0
|
||||
|
||||
statement {
|
||||
actions = [
|
||||
"ec2:CreateLaunchTemplate",
|
||||
"ec2:CreateFleet",
|
||||
"ec2:CreateTags",
|
||||
"ec2:DescribeLaunchTemplates",
|
||||
"ec2:DescribeImages",
|
||||
"ec2:DescribeInstances",
|
||||
"ec2:DescribeSecurityGroups",
|
||||
"ec2:DescribeSubnets",
|
||||
"ec2:DescribeInstanceTypes",
|
||||
"ec2:DescribeInstanceTypeOfferings",
|
||||
"ec2:DescribeAvailabilityZones",
|
||||
"ec2:DescribeSpotPriceHistory",
|
||||
"pricing:GetProducts",
|
||||
]
|
||||
|
||||
resources = ["*"]
|
||||
}
|
||||
|
||||
statement {
|
||||
actions = [
|
||||
"ec2:TerminateInstances",
|
||||
"ec2:DeleteLaunchTemplate",
|
||||
]
|
||||
|
||||
resources = ["*"]
|
||||
|
||||
condition {
|
||||
test = "StringEquals"
|
||||
variable = "ec2:ResourceTag/${var.irsa_tag_key}"
|
||||
values = [var.cluster_name]
|
||||
}
|
||||
}
|
||||
|
||||
statement {
|
||||
actions = ["ec2:RunInstances"]
|
||||
resources = [
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:launch-template/*",
|
||||
]
|
||||
|
||||
condition {
|
||||
test = "StringEquals"
|
||||
variable = "ec2:ResourceTag/${var.irsa_tag_key}"
|
||||
values = [var.cluster_name]
|
||||
}
|
||||
}
|
||||
|
||||
statement {
|
||||
actions = ["ec2:RunInstances"]
|
||||
resources = [
|
||||
"arn:${local.partition}:ec2:*::image/*",
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:instance/*",
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:spot-instances-request/*",
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:security-group/*",
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:volume/*",
|
||||
"arn:${local.partition}:ec2:*:${local.account_id}:network-interface/*",
|
||||
"arn:${local.partition}:ec2:*:${coalesce(var.irsa_subnet_account_id, local.account_id)}:subnet/*",
|
||||
]
|
||||
}
|
||||
|
||||
statement {
|
||||
actions = ["ssm:GetParameter"]
|
||||
resources = var.irsa_ssm_parameter_arns
|
||||
}
|
||||
|
||||
statement {
|
||||
actions = ["iam:PassRole"]
|
||||
resources = [var.create_iam_role ? aws_iam_role.this[0].arn : var.iam_role_arn]
|
||||
}
|
||||
|
||||
dynamic "statement" {
|
||||
for_each = local.enable_spot_termination ? [1] : []
|
||||
|
||||
content {
|
||||
actions = [
|
||||
"sqs:DeleteMessage",
|
||||
"sqs:GetQueueUrl",
|
||||
"sqs:GetQueueAttributes",
|
||||
"sqs:ReceiveMessage",
|
||||
]
|
||||
resources = [aws_sqs_queue.this[0].arn]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_iam_policy" "irsa" {
|
||||
count = var.create_irsa ? 1 : 0
|
||||
|
||||
name_prefix = "${local.irsa_name}-"
|
||||
path = var.irsa_path
|
||||
description = var.irsa_description
|
||||
policy = data.aws_iam_policy_document.irsa[0].json
|
||||
|
||||
tags = var.tags
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy_attachment" "irsa" {
|
||||
count = local.create_irsa ? 1 : 0
|
||||
|
||||
role = aws_iam_role.irsa[0].name
|
||||
policy_arn = aws_iam_policy.irsa[0].arn
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node Termination Queue
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
enable_spot_termination = var.create && var.enable_spot_termination
|
||||
|
||||
queue_name = coalesce(var.queue_name, "Karpenter-${var.cluster_name}")
|
||||
}
|
||||
|
||||
resource "aws_sqs_queue" "this" {
|
||||
count = local.enable_spot_termination ? 1 : 0
|
||||
|
||||
name = local.queue_name
|
||||
message_retention_seconds = 300
|
||||
sqs_managed_sse_enabled = var.queue_managed_sse_enabled
|
||||
kms_master_key_id = var.queue_kms_master_key_id
|
||||
kms_data_key_reuse_period_seconds = var.queue_kms_data_key_reuse_period_seconds
|
||||
|
||||
tags = var.tags
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "queue" {
|
||||
count = local.enable_spot_termination ? 1 : 0
|
||||
|
||||
statement {
|
||||
sid = "SqsWrite"
|
||||
actions = ["sqs:SendMessage"]
|
||||
resources = [aws_sqs_queue.this[0].arn]
|
||||
|
||||
principals {
|
||||
type = "Service"
|
||||
identifiers = [
|
||||
"events.${local.dns_suffix}",
|
||||
"sqs.${local.dns_suffix}",
|
||||
]
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_sqs_queue_policy" "this" {
|
||||
count = local.enable_spot_termination ? 1 : 0
|
||||
|
||||
queue_url = aws_sqs_queue.this[0].url
|
||||
policy = data.aws_iam_policy_document.queue[0].json
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node Termination Event Rules
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
events = {
|
||||
health_event = {
|
||||
name = "HealthEvent"
|
||||
description = "Karpenter interrupt - AWS health event"
|
||||
event_pattern = {
|
||||
source = ["aws.health"]
|
||||
detail-type = ["AWS Health Event"]
|
||||
}
|
||||
}
|
||||
spot_interupt = {
|
||||
name = "SpotInterrupt"
|
||||
description = "Karpenter interrupt - EC2 spot instance interruption warning"
|
||||
event_pattern = {
|
||||
source = ["aws.ec2"]
|
||||
detail-type = ["EC2 Spot Instance Interruption Warning"]
|
||||
}
|
||||
}
|
||||
instance_rebalance = {
|
||||
name = "InstanceRebalance"
|
||||
description = "Karpenter interrupt - EC2 instance rebalance recommendation"
|
||||
event_pattern = {
|
||||
source = ["aws.ec2"]
|
||||
detail-type = ["EC2 Instance Rebalance Recommendation"]
|
||||
}
|
||||
}
|
||||
instance_state_change = {
|
||||
name = "InstanceStateChange"
|
||||
description = "Karpenter interrupt - EC2 instance state-change notification"
|
||||
event_pattern = {
|
||||
source = ["aws.ec2"]
|
||||
detail-type = ["EC2 Instance State-change Notification"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_cloudwatch_event_rule" "this" {
|
||||
for_each = { for k, v in local.events : k => v if local.enable_spot_termination }
|
||||
|
||||
name = "Karpenter${each.value.name}-${var.cluster_name}"
|
||||
description = each.value.description
|
||||
event_pattern = jsonencode(each.value.event_pattern)
|
||||
|
||||
tags = var.tags
|
||||
}
|
||||
|
||||
resource "aws_cloudwatch_event_target" "this" {
|
||||
for_each = { for k, v in local.events : k => v if local.enable_spot_termination }
|
||||
|
||||
rule = aws_cloudwatch_event_rule.this[each.key].name
|
||||
target_id = "KarpenterInterruptionQueueTarget"
|
||||
arn = aws_sqs_queue.this[0].arn
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Role
|
||||
# This is used by the nodes launched by Karpenter
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
create_iam_role = var.create && var.create_iam_role
|
||||
|
||||
iam_role_name = coalesce(var.iam_role_name, "Karpenter-${var.cluster_name}")
|
||||
iam_role_policy_prefix = "arn:${local.partition}:iam::aws:policy"
|
||||
cni_policy = var.cluster_ip_family == "ipv6" ? "${local.iam_role_policy_prefix}/AmazonEKS_CNI_IPv6_Policy" : "${local.iam_role_policy_prefix}/AmazonEKS_CNI_Policy"
|
||||
}
|
||||
|
||||
data "aws_iam_policy_document" "assume_role" {
|
||||
count = local.create_iam_role ? 1 : 0
|
||||
|
||||
statement {
|
||||
sid = "EKSNodeAssumeRole"
|
||||
actions = ["sts:AssumeRole"]
|
||||
|
||||
principals {
|
||||
type = "Service"
|
||||
identifiers = ["ec2.${local.dns_suffix}"]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resource "aws_iam_role" "this" {
|
||||
count = local.create_iam_role ? 1 : 0
|
||||
|
||||
name = var.iam_role_use_name_prefix ? null : local.iam_role_name
|
||||
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
|
||||
path = var.iam_role_path
|
||||
description = var.iam_role_description
|
||||
|
||||
assume_role_policy = data.aws_iam_policy_document.assume_role[0].json
|
||||
max_session_duration = var.iam_role_max_session_duration
|
||||
permissions_boundary = var.iam_role_permissions_boundary
|
||||
force_detach_policies = true
|
||||
|
||||
tags = merge(var.tags, var.iam_role_tags)
|
||||
}
|
||||
|
||||
# Policies attached ref https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group
|
||||
resource "aws_iam_role_policy_attachment" "this" {
|
||||
for_each = { for k, v in toset(compact([
|
||||
"${local.iam_role_policy_prefix}/AmazonEKSWorkerNodePolicy",
|
||||
"${local.iam_role_policy_prefix}/AmazonEC2ContainerRegistryReadOnly",
|
||||
var.iam_role_attach_cni_policy ? local.cni_policy : "",
|
||||
])) : k => v if local.create_iam_role }
|
||||
|
||||
policy_arn = each.value
|
||||
role = aws_iam_role.this[0].name
|
||||
}
|
||||
|
||||
resource "aws_iam_role_policy_attachment" "additional" {
|
||||
for_each = { for k, v in var.iam_role_additional_policies : k => v if local.create_iam_role }
|
||||
|
||||
policy_arn = each.value
|
||||
role = aws_iam_role.this[0].name
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Instance Profile
|
||||
# This is used by the nodes launched by Karpenter
|
||||
################################################################################
|
||||
|
||||
locals {
|
||||
external_role_name = try(replace(var.iam_role_arn, "/^(.*role/)/", ""), null)
|
||||
}
|
||||
|
||||
resource "aws_iam_instance_profile" "this" {
|
||||
count = var.create && var.create_instance_profile ? 1 : 0
|
||||
|
||||
name = var.iam_role_use_name_prefix ? null : local.iam_role_name
|
||||
name_prefix = var.iam_role_use_name_prefix ? "${local.iam_role_name}-" : null
|
||||
path = var.iam_role_path
|
||||
role = var.create_iam_role ? aws_iam_role.this[0].name : local.external_role_name
|
||||
|
||||
tags = merge(var.tags, var.iam_role_tags)
|
||||
}
|
||||
89
modules/karpenter/outputs.tf
Normal file
89
modules/karpenter/outputs.tf
Normal file
@@ -0,0 +1,89 @@
|
||||
################################################################################
|
||||
# IAM Role for Service Account (IRSA)
|
||||
################################################################################
|
||||
|
||||
output "irsa_name" {
|
||||
description = "The name of the IAM role for service accounts"
|
||||
value = try(aws_iam_role.irsa[0].name, null)
|
||||
}
|
||||
|
||||
output "irsa_arn" {
|
||||
description = "The Amazon Resource Name (ARN) specifying the IAM role for service accounts"
|
||||
value = try(aws_iam_role.irsa[0].arn, null)
|
||||
}
|
||||
|
||||
output "irsa_unique_id" {
|
||||
description = "Stable and unique string identifying the IAM role for service accounts"
|
||||
value = try(aws_iam_role.irsa[0].unique_id, null)
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node Termination Queue
|
||||
################################################################################
|
||||
|
||||
output "queue_arn" {
|
||||
description = "The ARN of the SQS queue"
|
||||
value = try(aws_sqs_queue.this[0].arn, null)
|
||||
}
|
||||
|
||||
output "queue_name" {
|
||||
description = "The name of the created Amazon SQS queue"
|
||||
value = try(aws_sqs_queue.this[0].name, null)
|
||||
}
|
||||
|
||||
output "queue_url" {
|
||||
description = "The URL for the created Amazon SQS queue"
|
||||
value = try(aws_sqs_queue.this[0].url, null)
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node Termination Event Rules
|
||||
################################################################################
|
||||
|
||||
output "event_rules" {
|
||||
description = "Map of the event rules created and their attributes"
|
||||
value = aws_cloudwatch_event_rule.this
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Role
|
||||
################################################################################
|
||||
|
||||
output "role_name" {
|
||||
description = "The name of the IAM role"
|
||||
value = try(aws_iam_role.this[0].name, null)
|
||||
}
|
||||
|
||||
output "role_arn" {
|
||||
description = "The Amazon Resource Name (ARN) specifying the IAM role"
|
||||
value = try(aws_iam_role.this[0].arn, var.iam_role_arn)
|
||||
}
|
||||
|
||||
output "role_unique_id" {
|
||||
description = "Stable and unique string identifying the IAM role"
|
||||
value = try(aws_iam_role.this[0].unique_id, null)
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Instance Profile
|
||||
################################################################################
|
||||
|
||||
output "instance_profile_arn" {
|
||||
description = "ARN assigned by AWS to the instance profile"
|
||||
value = try(aws_iam_instance_profile.this[0].arn, null)
|
||||
}
|
||||
|
||||
output "instance_profile_id" {
|
||||
description = "Instance profile's ID"
|
||||
value = try(aws_iam_instance_profile.this[0].id, null)
|
||||
}
|
||||
|
||||
output "instance_profile_name" {
|
||||
description = "Name of the instance profile"
|
||||
value = try(aws_iam_instance_profile.this[0].name, null)
|
||||
}
|
||||
|
||||
output "instance_profile_unique" {
|
||||
description = "Stable and unique string identifying the IAM instance profile"
|
||||
value = try(aws_iam_instance_profile.this[0].unique_id, null)
|
||||
}
|
||||
226
modules/karpenter/variables.tf
Normal file
226
modules/karpenter/variables.tf
Normal file
@@ -0,0 +1,226 @@
|
||||
variable "create" {
|
||||
description = "Determines whether to create EKS managed node group or not"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "tags" {
|
||||
description = "A map of tags to add to all resources"
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "cluster_name" {
|
||||
description = "The name of the EKS cluster"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# IAM Role for Service Account (IRSA)
|
||||
################################################################################
|
||||
|
||||
variable "create_irsa" {
|
||||
description = "Determines whether an IAM role for service accounts is created"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "irsa_name" {
|
||||
description = "Name of IAM role for service accounts"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "irsa_use_name_prefix" {
|
||||
description = "Determines whether the IAM role for service accounts name (`irsa_name`) is used as a prefix"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "irsa_path" {
|
||||
description = "Path of IAM role for service accounts"
|
||||
type = string
|
||||
default = "/"
|
||||
}
|
||||
|
||||
variable "irsa_description" {
|
||||
description = "IAM role for service accounts description"
|
||||
type = string
|
||||
default = "Karpenter IAM role for service account"
|
||||
}
|
||||
|
||||
variable "irsa_max_session_duration" {
|
||||
description = "Maximum API session duration in seconds between 3600 and 43200"
|
||||
type = number
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "irsa_permissions_boundary_arn" {
|
||||
description = "Permissions boundary ARN to use for IAM role for service accounts"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "irsa_tags" {
|
||||
description = "A map of additional tags to add the the IAM role for service accounts"
|
||||
type = map(any)
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "irsa_tag_key" {
|
||||
description = "Tag key (`{key = value}`) applied to resources launched by Karpenter through the Karpenter provisioner"
|
||||
type = string
|
||||
default = "karpenter.sh/discovery"
|
||||
}
|
||||
|
||||
variable "irsa_ssm_parameter_arns" {
|
||||
description = "List of SSM Parameter ARNs that contain AMI IDs launched by Karpenter"
|
||||
type = list(string)
|
||||
# https://github.com/aws/karpenter/blob/ed9473a9863ca949b61b9846c8b9f33f35b86dbd/pkg/cloudprovider/aws/ami.go#L105-L123
|
||||
default = ["arn:aws:ssm:*:*:parameter/aws/service/*"]
|
||||
}
|
||||
|
||||
variable "irsa_subnet_account_id" {
|
||||
description = "Account ID of where the subnets Karpenter will utilize resides. Used when subnets are shared from another account"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "irsa_oidc_provider_arn" {
|
||||
description = "OIDC provider arn used in trust policy for IAM role for service accounts"
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
|
||||
variable "irsa_namespace_service_accounts" {
|
||||
description = "List of `namespace:serviceaccount`pairs to use in trust policy for IAM role for service accounts"
|
||||
type = list(string)
|
||||
default = ["karpenter:karpenter"]
|
||||
}
|
||||
|
||||
variable "irsa_assume_role_condition_test" {
|
||||
description = "Name of the [IAM condition operator](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html) to evaluate when assuming the role"
|
||||
type = string
|
||||
default = "StringEquals"
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node Termination Queue
|
||||
################################################################################
|
||||
|
||||
variable "enable_spot_termination" {
|
||||
description = "Determines whether to enable native spot termination handling"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "queue_name" {
|
||||
description = "Name of the SQS queue"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "queue_managed_sse_enabled" {
|
||||
description = "Boolean to enable server-side encryption (SSE) of message content with SQS-owned encryption keys"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "queue_kms_master_key_id" {
|
||||
description = "The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "queue_kms_data_key_reuse_period_seconds" {
|
||||
description = "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"
|
||||
type = number
|
||||
default = null
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Role & Instance Profile
|
||||
################################################################################
|
||||
|
||||
variable "create_iam_role" {
|
||||
description = "Determines whether an IAM role is created or to use an existing IAM role"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "cluster_ip_family" {
|
||||
description = "The IP family used to assign Kubernetes pod and service addresses. Valid values are `ipv4` (default) and `ipv6`"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_arn" {
|
||||
description = "Existing IAM role ARN for the IAM instance profile. Required if `create_iam_role` is set to `false`"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_name" {
|
||||
description = "Name to use on IAM role created"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_use_name_prefix" {
|
||||
description = "Determines whether the IAM role name (`iam_role_name`) is used as a prefix"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "iam_role_path" {
|
||||
description = "IAM role path"
|
||||
type = string
|
||||
default = "/"
|
||||
}
|
||||
|
||||
variable "iam_role_description" {
|
||||
description = "Description of the role"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_max_session_duration" {
|
||||
description = "Maximum API session duration in seconds between 3600 and 43200"
|
||||
type = number
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_permissions_boundary" {
|
||||
description = "ARN of the policy that is used to set the permissions boundary for the IAM role"
|
||||
type = string
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_role_attach_cni_policy" {
|
||||
description = "Whether to attach the `AmazonEKS_CNI_Policy`/`AmazonEKS_CNI_IPv6_Policy` IAM policy to the IAM IAM role. WARNING: If set `false` the permissions must be assigned to the `aws-node` DaemonSet pods via another method or nodes will not be able to join the cluster"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
|
||||
variable "iam_role_additional_policies" {
|
||||
description = "Additional policies to be added to the IAM role"
|
||||
type = list(string)
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "iam_role_tags" {
|
||||
description = "A map of additional tags to add to the IAM role created"
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
|
||||
################################################################################
|
||||
# Node IAM Instance Profile
|
||||
################################################################################
|
||||
|
||||
variable "create_instance_profile" {
|
||||
description = "Whether to create an IAM instance profile"
|
||||
type = bool
|
||||
default = true
|
||||
}
|
||||
10
modules/karpenter/versions.tf
Normal file
10
modules/karpenter/versions.tf
Normal file
@@ -0,0 +1,10 @@
|
||||
terraform {
|
||||
required_version = ">= 0.13.1"
|
||||
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = ">= 3.72"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user