Skip to content

Commit 0b0ca66

Browse files
myerscfbryantbiggs
andauthored
feat: Use aws_service_principal data source for deriving IAM service prinicpals (#3539)
* fix: removed use of dns_suffix use the service prinipal name in policies Signed-off-by: Fred Myerscough <myerscf@amazon.co.uk> * fix: added missing data. accessor Signed-off-by: Fred Myerscough <myerscf@amazon.co.uk> * refactor: updates to address pr comments Signed-off-by: Fred Myerscough <myerscf@amazon.co.uk> * refactor: updates to address pr comments Signed-off-by: Fred Myerscough <myerscf@amazon.co.uk> * fix: Remove unused local var, run pre-commit --------- Signed-off-by: Fred Myerscough <myerscf@amazon.co.uk> Co-authored-by: Bryant Biggs <bryantbiggs@gmail.com>
1 parent 4c77bbc commit 0b0ca66

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

modules/karpenter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ No modules.
123123
| [aws_iam_policy_document.queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
124124
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
125125
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
126+
| [aws_service_principal.ec2](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/service_principal) | data source |
126127

127128
## Inputs
128129

modules/karpenter/main.tf

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ data "aws_region" "current" {
33

44
region = var.region
55
}
6+
67
data "aws_partition" "current" {
78
count = var.create ? 1 : 0
89
}
10+
911
data "aws_caller_identity" "current" {
1012
count = var.create ? 1 : 0
1113
}
1214

15+
data "aws_service_principal" "ec2" {
16+
count = var.create ? 1 : 0
17+
18+
service_name = "ec2"
19+
}
20+
1321
locals {
14-
account_id = try(data.aws_caller_identity.current[0].account_id, "")
15-
dns_suffix = try(data.aws_partition.current[0].dns_suffix, "")
16-
partition = try(data.aws_partition.current[0].partition, "")
17-
region = try(data.aws_region.current[0].region, "")
22+
account_id = try(data.aws_caller_identity.current[0].account_id, "")
23+
ec2_sp_name = try(data.aws_service_principal.ec2[0].name, "")
24+
partition = try(data.aws_partition.current[0].partition, "")
25+
region = try(data.aws_region.current[0].region, "")
1826
}
1927

2028
################################################################################
@@ -313,7 +321,7 @@ data "aws_iam_policy_document" "node_assume_role" {
313321

314322
principals {
315323
type = "Service"
316-
identifiers = ["ec2.${local.dns_suffix}"]
324+
identifiers = [local.ec2_sp_name]
317325
}
318326
}
319327
}

modules/karpenter/policy.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ data "aws_iam_policy_document" "controller" {
234234
condition {
235235
test = "StringEquals"
236236
variable = "iam:PassedToService"
237-
values = distinct(["ec2.${local.dns_suffix}", "ec2.amazonaws.com"])
237+
values = distinct([local.ec2_sp_name, "ec2.amazonaws.com"])
238238
}
239239
}
240240

outputs.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
locals {
2-
3-
}
4-
51
################################################################################
62
# Cluster
73
################################################################################

0 commit comments

Comments
 (0)