Skip to content

Commit 0c9148e

Browse files
committed
feat: Use aws_iam_role_policies_exclusive
1 parent c60b70f commit 0c9148e

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

main.tf

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -411,32 +411,38 @@ resource "aws_iam_role" "this" {
411411
permissions_boundary = var.iam_role_permissions_boundary
412412
force_detach_policies = true
413413

414-
# https://github.com/terraform-aws-modules/terraform-aws-eks/issues/920
415-
# Resources running on the cluster are still generating logs when destroying the module resources
416-
# which results in the log group being re-created even after Terraform destroys it. Removing the
417-
# ability for the cluster role to create the log group prevents this log group from being re-created
418-
# outside of Terraform due to services still generating logs during destroy process
419-
dynamic "inline_policy" {
420-
for_each = var.create_cloudwatch_log_group ? [1] : []
421-
content {
422-
name = local.iam_role_name
423-
424-
policy = jsonencode({
425-
Version = "2012-10-17"
426-
Statement = [
427-
{
428-
Action = ["logs:CreateLogGroup"]
429-
Effect = "Deny"
430-
Resource = "*"
431-
},
432-
]
433-
})
434-
}
435-
}
436-
437414
tags = merge(var.tags, var.iam_role_tags)
438415
}
439416

417+
# https://github.com/terraform-aws-modules/terraform-aws-eks/issues/920
418+
# Resources running on the cluster are still generating logs when destroying the module resources
419+
# which results in the log group being re-created even after Terraform destroys it. Removing the
420+
# ability for the cluster role to create the log group prevents this log group from being re-created
421+
# outside of Terraform due to services still generating logs during destroy process
422+
resource "aws_iam_role_policy" "this" {
423+
count = local.create_iam_role && var.create_cloudwatch_log_group ? 1 : 0
424+
425+
name = local.iam_role_name
426+
role = aws_iam_role.this[0].name
427+
policy = jsonencode({
428+
Version = "2012-10-17"
429+
Statement = [
430+
{
431+
Action = ["logs:CreateLogGroup"]
432+
Effect = "Deny"
433+
Resource = "*"
434+
},
435+
]
436+
})
437+
}
438+
439+
resource "aws_iam_role_policies_exclusive" "this" {
440+
count = local.create_iam_role && var.create_cloudwatch_log_group ? 1 : 0
441+
442+
role_name = aws_iam_role.this[0].name
443+
policy_names = [local.iam_role_name]
444+
}
445+
440446
# Policies attached ref https://docs.aws.amazon.com/eks/latest/userguide/service_IAM_role.html
441447
resource "aws_iam_role_policy_attachment" "this" {
442448
for_each = { for k, v in {

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 5.61"
7+
version = ">= 5.68"
88
}
99
tls = {
1010
source = "hashicorp/tls"

0 commit comments

Comments
 (0)