Skip to content

Commit

Permalink
add to policy for CW logs for org
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Nov 23, 2021
1 parent 3d13ad7 commit f0d9f88
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions cloudtrail/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ No modules.
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | AWS CloudTrail KMS ARN to be used for encrypting the ClouldTrail, S3 Bucket, and SQS | `string` | n/a | yes |
| <a name="input_kms_key_management_identifiers"></a> [kms\_key\_management\_identifiers](#input\_kms\_key\_management\_identifiers) | AWS IAM ARNs (roles, groups, users) for full access to the created KMS Key for this bucket | `list(string)` | `[]` | no |
| <a name="input_name"></a> [name](#input\_name) | Name to apply to Cloudtrail, S3, SNS and SQS | `string` | `null` | no |
| <a name="input_organization_id"></a> [organization\_id](#input\_organization\_id) | AWS Organization ID | `string` | `""` | no |
| <a name="input_override_prefixes"></a> [override\_prefixes](#input\_override\_prefixes) | Override built-in prefixes by component (efs, s3, ebs, kms, role, policy, security-group). This should be used primarily for common infrastructure things | `map(string)` | `{}` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | AWS Tags to apply to appropriate resources (S3, KMS). Do not include safeguard tags here, use the data\_safeguard field for such things. | `map(string)` | `{}` | no |

Expand Down
13 changes: 8 additions & 5 deletions cloudtrail/cloudwatch.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
locals {
cloudwatch_prefix = replace(aws_cloudwatch_log_group.this.arn, "/:\\*$/", "")
cloudwatch_suffix = format("%v_CloudTrail_%v", local.account_id, local.region)
cloudwatch_resources = join(":", list(local.cloudwatch_prefix, "log-stream", local.cloudwatch_suffix))
cloudwatch_prefix = replace(aws_cloudwatch_log_group.this.arn, "/:\\*$/", "")
cloudwatch_suffix = format("%v_CloudTrail_%v", local.account_id, local.region)
org_cloudwatch_suffix = format("o-%v_*", var.organization_id)
cloudwatch_resources = join(":", list(local.cloudwatch_prefix, "log-stream", local.cloudwatch_suffix))
org_cloudwatch_resources = var.enable_organization ? join(":", list(local.cloudwatch_prefix, "log-stream", local.org_cloudwatch_suffix)) : ""
resources = compact(concat(local.cloudwatch_resoures, local.org_cloudwatch_resources))
}

data "aws_iam_policy_document" "cloudwatch_policy" {
statement {
sid = "AWSCloudTrailCreateLogStream"
effect = "Allow"
actions = ["logs:CreateLogStream"]
resources = [local.cloudwatch_resources]
resources = [local.resources]
}

statement {
sid = "AWSCloudTrailPutLogEvents"
effect = "Allow"
actions = ["logs:PutLogEvents"]
resources = [local.cloudwatch_resources]
resources = [local.resources]
}
}

Expand Down
3 changes: 3 additions & 0 deletions cloudtrail/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,6 @@ locals {
data "aws_kms_key" "incoming_key" {
key_id = var.kms_key_arn
}

# data "aws_organizations_organization" "org" {}

7 changes: 6 additions & 1 deletion cloudtrail/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ variable "component_tags" {
default = { "s3" = {}, "kms" = {}, "ddb" = {} }
}


variable "enable_organization" {
description = "Enable CloudTrail as an organization trail. This will only work in the organization master account"
type = bool
default = false
}

variable "organization_id" {
description = "AWS Organization ID"
type = string
default = ""
}

0 comments on commit f0d9f88

Please sign in to comment.