|
| 1 | +# *************** Steps are as Below to create Sumo Logic CloudTrail source *************** # |
| 2 | +# 1. Create AWS S3 Bucket. If the Bucket is created, create SNS Topic and SNS policy to attach to Bucket. |
| 3 | +# 2. Create CloudTrail in AWS. Create CloudTrail only when the bucket is created. |
| 4 | +# 3. Create IAM role in AWS with access to the bucket name provided. |
| 5 | +# 4. Create a Collector. If the Collector ID is provided, do not create a Collector. |
| 6 | +# 5. Create the source either in the collector created or in the collector id provided. |
| 7 | +# 6. Create SNS Subscription to be attached to the source and SNS Topic. |
| 8 | + |
| 9 | +resource "aws_s3_bucket" "s3_bucket" { |
| 10 | + for_each = toset(var.source_details.bucket_details.create_bucket ? ["s3_bucket"] : []) |
| 11 | + |
| 12 | + bucket = local.bucket_name |
| 13 | + force_destroy = var.source_details.bucket_details.force_destroy_bucket |
| 14 | + |
| 15 | + policy = templatefile("${path.module}/templates/cloudtrail_bucket_policy.tmpl", { |
| 16 | + BUCKET_NAME = local.bucket_name |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +resource "aws_sns_topic" "sns_topic" { |
| 21 | + for_each = toset(local.create_sns_topic ? ["sns_topic"] : []) |
| 22 | + |
| 23 | + name = "SumoLogic-Terraform-CloudTrail-Module-${local.aws_account_id}" |
| 24 | + policy = templatefile("${path.module}/templates/sns_topic_policy.tmpl", { |
| 25 | + BUCKET_NAME = local.bucket_name, |
| 26 | + AWS_REGION = local.aws_region, |
| 27 | + SNS_TOPIC_NAME = "SumoLogic-Terraform-CloudTrail-Module-${local.aws_account_id}", |
| 28 | + AWS_ACCOUNT = local.aws_account_id |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +resource "aws_s3_bucket_notification" "bucket_notification" { |
| 33 | + for_each = toset(local.create_sns_topic && var.source_details.bucket_details.create_bucket ? ["bucket_notification"] : []) |
| 34 | + |
| 35 | + bucket = aws_s3_bucket.s3_bucket["s3_bucket"].id |
| 36 | + |
| 37 | + topic { |
| 38 | + topic_arn = aws_sns_topic.sns_topic["sns_topic"].arn |
| 39 | + events = ["s3:ObjectCreated:Put"] |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +resource "aws_cloudtrail" "cloudtrail" { |
| 44 | + for_each = toset(local.create_trail ? ["cloudtrail"] : []) |
| 45 | + |
| 46 | + name = var.cloudtrail_details.name |
| 47 | + include_global_service_events = var.cloudtrail_details.include_global_service_events |
| 48 | + s3_bucket_name = var.source_details.bucket_details.create_bucket ? aws_s3_bucket.s3_bucket["s3_bucket"].id : local.bucket_name |
| 49 | + is_multi_region_trail = var.cloudtrail_details.is_multi_region_trail |
| 50 | + is_organization_trail = var.cloudtrail_details.is_organization_trail |
| 51 | +} |
| 52 | + |
| 53 | +resource "aws_iam_role" "source_iam_role" { |
| 54 | + for_each = toset(local.create_iam_role ? ["source_iam_role"] : []) |
| 55 | + |
| 56 | + name = "SumoLogic-Terraform-CloudTrail-Module-${local.aws_account_id}-${local.aws_region}" |
| 57 | + path = "/" |
| 58 | + |
| 59 | + assume_role_policy = templatefile("${path.module}/templates/sumologic_assume_role.tmpl", { |
| 60 | + SUMO_LOGIC_ACCOUNT_ID = var.source_details.sumo_account_id, |
| 61 | + ENVIRONMENT = data.sumologic_caller_identity.current.environment, |
| 62 | + SUMO_LOGIC_ORG_ID = var.sumologic_organization_id |
| 63 | + }) |
| 64 | + |
| 65 | + managed_policy_arns = [aws_iam_policy.iam_policy["iam_policy"].arn] |
| 66 | +} |
| 67 | + |
| 68 | +resource "aws_iam_policy" "iam_policy" { |
| 69 | + for_each = toset(local.create_iam_role ? ["iam_policy"] : []) |
| 70 | + |
| 71 | + name = "SumoLogicCloudTrailSource-${local.aws_account_id}-${local.aws_region}" |
| 72 | + policy = templatefile("${path.module}/templates/sumologic_source_policy.tmpl", { |
| 73 | + BUCKET_NAME = local.bucket_name |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +resource "sumologic_collector" "collector" { |
| 78 | + for_each = toset(var.create_collector ? ["collector"] : []) |
| 79 | + name = local.collector_name |
| 80 | + description = var.collector_details.description |
| 81 | + fields = var.collector_details.fields |
| 82 | + timezone = "UTC" |
| 83 | +} |
| 84 | + |
| 85 | +resource "time_sleep" "wait_3_minutes" { |
| 86 | + create_duration = "180s" |
| 87 | +} |
| 88 | + |
| 89 | +resource "sumologic_cloudtrail_source" "source" { |
| 90 | + depends_on = [ |
| 91 | + time_sleep.wait_3_minutes |
| 92 | + ] |
| 93 | + |
| 94 | + lifecycle { |
| 95 | + ignore_changes = [cutoff_timestamp, cutoff_relative_time] |
| 96 | + } |
| 97 | + category = var.source_details.source_category |
| 98 | + collector_id = var.create_collector ? sumologic_collector.collector["collector"].id : var.source_details.collector_id |
| 99 | + content_type = "AwsCloudTrailBucket" |
| 100 | + cutoff_relative_time = var.source_details.cutoff_relative_time |
| 101 | + description = var.source_details.description |
| 102 | + fields = var.source_details.fields |
| 103 | + name = var.source_details.source_name |
| 104 | + paused = var.source_details.paused |
| 105 | + scan_interval = var.source_details.scan_interval |
| 106 | + authentication { |
| 107 | + type = "AWSRoleBasedAuthentication" |
| 108 | + role_arn = local.create_iam_role ? aws_iam_role.source_iam_role["source_iam_role"].arn : var.source_details.iam_role_arn |
| 109 | + } |
| 110 | + |
| 111 | + path { |
| 112 | + type = "S3BucketPathExpression" |
| 113 | + bucket_name = var.source_details.bucket_details.create_bucket ? aws_s3_bucket.s3_bucket["s3_bucket"].id : local.bucket_name |
| 114 | + path_expression = local.logs_path_expression |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_sns_topic_subscription" "subscription" { |
| 119 | + delivery_policy = jsonencode({ |
| 120 | + "guaranteed" = false, |
| 121 | + "healthyRetryPolicy" = { |
| 122 | + "numRetries" = 40, |
| 123 | + "minDelayTarget" = 10, |
| 124 | + "maxDelayTarget" = 300, |
| 125 | + "numMinDelayRetries" = 3, |
| 126 | + "numMaxDelayRetries" = 5, |
| 127 | + "numNoDelayRetries" = 0, |
| 128 | + "backoffFunction" = "exponential" |
| 129 | + }, |
| 130 | + "sicklyRetryPolicy" = null, |
| 131 | + "throttlePolicy" = null |
| 132 | + }) |
| 133 | + endpoint = sumologic_cloudtrail_source.source.url |
| 134 | + endpoint_auto_confirms = true |
| 135 | + protocol = "https" |
| 136 | + topic_arn = local.create_sns_topic ? aws_sns_topic.sns_topic["sns_topic"].arn : var.source_details.sns_topic_arn |
| 137 | +} |
0 commit comments