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
+7
View File
@@ -0,0 +1,7 @@
.terraform/
*.tfstate
*.tfstate.*
*.tfplan
backend.hcl
crash.log
crash.*.log
+26
View File
@@ -0,0 +1,26 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "6.73.0"
constraints = "~> 6.73"
hashes = [
"h1:87t8o/A0y2RE3PtCLx/BiASSrDrKSETYTF9jRvKc7aw=",
"zh:0f7403264fbfbcad526a5ddb9553be6ab12dba14e5901d0c2ccabc38d9ccf9a0",
"zh:26be4e8afc94ef29c98b8219dc49b19ce21a04d3f2b4e5be6116e9526ece0355",
"zh:2ba661a4a782fba053c411ce4d828dc148d7ed3998d073a28370a559400b1578",
"zh:2c422d0edd37bf7470c84f1d1f76487f30948768878204a65a18189bf878467a",
"zh:49f9d1486d48033d298977f7c288f8bbb22bb246f00926d1cb8b0bb98985451c",
"zh:4f946cf11667e1f3bce53fd5ad92eccf47fafaa1bbaa68c742173db61773a1da",
"zh:70beb5e13ddcdbeecb2a78be2b75d09a0fc6ce7e69b574c84a90924fd4262406",
"zh:8c5b974104ff9a887b29fdb2700cdbb5e6fd2eceef1cc3b28f523beda4fd8dfb",
"zh:9252068667bc6528ee6e83e837f3c2f901756136a33442455d61c6fa34e1160a",
"zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
"zh:9d086b792598b43e87502d47c4a3f388465b3a21a6a6b2783e48c89dee1f0543",
"zh:b5e6e77a43c1655285d99145b2cf0cc2da7a6c6ea3531d000cf8cca2c717693b",
"zh:b7ebe893e5ec76b362b61ffb28c67b40d890e0f59f5f2f8829765b16ee3e53b0",
"zh:c8c1e186839ad3cba37b427181af1e62eaf06c6daa5dca29105e1981dfaeb4c2",
"zh:e988377e7aea51e1fefe0b52c78bc725a11327051e20a71f81069191ee077d2d",
"zh:ee9188027919459a48bf9bac8f8f1dd172f5be198b88c2c47789b6daa8bb8bcb",
]
}
+8
View File
@@ -0,0 +1,8 @@
terraform {
backend "s3" {
key = "prod/terraform.tfstate"
region = "ap-northeast-1"
encrypt = true
use_lockfile = true
}
}
+80
View File
@@ -0,0 +1,80 @@
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket.this[each.key]
id = each.value
}
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket_versioning.this[each.key]
id = each.value
}
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket_server_side_encryption_configuration.this[each.key]
id = each.value
}
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket_public_access_block.this[each.key]
id = each.value
}
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket_ownership_controls.this[each.key]
id = each.value
}
import {
for_each = local.bucket_names
to = module.audit_logging_encryption.aws_s3_bucket_policy.this[each.key]
id = each.value
}
import {
for_each = {
cloudtrail = local.bucket_names.cloudtrail
flowlogs = local.bucket_names.flowlogs
}
to = module.audit_logging_encryption.aws_s3_bucket_lifecycle_configuration.this[each.key]
id = each.value
}
import {
to = module.audit_logging_encryption.aws_kms_key.vault
id = "46975f6d-881a-4188-8fdf-b641e1258a85"
}
import {
to = module.audit_logging_encryption.aws_kms_alias.vault
id = "alias/vault-kms"
}
import {
for_each = local.log_groups
to = module.audit_logging_encryption.aws_cloudwatch_log_group.this[each.key]
id = each.value.name
}
import {
to = module.audit_logging_encryption.aws_cloudwatch_dashboard.cost_check
id = "CostCheck"
}
import {
to = module.audit_logging_encryption.aws_sns_topic.notify_me
id = "arn:aws:sns:us-east-1:${var.account_id}:NotifyMe"
}
import {
to = module.audit_logging_encryption.aws_cloudwatch_metric_alarm.billing
id = "awsbilling-AWS-Service-Charges-total"
}
import {
to = module.audit_logging_encryption.aws_cloudtrail.management_events
id = "arn:aws:cloudtrail:${var.region}:${var.account_id}:trail/management-events"
}
+58
View File
@@ -0,0 +1,58 @@
locals {
bucket_names = {
obsidian = "${var.account_id}-obsidian-contents"
cloudtrail = "aws-cloudtrail-logs-${var.account_id}-97f3dbeb"
flowlogs = "kazu634-flowlogs"
}
log_groups = {
ecs_a49f = {
name = "/aws/ecs/default/nginx-a49f-48d8"
tags = {
AmazonECSManaged = "true"
category = "ecs-test"
}
}
ecs_d2a0 = {
name = "/aws/ecs/default/nginx-d2a0-c9dc"
tags = {
AmazonECSManaged = "true"
category = "ecs-test"
}
}
vpc_flowlogs = {
name = "nw-vpcflowlogs"
retention_in_days = 14
}
}
}
module "audit_logging_encryption" {
source = "../../modules/audit_logging_encryption"
providers = {
aws = aws
aws.us_east_1 = aws.us_east_1
}
region = var.region
name_prefix = var.name_prefix
account_id = var.account_id
bucket_names = local.bucket_names
bucket_tags = {
obsidian = {
Env = "Obsidian"
}
}
trail_name = "management-events"
kms_alias_name = "alias/vault-kms"
log_groups = local.log_groups
dashboard_name = "CostCheck"
dashboard_nat_gateway_id = "nat-13131172004a3e65b"
sns_topic_name = "NotifyMe"
billing_alarm_name = "awsbilling-AWS-Service-Charges-total"
}
+10
View File
@@ -0,0 +1,10 @@
provider "aws" {
profile = var.aws_profile
region = var.region
}
provider "aws" {
alias = "us_east_1"
profile = var.aws_profile
region = "us-east-1"
}
+27
View File
@@ -0,0 +1,27 @@
variable "aws_profile" {
description = "Read-only AWS CLI profile used for plan."
type = string
default = "aws-terraform-ro"
}
variable "region" {
description = "Primary production region."
type = string
default = "ap-northeast-1"
}
variable "name_prefix" {
description = "Name prefix passed to the reusable module."
type = string
default = "prod"
}
variable "account_id" {
description = "AWS account ID. Supply with TF_VAR_account_id; do not commit a tfvars file."
type = string
validation {
condition = can(regex("^[0-9]{12}$", var.account_id))
error_message = "account_id must contain exactly 12 digits."
}
}
+10
View File
@@ -0,0 +1,10 @@
terraform {
required_version = "= 1.17.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.73"
}
}
}
@@ -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]
}
}
}