Add group 1 audit logging encryption imports

This commit is contained in:
2026-11-14 13:31:59 +09:00
commit 3b54c4eb84
16 changed files with 607 additions and 0 deletions
@@ -0,0 +1,20 @@
resource "aws_cloudtrail" "management_events" {
name = var.trail_name
s3_bucket_name = aws_s3_bucket.this["cloudtrail"].id
include_global_service_events = true
is_multi_region_trail = true
is_organization_trail = false
enable_log_file_validation = false
enable_logging = true
advanced_event_selector {
name = "Management events selector"
field_selector {
field = "eventCategory"
equals = ["Management"]
}
}
depends_on = [aws_s3_bucket_policy.this["cloudtrail"]]
}
@@ -0,0 +1,57 @@
resource "aws_cloudwatch_log_group" "this" {
for_each = var.log_groups
name = each.value.name
retention_in_days = each.value.retention_in_days
log_group_class = "STANDARD"
tags = each.value.tags
}
resource "aws_cloudwatch_dashboard" "cost_check" {
dashboard_name = var.dashboard_name
dashboard_body = jsonencode({
widgets = [{
type = "metric"
x = 0
y = 0
width = 23
height = 7
properties = {
view = "timeSeries"
stacked = true
metrics = [
["AWS/NATGateway", "BytesOutToSource", "AvailabilityZone", "ap-northeast-1a", "NatGatewayId", var.dashboard_nat_gateway_id],
[".", "BytesOutToDestination", ".", ".", ".", "."],
[".", "BytesInFromDestination", ".", ".", ".", "."],
[".", "BytesInFromSource", ".", ".", ".", "."],
[".", "BytesInFromDestination", ".", "ap-northeast-1c", ".", "."],
[".", "BytesInFromSource", ".", ".", ".", "."],
[".", "BytesOutToDestination", ".", ".", ".", "."],
[".", "BytesOutToSource", ".", ".", ".", "."],
]
region = var.region
title = "NAT Gayeway通信量"
}
}]
})
}
resource "aws_cloudwatch_metric_alarm" "billing" {
provider = aws.us_east_1
alarm_name = var.billing_alarm_name
alarm_description = "Created from CloudWatch console"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
metric_name = "EstimatedCharges"
namespace = "AWS/Billing"
period = 21600
statistic = "Maximum"
threshold = 16
actions_enabled = true
alarm_actions = [aws_sns_topic.notify_me.arn]
dimensions = {
Currency = "USD"
}
}
+34
View File
@@ -0,0 +1,34 @@
resource "aws_kms_key" "vault" {
description = "KMS for Vault"
key_usage = "ENCRYPT_DECRYPT"
customer_master_key_spec = "SYMMETRIC_DEFAULT"
enable_key_rotation = false
multi_region = false
policy = jsonencode({
Version = "2012-10-17"
Id = "key-consolepolicy-3"
Statement = [{
Sid = "Enable IAM User Permissions"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${var.account_id}:root"
}
Action = "kms:*"
Resource = "*"
}]
})
lifecycle {
prevent_destroy = true
ignore_changes = [
bypass_policy_lockout_safety_check,
deletion_window_in_days,
]
}
}
resource "aws_kms_alias" "vault" {
name = var.kms_alias_name
target_key_id = aws_kms_key.vault.key_id
}
+105
View File
@@ -0,0 +1,105 @@
locals {
buckets = {
obsidian = coalesce(var.bucket_names.obsidian, "${var.name_prefix}-obsidian-contents")
cloudtrail = coalesce(var.bucket_names.cloudtrail, "${var.name_prefix}-cloudtrail-logs")
flowlogs = coalesce(var.bucket_names.flowlogs, "${var.name_prefix}-flowlogs")
}
trail_arn = "arn:aws:cloudtrail:${var.region}:${var.account_id}:trail/${var.trail_name}"
sns_topic_arn = "arn:aws:sns:us-east-1:${var.account_id}:${var.sns_topic_name}"
bucket_policies = {
obsidian = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${var.account_id}:user/${var.account_id}-obsidian-contents"
}
Action = "s3:*"
Resource = [
"arn:aws:s3:::${local.buckets.obsidian}",
"arn:aws:s3:::${local.buckets.obsidian}/*",
]
}]
})
cloudtrail = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AWSCloudTrailAclCheck20150319-fbd4fef1-e790-49e5-8746-eb45d0ff5866"
Effect = "Allow"
Principal = { Service = "cloudtrail.amazonaws.com" }
Action = "s3:GetBucketAcl"
Resource = "arn:aws:s3:::${local.buckets.cloudtrail}"
Condition = {
StringEquals = { "AWS:SourceArn" = local.trail_arn }
}
},
{
Sid = "AWSCloudTrailWrite20150319-2640129e-451d-4772-b3b0-faea699af22a"
Effect = "Allow"
Principal = { Service = "cloudtrail.amazonaws.com" }
Action = "s3:PutObject"
Resource = "arn:aws:s3:::${local.buckets.cloudtrail}/AWSLogs/${var.account_id}/*"
Condition = {
StringEquals = {
"AWS:SourceArn" = local.trail_arn
"s3:x-amz-acl" = "bucket-owner-full-control"
}
}
},
]
})
flowlogs = jsonencode({
Version = "2012-10-17"
Id = "AWSLogDeliveryWrite20150319"
Statement = [
{
Sid = "AWSLogDeliveryWrite"
Effect = "Allow"
Principal = { Service = "delivery.logs.amazonaws.com" }
Action = "s3:PutObject"
Resource = "arn:aws:s3:::${local.buckets.flowlogs}/AWSLogs/${var.account_id}/*"
Condition = {
StringEquals = {
"s3:x-amz-acl" = "bucket-owner-full-control"
"aws:SourceAccount" = var.account_id
}
ArnLike = {
"aws:SourceArn" = "arn:aws:logs:${var.region}:${var.account_id}:*"
}
}
},
{
Sid = "AWSLogDeliveryAclCheck"
Effect = "Allow"
Principal = { Service = "delivery.logs.amazonaws.com" }
Action = "s3:GetBucketAcl"
Resource = "arn:aws:s3:::${local.buckets.flowlogs}"
Condition = {
StringEquals = {
"aws:SourceAccount" = var.account_id
}
ArnLike = {
"aws:SourceArn" = "arn:aws:logs:${var.region}:${var.account_id}:*"
}
}
},
]
})
}
lifecycle_rules = {
cloudtrail = {
id = "DeleteAfter3months"
expiration_days = 90
}
flowlogs = {
id = "Housekeep"
expiration_days = 7
}
}
}
+80
View File
@@ -0,0 +1,80 @@
resource "aws_s3_bucket" "this" {
for_each = local.buckets
bucket = each.value
force_destroy = false
tags = lookup(var.bucket_tags, each.key, {})
lifecycle {
prevent_destroy = true
}
}
resource "aws_s3_bucket_versioning" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
versioning_configuration {
status = "Disabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
bucket_key_enabled = each.key != "cloudtrail"
blocked_encryption_types = ["SSE-C"]
}
}
resource "aws_s3_bucket_public_access_block" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_ownership_controls" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_policy" "this" {
for_each = local.buckets
bucket = aws_s3_bucket.this[each.key].id
policy = local.bucket_policies[each.key]
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
for_each = local.lifecycle_rules
bucket = aws_s3_bucket.this[each.key].id
transition_default_minimum_object_size = "all_storage_classes_128K"
rule {
id = each.value.id
status = "Enabled"
expiration {
days = each.value.expiration_days
}
}
}
+5
View File
@@ -0,0 +1,5 @@
resource "aws_sns_topic" "notify_me" {
provider = aws.us_east_1
name = var.sns_topic_name
}
@@ -0,0 +1,69 @@
variable "region" {
description = "Primary AWS region for regional resources."
type = string
}
variable "name_prefix" {
description = "Prefix available to callers that create an isolated test copy."
type = string
}
variable "account_id" {
description = "AWS account ID used to construct existing policies and ARNs."
type = string
}
variable "bucket_names" {
description = "Optional existing bucket names. Omitted values are derived from name_prefix for isolated tests."
type = object({
obsidian = optional(string)
cloudtrail = optional(string)
flowlogs = optional(string)
})
default = {}
}
variable "bucket_tags" {
description = "Existing tags keyed by the logical bucket name."
type = map(map(string))
default = {}
}
variable "trail_name" {
description = "CloudTrail trail name."
type = string
}
variable "kms_alias_name" {
description = "Alias of the customer managed Vault key."
type = string
}
variable "log_groups" {
description = "CloudWatch log groups and their retention in days. Null means never expire."
type = map(object({
name = string
retention_in_days = optional(number)
tags = optional(map(string), {})
}))
}
variable "dashboard_name" {
description = "CloudWatch dashboard name."
type = string
}
variable "dashboard_nat_gateway_id" {
description = "NAT Gateway ID referenced by the existing dashboard widget."
type = string
}
variable "sns_topic_name" {
description = "SNS topic name in us-east-1."
type = string
}
variable "billing_alarm_name" {
description = "CloudWatch billing alarm name in us-east-1."
type = string
}
@@ -0,0 +1,11 @@
terraform {
required_version = "= 1.17.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.73"
configuration_aliases = [aws.us_east_1]
}
}
}