Skip to content

Commit 8de02b9

Browse files
authored
fix: Update examples to show integration and usage of new IRSA submodule (#1882)
1 parent 8993d85 commit 8de02b9

6 files changed

Lines changed: 135 additions & 104 deletions

File tree

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,89 @@ module "eks" {
200200
}
201201
```
202202

203+
### IRSA Integration
204+
205+
An [IAM role for service accounts](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html) module has been created to work in conjunction with the EKS module. The [`iam-role-for-service-accounts`](https://github.com/terraform-aws-modules/terraform-aws-iam/tree/master/modules/iam-role-for-service-accounts-eks) module has a set of pre-defined IAM policies for common addons/controllers/custom resources to allow users to quickly enable common integrations. Check [`policy.tf`](https://github.com/terraform-aws-modules/terraform-aws-iam/blob/master/modules/iam-role-for-service-accounts-eks/policies.tf) for a list of the policies currently supported. A example of this integration is shown below, and more can be found in the [`iam-role-for-service-accounts`](https://github.com/terraform-aws-modules/terraform-aws-iam/blob/master/examples/iam-role-for-service-accounts-eks/main.tf example directory):
206+
207+
```hcl
208+
module "eks" {
209+
source = "terraform-aws-modules/eks/aws"
210+
211+
cluster_name = "example"
212+
cluster_version = "1.21"
213+
214+
cluster_addons = {
215+
vpc-cni = {
216+
resolve_conflicts = "OVERWRITE"
217+
service_account_role_arn = module.vpc_cni_irsa.iam_role_arn
218+
}
219+
}
220+
221+
vpc_id = "vpc-1234556abcdef"
222+
subnet_ids = ["subnet-abcde012", "subnet-bcde012a", "subnet-fghi345a"]
223+
224+
eks_managed_node_group_defaults = {
225+
# We are using the IRSA created below for permissions
226+
# This is a better practice as well so that the nodes do not have the permission,
227+
# only the VPC CNI addon will have the permission
228+
iam_role_attach_cni_policy = false
229+
}
230+
231+
eks_managed_node_groups = {
232+
default = {}
233+
}
234+
235+
tags = {
236+
Environment = "dev"
237+
Terraform = "true"
238+
}
239+
}
240+
241+
module "vpc_cni_irsa" {
242+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
243+
244+
role_name = "vpc_cni"
245+
attach_vpc_cni_policy = true
246+
vpc_cni_enable_ipv4 = true
247+
248+
oidc_providers = {
249+
main = {
250+
provider_arn = module.eks.oidc_provider_arn
251+
namespace_service_accounts = ["kube-system:aws-node"]
252+
}
253+
}
254+
255+
tags = {
256+
Environment = "dev"
257+
Terraform = "true"
258+
}
259+
}
260+
261+
module "karpenter_irsa" {
262+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
263+
264+
role_name = "karpenter_controller"
265+
attach_karpenter_controller_policy = true
266+
267+
karpenter_controller_cluster_ids = [module.eks.cluster_id]
268+
karpenter_controller_node_iam_role_arns = [
269+
module.eks.eks_managed_node_groups["default"].iam_role_arn
270+
]
271+
272+
oidc_providers = {
273+
main = {
274+
provider_arn = module.eks.oidc_provider_arn
275+
namespace_service_accounts = ["karpenter:karpenter"]
276+
}
277+
}
278+
279+
tags = {
280+
Environment = "dev"
281+
Terraform = "true"
282+
}
283+
}
284+
```
285+
203286
## Node Group Configuration
204287

205288
⚠️ The configurations shown below are referenced from within the root EKS module; there will be slight differences in the default values provided when compared to the underlying sub-modules (`eks-managed-node-group`, `self-managed-node-group`, and `fargate-profile`).

examples/eks_managed_node_group/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Note that this example may create resources which cost money. Run `terraform des
7575
|------|--------|---------|
7676
| <a name="module_eks"></a> [eks](#module\_eks) | ../.. | n/a |
7777
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
78+
| <a name="module_vpc_cni_irsa"></a> [vpc\_cni\_irsa](#module\_vpc\_cni\_irsa) | terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks | ~> 4.12 |
7879

7980
## Resources
8081

examples/eks_managed_node_group/main.tf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ module "eks" {
2929
cluster_endpoint_public_access = true
3030

3131
# IPV6
32-
cluster_ip_family = "ipv6"
33-
create_cni_ipv6_iam_policy = true
32+
cluster_ip_family = "ipv6"
3433

3534
cluster_addons = {
3635
coredns = {
3736
resolve_conflicts = "OVERWRITE"
3837
}
3938
kube-proxy = {}
4039
vpc-cni = {
41-
resolve_conflicts = "OVERWRITE"
40+
resolve_conflicts = "OVERWRITE"
41+
service_account_role_arn = module.vpc_cni_irsa.iam_role_arn
4242
}
4343
}
4444

@@ -87,6 +87,9 @@ module "eks" {
8787
ami_type = "AL2_x86_64"
8888
disk_size = 50
8989
instance_types = ["m6i.large", "m5.large", "m5n.large", "m5zn.large"]
90+
91+
# We are using the IRSA created below for permissions
92+
iam_role_attach_cni_policy = false
9093
}
9194

9295
eks_managed_node_groups = {
@@ -421,6 +424,24 @@ module "vpc" {
421424
tags = local.tags
422425
}
423426

427+
module "vpc_cni_irsa" {
428+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
429+
version = "~> 4.12"
430+
431+
role_name_prefix = "VPC-CNI-IRSA"
432+
attach_vpc_cni_policy = true
433+
vpc_cni_enable_ipv6 = true
434+
435+
oidc_providers = {
436+
main = {
437+
provider_arn = module.eks.oidc_provider_arn
438+
namespace_service_accounts = ["kube-system:aws-node"]
439+
}
440+
}
441+
442+
tags = local.tags
443+
}
444+
424445
resource "aws_security_group" "additional" {
425446
name_prefix = "${local.name}-additional"
426447
vpc_id = module.vpc.vpc_id

examples/irsa_autoscale_refresh/README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ Note that this example may create resources which cost money. Run `terraform des
4040

4141
| Name | Source | Version |
4242
|------|--------|---------|
43-
| <a name="module_aws_node_termination_handler_role"></a> [aws\_node\_termination\_handler\_role](#module\_aws\_node\_termination\_handler\_role) | terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc | ~> 4.0 |
4443
| <a name="module_aws_node_termination_handler_sqs"></a> [aws\_node\_termination\_handler\_sqs](#module\_aws\_node\_termination\_handler\_sqs) | terraform-aws-modules/sqs/aws | ~> 3.0 |
44+
| <a name="module_cluster_autoscaler_irsa"></a> [cluster\_autoscaler\_irsa](#module\_cluster\_autoscaler\_irsa) | terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks | ~> 4.12 |
4545
| <a name="module_eks"></a> [eks](#module\_eks) | ../.. | n/a |
46-
| <a name="module_iam_assumable_role_cluster_autoscaler"></a> [iam\_assumable\_role\_cluster\_autoscaler](#module\_iam\_assumable\_role\_cluster\_autoscaler) | terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc | ~> 4.0 |
46+
| <a name="module_node_termination_handler_irsa"></a> [node\_termination\_handler\_irsa](#module\_node\_termination\_handler\_irsa) | terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks | ~> 4.12 |
4747
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 3.0 |
4848

4949
## Resources
@@ -55,17 +55,13 @@ Note that this example may create resources which cost money. Run `terraform des
5555
| [aws_cloudwatch_event_rule.aws_node_termination_handler_spot](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
5656
| [aws_cloudwatch_event_target.aws_node_termination_handler_asg](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
5757
| [aws_cloudwatch_event_target.aws_node_termination_handler_spot](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
58-
| [aws_iam_policy.aws_node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
59-
| [aws_iam_policy.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
6058
| [helm_release.aws_node_termination_handler](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
6159
| [helm_release.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
6260
| [null_resource.apply](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6361
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
6462
| [aws_eks_cluster_auth.cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |
6563
| [aws_eks_cluster_auth.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/eks_cluster_auth) | data source |
66-
| [aws_iam_policy_document.aws_node_termination_handler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
6764
| [aws_iam_policy_document.aws_node_termination_handler_sqs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
68-
| [aws_iam_policy_document.cluster_autoscaler](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
6965

7066
## Inputs
7167

examples/irsa_autoscale_refresh/charts.tf

Lines changed: 24 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ resource "helm_release" "cluster_autoscaler" {
3232

3333
set {
3434
name = "rbac.serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
35-
value = module.iam_assumable_role_cluster_autoscaler.iam_role_arn
35+
value = module.cluster_autoscaler_irsa.iam_role_arn
3636
type = "string"
3737
}
3838

@@ -57,63 +57,24 @@ resource "helm_release" "cluster_autoscaler" {
5757
]
5858
}
5959

60-
module "iam_assumable_role_cluster_autoscaler" {
61-
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
62-
version = "~> 4.0"
60+
module "cluster_autoscaler_irsa" {
61+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
62+
version = "~> 4.12"
6363

64-
create_role = true
6564
role_name_prefix = "cluster-autoscaler"
6665
role_description = "IRSA role for cluster autoscaler"
6766

68-
provider_url = replace(module.eks.cluster_oidc_issuer_url, "https://", "")
69-
role_policy_arns = [aws_iam_policy.cluster_autoscaler.arn]
70-
oidc_fully_qualified_subjects = ["system:serviceaccount:kube-system:cluster-autoscaler-aws"]
71-
oidc_fully_qualified_audiences = ["sts.amazonaws.com"]
67+
attach_cluster_autoscaler_policy = true
68+
cluster_autoscaler_cluster_ids = [module.eks.cluster_id]
7269

73-
tags = local.tags
74-
}
75-
76-
resource "aws_iam_policy" "cluster_autoscaler" {
77-
name = "KarpenterControllerPolicy-refresh"
78-
policy = data.aws_iam_policy_document.cluster_autoscaler.json
79-
80-
tags = local.tags
81-
}
82-
83-
data "aws_iam_policy_document" "cluster_autoscaler" {
84-
statement {
85-
sid = "clusterAutoscalerAll"
86-
actions = [
87-
"autoscaling:DescribeAutoScalingGroups",
88-
"autoscaling:DescribeAutoScalingInstances",
89-
"autoscaling:DescribeLaunchConfigurations",
90-
"autoscaling:DescribeTags",
91-
"ec2:DescribeLaunchTemplateVersions",
92-
]
93-
resources = ["*"]
94-
}
95-
96-
statement {
97-
sid = "clusterAutoscalerOwn"
98-
actions = [
99-
"autoscaling:SetDesiredCapacity",
100-
"autoscaling:TerminateInstanceInAutoScalingGroup",
101-
"autoscaling:UpdateAutoScalingGroup",
102-
]
103-
resources = ["*"]
104-
105-
condition {
106-
test = "StringEquals"
107-
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/${module.eks.cluster_id}"
108-
values = ["owned"]
109-
}
110-
111-
condition {
112-
test = "StringEquals"
113-
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled"
114-
values = ["true"]
70+
oidc_providers = {
71+
main = {
72+
provider_arn = module.eks.oidc_provider_arn
73+
namespace_service_accounts = ["kube-system:cluster-autoscaler-aws"]
11574
}
11675
}
76+
77+
tags = local.tags
11778
}
11879

11980
################################################################################
@@ -142,7 +103,7 @@ resource "helm_release" "aws_node_termination_handler" {
142103

143104
set {
144105
name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
145-
value = module.aws_node_termination_handler_role.iam_role_arn
106+
value = module.node_termination_handler_irsa.iam_role_arn
146107
type = "string"
147108
}
148109

@@ -172,51 +133,24 @@ resource "helm_release" "aws_node_termination_handler" {
172133
]
173134
}
174135

175-
module "aws_node_termination_handler_role" {
176-
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc"
177-
version = "~> 4.0"
136+
module "node_termination_handler_irsa" {
137+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
138+
version = "~> 4.12"
178139

179-
create_role = true
180140
role_name_prefix = "node-termination-handler"
181141
role_description = "IRSA role for node termination handler"
182142

183-
provider_url = replace(module.eks.cluster_oidc_issuer_url, "https://", "")
184-
role_policy_arns = [aws_iam_policy.aws_node_termination_handler.arn]
185-
oidc_fully_qualified_subjects = ["system:serviceaccount:kube-system:aws-node-termination-handler"]
186-
oidc_fully_qualified_audiences = ["sts.amazonaws.com"]
143+
attach_node_termination_handler_policy = true
144+
node_termination_handler_sqs_queue_arns = [module.aws_node_termination_handler_sqs.sqs_queue_arn]
187145

188-
tags = local.tags
189-
}
190-
191-
resource "aws_iam_policy" "aws_node_termination_handler" {
192-
name = "${local.name}-aws-node-termination-handler"
193-
policy = data.aws_iam_policy_document.aws_node_termination_handler.json
194-
195-
tags = local.tags
196-
}
197-
198-
data "aws_iam_policy_document" "aws_node_termination_handler" {
199-
statement {
200-
actions = [
201-
"ec2:DescribeInstances",
202-
"autoscaling:DescribeAutoScalingInstances",
203-
"autoscaling:DescribeTags",
204-
]
205-
resources = ["*"]
206-
}
207-
208-
statement {
209-
actions = ["autoscaling:CompleteLifecycleAction"]
210-
resources = [for group in module.eks.self_managed_node_groups : group.autoscaling_group_arn]
146+
oidc_providers = {
147+
main = {
148+
provider_arn = module.eks.oidc_provider_arn
149+
namespace_service_accounts = ["kube-system:aws-node-termination-handler"]
150+
}
211151
}
212152

213-
statement {
214-
actions = [
215-
"sqs:DeleteMessage",
216-
"sqs:ReceiveMessage"
217-
]
218-
resources = [module.aws_node_termination_handler_sqs.sqs_queue_arn]
219-
}
153+
tags = local.tags
220154
}
221155

222156
module "aws_node_termination_handler_sqs" {

main.tf

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ data "aws_iam_policy_document" "assume_role_policy" {
183183

184184
principals {
185185
type = "Service"
186-
identifiers = ["eks.amazonaws.com"]
186+
identifiers = ["eks.${data.aws_partition.current.dns_suffix}"]
187187
}
188188
}
189189
}
@@ -234,10 +234,6 @@ resource "aws_eks_addon" "this" {
234234
]
235235
}
236236

237-
# Note: if an addon needs to be provisioned ahead of a node group users will
238-
# need to create the addon outside of this module until a 2nd addon resource is added
239-
# to the module (here) that is not dependent on node groups
240-
# Or if addon management improves, this dependency can be removed https://github.com/aws/containers-roadmap/issues/1389
241237
depends_on = [
242238
module.fargate_profile,
243239
module.eks_managed_node_group,

0 commit comments

Comments
 (0)