Skip to content

Commit f54bd30

Browse files
fix: Correct remote access variable for security groups and add example for additional IAM policies (#1766)
Co-authored-by: Anton Babenko <[email protected]>
1 parent 7babe87 commit f54bd30

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ Terraform module which creates AWS EKS (Kubernetes) resources
1818
- Support for providing maps of node groups/Fargate profiles to the cluster module definition or use separate node group/Fargate profile sub-modules
1919
- Provisions to provide node group/Fargate profile "default" settings - useful for when creating multiple node groups/Fargate profiles where you want to set a common set of configurations once, and then individual control only select features
2020

21+
### ℹ️ `Error: Invalid for_each argument ...`
22+
23+
Users may encounter an error such as `Error: Invalid for_each argument - The "for_each" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply ...`
24+
25+
This error is due to an upstream issue with [Terraform core](https://github.com/hashicorp/terraform/issues/4149). There are two potential options you can take to help mitigate this issue:
26+
27+
1. Create the dependent resources before the cluster => `terraform apply -target <your policy or your security group>` and then `terraform apply` for the cluster (or other similar means to just ensure the referenced resources exist before creating the cluster)
28+
- Note: this is the route users will have to take for adding additonal security groups to nodes since there isn't a separate "security group attachment" resource
29+
2. For addtional IAM policies, users can attach the policies outside of the cluster definition as demonstrated below
30+
31+
```hcl
32+
resource "aws_iam_role_policy_attachment" "additional" {
33+
for_each = module.eks.eks_managed_node_groups
34+
# you could also do the following or any comibination:
35+
# for_each = merge(
36+
# module.eks.eks_managed_node_groups,
37+
# module.eks.self_managed_node_group,
38+
# module.eks.fargate_profile,
39+
# )
40+
41+
# This policy does not have to exist at the time of cluster creation. Terraform can
42+
# deduce the proper order of its creation to avoid errors during creation
43+
policy_arn = aws_iam_policy.node_additional.arn
44+
role = each.value.iam_role_name
45+
}
46+
```
47+
48+
The tl;dr for this issue is that the Terraform resource passed into the modules map definition *must* be known before you can apply the EKS module. The variables this potentially affects are:
49+
50+
- `cluster_security_group_additional_rules` (i.e. - referencing an external security group resource in a rule)
51+
- `node_security_group_additional_rules` (i.e. - referencing an external security group resource in a rule)
52+
- `iam_role_additional_policies` (i.e. - referencing an external policy resource)
53+
2154
## Usage
2255

2356
```hcl

examples/eks_managed_node_group/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ Note that this example may create resources which cost money. Run `terraform des
5353

5454
| Name | Type |
5555
|------|------|
56+
| [aws_iam_policy.node_additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
57+
| [aws_iam_role_policy_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
5658
| [aws_key_pair.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/key_pair) | resource |
5759
| [aws_kms_key.ebs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
5860
| [aws_kms_key.eks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
5961
| [aws_launch_template.external](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource |
6062
| [aws_security_group.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
63+
| [aws_security_group.remote_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
6164
| [null_resource.patch](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
6265
| [tls_private_key.this](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource |
6366
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |

examples/eks_managed_node_group/main.tf

+58-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ module "eks" {
7676

7777
# Remote access cannot be specified with a launch template
7878
remote_access = {
79-
ec2_ssh_key = aws_key_pair.this.key_name
79+
ec2_ssh_key = aws_key_pair.this.key_name
80+
source_security_group_ids = [aws_security_group.remote_access.id]
8081
}
8182
}
8283

@@ -269,6 +270,18 @@ module "eks" {
269270
tags = local.tags
270271
}
271272

273+
# References to resources that do not exist yet when creating a cluster will cause a plan failure due to https://github.com/hashicorp/terraform/issues/4149
274+
# There are two options users can take
275+
# 1. Create the dependent resources before the cluster => `terraform apply -target <your policy or your security group> and then `terraform apply`
276+
# Note: this is the route users will have to take for adding additonal security groups to nodes since there isn't a separate "security group attachment" resource
277+
# 2. For addtional IAM policies, users can attach the policies outside of the cluster definition as demonstrated below
278+
resource "aws_iam_role_policy_attachment" "additional" {
279+
for_each = module.eks.eks_managed_node_groups
280+
281+
policy_arn = aws_iam_policy.node_additional.arn
282+
role = each.value.iam_role_name
283+
}
284+
272285
################################################################################
273286
# aws-auth configmap
274287
# Only EKS managed node groups automatically add roles to aws-auth configmap
@@ -529,3 +542,47 @@ resource "aws_key_pair" "this" {
529542

530543
tags = local.tags
531544
}
545+
546+
resource "aws_security_group" "remote_access" {
547+
name_prefix = "${local.name}-remote-access"
548+
description = "Allow remote SSH access"
549+
vpc_id = module.vpc.vpc_id
550+
551+
ingress {
552+
description = "SSH access"
553+
from_port = 22
554+
to_port = 22
555+
protocol = "tcp"
556+
cidr_blocks = ["10.0.0.0/8"]
557+
}
558+
559+
egress {
560+
from_port = 0
561+
to_port = 0
562+
protocol = "-1"
563+
cidr_blocks = ["0.0.0.0/0"]
564+
ipv6_cidr_blocks = ["::/0"]
565+
}
566+
567+
tags = local.tags
568+
}
569+
570+
resource "aws_iam_policy" "node_additional" {
571+
name = "${local.name}-additional"
572+
description = "Example usage of node additional policy"
573+
574+
policy = jsonencode({
575+
Version = "2012-10-17"
576+
Statement = [
577+
{
578+
Action = [
579+
"ec2:Describe*",
580+
]
581+
Effect = "Allow"
582+
Resource = "*"
583+
},
584+
]
585+
})
586+
587+
tags = local.tags
588+
}

modules/eks-managed-node-group/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module "eks_managed_node_group" {
139139
| <a name="input_post_bootstrap_user_data"></a> [post\_bootstrap\_user\_data](#input\_post\_bootstrap\_user\_data) | User data that is appended to the user data script after of the EKS bootstrap script. Not used when `platform` = `bottlerocket` | `string` | `""` | no |
140140
| <a name="input_pre_bootstrap_user_data"></a> [pre\_bootstrap\_user\_data](#input\_pre\_bootstrap\_user\_data) | User data that is injected into the user data script ahead of the EKS bootstrap script. Not used when `platform` = `bottlerocket` | `string` | `""` | no |
141141
| <a name="input_ram_disk_id"></a> [ram\_disk\_id](#input\_ram\_disk\_id) | The ID of the ram disk | `string` | `null` | no |
142-
| <a name="input_remote_access"></a> [remote\_access](#input\_remote\_access) | Configuration block with remote access settings | `map(string)` | `{}` | no |
142+
| <a name="input_remote_access"></a> [remote\_access](#input\_remote\_access) | Configuration block with remote access settings | `any` | `{}` | no |
143143
| <a name="input_security_group_description"></a> [security\_group\_description](#input\_security\_group\_description) | Description for the security group created | `string` | `"EKS managed node group security group"` | no |
144144
| <a name="input_security_group_name"></a> [security\_group\_name](#input\_security\_group\_name) | Name to use on security group created | `string` | `null` | no |
145145
| <a name="input_security_group_rules"></a> [security\_group\_rules](#input\_security\_group\_rules) | List of security group rules to add to the security group created | `any` | `{}` | no |

modules/eks-managed-node-group/variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ variable "launch_template_version" {
334334

335335
variable "remote_access" {
336336
description = "Configuration block with remote access settings"
337-
type = map(string)
337+
type = any
338338
default = {}
339339
}
340340

0 commit comments

Comments
 (0)