123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- data "aws_caller_identity" "current" {
- }
- data "aws_partition" "current" {
- }
- locals {
- account_id = data.aws_caller_identity.current.account_id
- partition = data.aws_partition.current.partition
- bucket_name = var.bucket_name == "" ? "${local.account_id}-${var.region}-cloudtrail" : var.bucket_name
- # Account IDs that will have access to stream CloudTrail logs
- account_ids = concat([local.account_id], var.allowed_account_ids)
- # Format account IDs into necessary resource lists.
- bucket_policy_put_resources = formatlist("${aws_s3_bucket.this.arn}/AWSLogs/%s/*", local.account_ids)
- kms_key_encrypt_resources = formatlist("arn:${local.partition}:cloudtrail:*:%s:trail/*", local.account_ids)
- }
- resource "aws_s3_bucket" "this" {
- bucket = local.bucket_name
- tags = var.tags
- lifecycle {
- prevent_destroy = true
- }
- }
- resource "aws_s3_bucket_lifecycle_configuration" "this" {
- bucket = aws_s3_bucket.this.id
- count = length(var.lifecycle_rules) > 0 ? 1 : 0 # handle the case of no lifecycle rules
- dynamic "rule" {
- for_each = var.lifecycle_rules
- content {
- id = rule.value.id
- status = rule.value.enabled == true ? "Enabled" : "Disabled"
-
- filter {
- prefix = lookup(rule.value, "prefix", null)
- }
- abort_incomplete_multipart_upload {
- days_after_initiation = lookup(rule.value, "abort_incomplete_multipart_upload_days", 0)
- }
- expiration {
- days = lookup(rule.value, "expiration", 2147483647)
- }
- noncurrent_version_expiration {
- noncurrent_days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
- }
- }
- }
- }
- resource "aws_s3_bucket_logging" "this" {
- bucket = aws_s3_bucket.this.id
- target_bucket = var.logging_bucket
- target_prefix = "${local.account_id}-${var.region}-cloudtrail/"
- }
- resource "aws_s3_bucket_versioning" "this" {
- bucket = aws_s3_bucket.this.id
- versioning_configuration {
- status = "Enabled"
- }
- }
- resource "aws_s3_bucket_acl" "this" {
- bucket = aws_s3_bucket.this.id
- acl = "private"
- }
- resource "aws_s3_bucket_server_side_encryption_configuration" "kinesis_firehose_s3_bucket" {
- bucket = aws_s3_bucket.this.id
- rule {
- apply_server_side_encryption_by_default {
- sse_algorithm = "aws:kms"
- kms_master_key_id = aws_kms_key.this.arn
- }
- }
- }
- resource "aws_s3_bucket_public_access_block" "this" {
- bucket = aws_s3_bucket.this.id
- block_public_acls = true
- block_public_policy = true
- ignore_public_acls = true
- restrict_public_buckets = true
- }
- data "aws_iam_policy_document" "this" {
- statement {
- actions = ["s3:GetBucketAcl"]
- effect = "Allow"
- resources = [aws_s3_bucket.this.arn]
- principals {
- type = "Service"
- identifiers = ["cloudtrail.amazonaws.com"]
- }
- }
- statement {
- actions = ["s3:PutObject"]
- effect = "Allow"
- resources = local.bucket_policy_put_resources
- condition {
- test = "StringEquals"
- variable = "s3:x-amz-acl"
- values = ["bucket-owner-full-control"]
- }
- principals {
- type = "Service"
- identifiers = ["cloudtrail.amazonaws.com"]
- }
- }
- }
- resource "aws_s3_bucket_policy" "this" {
- bucket = aws_s3_bucket.this.id
- policy = data.aws_iam_policy_document.this.json
- }
|