From d47e06df0cc8f806194692fa22a7b101522769b8 Mon Sep 17 00:00:00 2001 From: xpk Date: Mon, 5 Sep 2022 13:52:21 +0800 Subject: [PATCH] UPD: updated several modules to simplify input variables --- .../networking/delete-default-vpcs/main.tf | 5 +- .../aws_config/main.tf | 14 +- .../aws_config/variables.tf | 10 +- .../cloudtrail_cwlogs/cloudtrail.tf | 6 +- .../cloudtrail_cwlogs/ct-key.tf | 2 +- .../cloudtrail_cwlogs/ct-s3-bucket.tf | 54 +-- .../cloudtrail_cwlogs/cw-loggroup.tf | 350 +++++++++--------- .../cloudtrail_cwlogs/variables.tf | 9 +- .../guardduty/variables.tf | 11 - .../roles_iam_resources/variables.tf | 18 +- modules/storage/infra-s3-bucket/variables.tf | 11 - 11 files changed, 206 insertions(+), 284 deletions(-) diff --git a/modules/networking/delete-default-vpcs/main.tf b/modules/networking/delete-default-vpcs/main.tf index ab6130e..ea8e4cd 100644 --- a/modules/networking/delete-default-vpcs/main.tf +++ b/modules/networking/delete-default-vpcs/main.tf @@ -1,5 +1,8 @@ +data aws_regions all-aws-regions {} + resource "null_resource" "shell" { + for_each = data.aws_regions.all-aws-regions.names provisioner "local-exec" { - command = "/bin/bash -c '${path.module}/exec.sh ${var.region-name}'" + command = "/bin/bash -c '${path.module}/exec.sh ${each.value}'" } } \ No newline at end of file diff --git a/modules/security_identity_compliance/aws_config/main.tf b/modules/security_identity_compliance/aws_config/main.tf index e172743..d80c78d 100644 --- a/modules/security_identity_compliance/aws_config/main.tf +++ b/modules/security_identity_compliance/aws_config/main.tf @@ -80,16 +80,10 @@ resource "aws_config_configuration_recorder_status" "main" { module config-bucket { source = "../../storage/infra-s3-bucket" - - application = var.application - aws-region-short = var.aws-region-short - bucket-name = "${local.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}" + bucket-name = "${var.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}" add-random-suffix = false bucket-policy-json = data.aws_iam_policy_document.config_bucket_policy.json - customer-name = var.customer-name default-tags = var.default-tags - environment = var.environment - project = var.project } data "aws_iam_policy_document" "config_bucket_policy" { @@ -107,7 +101,7 @@ data "aws_iam_policy_document" "config_bucket_policy" { ] resources = [ - "arn:aws:s3:::${local.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}", + "arn:aws:s3:::${var.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}", ] } @@ -124,7 +118,7 @@ data "aws_iam_policy_document" "config_bucket_policy" { ] resources = [ - "arn:aws:s3:::${local.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}", + "arn:aws:s3:::${var.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}", ] } @@ -141,7 +135,7 @@ data "aws_iam_policy_document" "config_bucket_policy" { ] resources = [ - "arn:aws:s3:::${local.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}/*", + "arn:aws:s3:::${var.resource-prefix}-configbucket-${data.aws_caller_identity.this.account_id}/*", ] condition { diff --git a/modules/security_identity_compliance/aws_config/variables.tf b/modules/security_identity_compliance/aws_config/variables.tf index c7324ea..7dc9a0c 100644 --- a/modules/security_identity_compliance/aws_config/variables.tf +++ b/modules/security_identity_compliance/aws_config/variables.tf @@ -1,13 +1,5 @@ -variable "customer-name" {} -variable "environment" {} -variable "project" {} -variable "application" {} -variable "aws-region-short" {} variable "default-tags" {} - -locals { - resource-prefix = "${var.environment}-${var.aws-region-short}-${var.customer-name}-${var.project}" -} +variable resource-prefix {} variable config-retention-days { type = number diff --git a/modules/security_identity_compliance/cloudtrail_cwlogs/cloudtrail.tf b/modules/security_identity_compliance/cloudtrail_cwlogs/cloudtrail.tf index 049ef54..eb88545 100644 --- a/modules/security_identity_compliance/cloudtrail_cwlogs/cloudtrail.tf +++ b/modules/security_identity_compliance/cloudtrail_cwlogs/cloudtrail.tf @@ -1,12 +1,12 @@ resource "aws_iam_role" "iam_cloudtrial_cloudwatch_role" { - name = "${local.resource-prefix}-cwl-role" + name = "${var.resource-prefix}-cwl-role" assume_role_policy = data.aws_iam_policy_document.ct-role-assumerole-policy.json description = "Enables AWS CloudTrail to deliver log to CloudWatch log" tags = var.default-tags } resource "aws_iam_role_policy" "iam_cloudtrial_cloudwatach_role_policy" { - name = "${local.resource-prefix}-cwl-role-policy" + name = "${var.resource-prefix}-cwl-role-policy" role = aws_iam_role.iam_cloudtrial_cloudwatch_role.id policy = data.aws_iam_policy_document.ct-role-pdoc.json } @@ -46,7 +46,7 @@ data "aws_iam_policy_document" "ct-role-pdoc" { resource "aws_cloudtrail" "default" { - name = "${local.resource-prefix}-trail-001" + name = "${var.resource-prefix}-trail-001" enable_logging = true s3_bucket_name = local.ct-bucket-name enable_log_file_validation = true diff --git a/modules/security_identity_compliance/cloudtrail_cwlogs/ct-key.tf b/modules/security_identity_compliance/cloudtrail_cwlogs/ct-key.tf index 2ebb9c3..3640e86 100644 --- a/modules/security_identity_compliance/cloudtrail_cwlogs/ct-key.tf +++ b/modules/security_identity_compliance/cloudtrail_cwlogs/ct-key.tf @@ -6,7 +6,7 @@ resource "aws_kms_key" "ctbucket-key" { } resource "aws_kms_alias" ctbucket-key-aliaas { - name = "alias/${local.resource-prefix}-kmskey-default" + name = "alias/${var.resource-prefix}-kmskey-default" target_key_id = aws_kms_key.ctbucket-key.key_id } diff --git a/modules/security_identity_compliance/cloudtrail_cwlogs/ct-s3-bucket.tf b/modules/security_identity_compliance/cloudtrail_cwlogs/ct-s3-bucket.tf index 2892258..4d1f05b 100644 --- a/modules/security_identity_compliance/cloudtrail_cwlogs/ct-s3-bucket.tf +++ b/modules/security_identity_compliance/cloudtrail_cwlogs/ct-s3-bucket.tf @@ -55,54 +55,10 @@ data "aws_iam_policy_document" "cloudtrail_bucket_policy" { } +module ct-bucket { + source = "../../storage/infra-s3-bucket" -resource "aws_s3_bucket" "ct-bucket" { - bucket = local.ct-bucket-name - policy = data.aws_iam_policy_document.cloudtrail_bucket_policy.json - - versioning { - enabled = false - } - - server_side_encryption_configuration { - rule { - apply_server_side_encryption_by_default { - kms_master_key_id = aws_kms_key.ctbucket-key.arn - sse_algorithm = "aws:kms" - } - } - } - tags = var.default-tags - - lifecycle_rule { - id = "${local.resource-prefix}-ctbucket-lifecycle-rule" - enabled = true - - transition { - days = 30 - storage_class = "INTELLIGENT_TIERING" - } - - expiration { - days = var.cloudtrail-retain-days - } - } + bucket-name = local.ct-bucket-name + bucket-policy-json = data.aws_iam_policy_document.cloudtrail_bucket_policy.json + default-tags = var.default-tags } - - -resource "aws_s3_bucket_public_access_block" "s3-public-access-settings" { - bucket = aws_s3_bucket.ct-bucket.id - - block_public_acls = true - block_public_policy = true - ignore_public_acls = true - restrict_public_buckets = true -} - -resource "aws_s3_bucket_ownership_controls" "ctbucket-ownership-setting" { - bucket = aws_s3_bucket.ct-bucket.id - - rule { - object_ownership = "BucketOwnerPreferred" - } -} \ No newline at end of file diff --git a/modules/security_identity_compliance/cloudtrail_cwlogs/cw-loggroup.tf b/modules/security_identity_compliance/cloudtrail_cwlogs/cw-loggroup.tf index 2735d56..b6e2467 100644 --- a/modules/security_identity_compliance/cloudtrail_cwlogs/cw-loggroup.tf +++ b/modules/security_identity_compliance/cloudtrail_cwlogs/cw-loggroup.tf @@ -1,13 +1,13 @@ resource "aws_cloudwatch_log_group" "ct-cwl" { - name_prefix = "cloudtrail/" + name_prefix = "cloudtrail/" retention_in_days = var.cloudtrail-retain-days - kms_key_id = aws_kms_key.ctbucket-key.arn - tags = var.default-tags + kms_key_id = aws_kms_key.ctbucket-key.arn + tags = var.default-tags } resource "aws_cloudwatch_log_metric_filter" "cwl-metric-filter-cis11" { name = "cis11-rootaccess-filter" - pattern = <