Skip to content

Commit 7d5f78c

Browse files
authored
Merge pull request #13 from joe-niland/feat/data-resources-dependent-on-session-logging-enabled
2 parents b942a1b + 1633b7d commit 7d5f78c

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,21 @@ data "aws_iam_policy_document" "default" {
5656
}
5757

5858
data "aws_s3_bucket" "logs_bucket" {
59+
count = var.session_logging_enabled ? 1 : 0
5960
bucket = try(coalesce(var.session_logging_bucket_name, module.logs_bucket.bucket_id), "")
6061
}
6162

6263
# https://docs.aws.amazon.com/systems-manager/latest/userguide/getting-started-create-iam-instance-profile.html#create-iam-instance-profile-ssn-logging
6364
data "aws_iam_policy_document" "session_logging" {
65+
count = var.session_logging_enabled ? 1 : 0
6466

6567
statement {
6668
sid = "SSMAgentSessionAllowS3Logging"
6769
effect = "Allow"
6870
actions = [
6971
"s3:PutObject"
7072
]
71-
resources = ["${data.aws_s3_bucket.logs_bucket.arn}/*"]
73+
resources = ["${join("", data.aws_s3_bucket.logs_bucket.*.arn)}/*"]
7274
}
7375

7476
statement {
@@ -119,7 +121,7 @@ resource "aws_iam_role_policy" "session_logging" {
119121

120122
name = "${module.role_label.id}-session-logging"
121123
role = aws_iam_role.default.name
122-
policy = data.aws_iam_policy_document.session_logging.json
124+
policy = join("", data.aws_iam_policy_document.session_logging.*.json)
123125
}
124126

125127
resource "aws_iam_instance_profile" "default" {

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ output "role_id" {
2424
}
2525

2626
output "session_logging_bucket_id" {
27-
value = var.session_logging_enabled && var.session_logging_bucket_name == "" ? data.aws_s3_bucket.logs_bucket.id : ""
27+
value = var.session_logging_enabled && var.session_logging_bucket_name == "" ? join("", data.aws_s3_bucket.logs_bucket.*.id) : ""
2828
description = "The ID of the SSM Agent Session Logging S3 Bucket."
2929
}
3030

3131
output "session_logging_bucket_arn" {
32-
value = var.session_logging_enabled && var.session_logging_bucket_name == "" ? data.aws_s3_bucket.logs_bucket.arn : ""
32+
value = var.session_logging_enabled && var.session_logging_bucket_name == "" ? join("", data.aws_s3_bucket.logs_bucket.*.arn) : ""
3333
description = "The ARN of the SSM Agent Session Logging S3 Bucket."
3434
}

0 commit comments

Comments
 (0)