Skip to content

Commit 4e64b5d

Browse files
authored
Merge pull request #5 from SumoLogic/aws
Modules for AWS and SumoLogic
2 parents 4302578 + b097382 commit 4e64b5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+5724
-414
lines changed

aws/cloudtrail/README.md

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
1-
# AWS CloudTrail
1+
# SumoLogic-AWS-CloudTrail
22

3-
## Purpose
3+
This module is used to create AWS and Sumo Logic resource to collect CloudTrail logs from an AWS S3 bucket. Features include
4+
- Create AWS CloudTrail (when creating a new AWS S3 bucket) or use an existing AWS CloudTrail (Provide existing AWS S3 bucket name) which send data to S3.
5+
- Create AWS S3 bucket or use an existing AWS S3 bucket.
6+
- Create AWS IAM role or use an existing IAM role.
7+
- Create AWS SNS Topic or use an existing AWS SNS topic.
8+
- Create Sumo Logic hosted collector or use an existing Sumo Logic hosted collector.
9+
- Create Sumo Logic CloudTrail source.
410

5-
This module installs [Sumo Logic CloudTrail applications](https://help.sumologic.com/07Sumo-Logic-Apps/01Amazon_and_AWS/AWS_CloudTrail) in Sumo Logic.
11+
## Requirements
612

7-
Apps installed are:
8-
- AWS CloudTrail
9-
- PCI Compliance for AWS CloudTrail
10-
- CIS AWS Foundations Benchmark
13+
| Name | Version |
14+
|------|---------|
15+
| terraform | >= 0.13.0 |
16+
| aws | ~> 3.29.1 |
17+
| sumologic | ~> 2.6.0 |
18+
| time | 0.7.1 |
1119

12-
## Requirements
20+
## Providers
1321

14-
* [Terraform](https://www.terraform.io/downloads.html) >= 0.13.0
15-
* Null >= 2.1
16-
* SumoLogic >= 2.1.0
17-
18-
## Module Declaration
19-
20-
This module requires Sumo Logic External Id and Folder id as explained [here](https://github.com/SumoLogic/terraform-sumologic-sumo-logic-integrations#prerequisites-for-using-modules).
21-
22-
```shell
23-
module "sumologic-cloudtrail-apps" {
24-
source = "SumoLogic/sumo-logic-integrations/sumologic//aws/cloudtrail"
25-
sumo_access_id = "<SUMO_ACCESS_ID>"
26-
sumo_access_key = "<SUMO_ACCESS_KEY>"
27-
sumo_external_id = "<SUMO_EXTERNAL_ID>"
28-
aws_resource_name = "sumo-logic-terraform-cloudtrail"
29-
sumo_api_endpoint = "https://api.sumologic.com/api/v1/"
30-
sumo_collector_name = "sumo-logic-terraform-cloudtrail"
31-
sumo_source_name = "sumo-logic-terraform-cloudtrail"
32-
sumo_source_category = "AWS/CloudTrail"
33-
sumo_aws_account_id = "926226587429"
34-
folder_id = sumologic_folder.folder.id
35-
app_version = "1.0"
36-
}
37-
```
22+
| Name | Version |
23+
|------|---------|
24+
| aws | ~> 3.29.1 |
25+
| sumologic | ~> 2.6.0 |
26+
| time | 0.7.1 |
3827

3928
## Inputs
4029

4130
| Name | Description | Type | Default | Required |
42-
|------|-------------|------|---------|:-----:|
43-
|sumo_access_id|[Sumo Logic Access ID](https://help.sumologic.com/Manage/Security/Access-Keys)|string||yes
44-
|sumo_access_key|[Sumo Logic Access Key](https://help.sumologic.com/Manage/Security/Access-Keys)|string||yes
45-
|sumo_external_id|[Sumo Logic External ID](https://help.sumologic.com/03Send-Data/Sources/02Sources-for-Hosted-Collectors/Amazon-Web-Services/Grant-Access-to-an-AWS-Product#iam-role)|string||yes
46-
|aws_resource_name|AWS S3 Bucket, AWS SNS Topic, AWS CloudTrail, AWS IAM Role and IAM Policy will be created with the provided name|string|sumo-logic-terraform-cloudtrail|no
47-
|sumo_api_endpoint|[Sumo Logic API Endpoint](https://help.sumologic.com/APIs/General-API-Information/Sumo-Logic-Endpoints-and-Firewall-Security)|string|https://api.sumologic.com/api/v1/|yes
48-
|folder_id|Sumo Logic Folder ID|string||yes
49-
|sumo_collector_name|Provide a Collector Name|string|sumo-logic-terraform-cloudtrail|no
50-
|sumo_source_name|Provide a CloudTrail Source Name|string|sumo-logic-terraform-cloudtrail|no
51-
|sumo_source_category|Provide a CloudTrail Source Category|string|AWS/CloudTrail|no
52-
|sumo_aws_account_id|Provide the Sumo Logic AWS Account ID. Get the Account ID - [Visit](https://help.sumologic.com/03Send-Data/Sources/02Sources-for-Hosted-Collectors/Amazon-Web-Services/Grant-Access-to-an-AWS-Product#iam-role)|string|926226587429|no
53-
|app_version|The app_version input parameter can be used to install a new copy of the app. When the app_version field is changed, it will force Terraform to install a new app folder with the current timestamp.|String|1.0|no
31+
|------|-------------|------|---------|:--------:|
32+
| cloudtrail\_details | Provide details for the AWS CloudTrail. If not provided, then defaults will be used. | <pre>object({<br> name = string<br> is_multi_region_trail = bool<br> is_organization_trail = bool<br> include_global_service_events = bool<br> })</pre> | <pre>{<br> "include_global_service_events": false,<br> "is_multi_region_trail": false,<br> "is_organization_trail": false,<br> "name": "SumoLogic-Terraform-CloudTrail"<br>}</pre> | no |
33+
| collector\_details | Provide details for the Sumo Logic collector. If not provided, then defaults will be used. | <pre>object({<br> collector_name = string<br> description = string<br> fields = map(string)<br> })</pre> | <pre>{<br> "collector_name": "SumoLogic CloudTrail Collector <AWS Account Id>",<br> "description": "This collector is created using Sumo Logic terraform AWS cloudtrail module to collect AWS cloudtrail logs.",<br> "fields": {}<br>}</pre> | no |
34+
| create\_collector | Provide "true" if you would like to create the Sumo Logic Collector. | `bool` | n/a | yes |
35+
| create\_trail | Provide "true" if you would like to create the AWS CloudTrail. If the bucket is created by the module, module by default creates the AWS cloudtrail. | `bool` | n/a | yes |
36+
| source\_details | Provide details for the Sumo Logic CloudTrail source. If not provided, then defaults will be used. | <pre>object({<br> source_name = string<br> source_category = string<br> collector_id = string<br> description = string<br> bucket_details = object({<br> create_bucket = bool<br> bucket_name = string<br> path_expression = string<br> force_destroy_bucket = bool<br> })<br> paused = bool<br> scan_interval = string<br> sumo_account_id = number<br> cutoff_relative_time = string<br> fields = map(string)<br> iam_role_arn = string<br> sns_topic_arn = string<br> })</pre> | <pre>{<br> "bucket_details": {<br> "bucket_name": "cloudtrail-logs-accountid-region",<br> "create_bucket": true,<br> "force_destroy_bucket": true,<br> "path_expression": "AWSLogs/<ACCOUNT-ID>/CloudTrail/<REGION-NAME>/*"<br> },<br> "collector_id": "",<br> "cutoff_relative_time": "-1d",<br> "description": "This source is created using Sumo Logic terraform AWS cloudtrail module to collect AWS cloudtrail logs.",<br> "fields": {},<br> "iam_role_arn": "",<br> "paused": false,<br> "scan_interval": 300000,<br> "sns_topic_arn": "",<br> "source_category": "Labs/aws/cloudtrail",<br> "source_name": "CloudTrail Source",<br> "sumo_account_id": 926226587429<br>}</pre> | no |
37+
| sumologic\_organization\_id | Appears on the Account Overview page that displays information about your Sumo Logic organization. Used for IAM Role in Sumo Logic AWS Sources. | `string` | n/a | yes |
38+
39+
## Outputs
40+
41+
| Name | Description |
42+
|------|-------------|
43+
| aws\_cloudtrail | AWS Trail created to send CloudTrail logs to AWS S3 bucket. |
44+
| aws\_iam\_role | AWS IAM role with permission to allow Sumo Logic to read logs from S3 Bucket. |
45+
| aws\_s3\_bucket | AWS S3 Bucket name created to Store the CloudTrail logs. |
46+
| aws\_s3\_bucket\_notification | AWS S3 Bucket Notification attached to the AWS S3 Bucket |
47+
| aws\_sns\_subscription | AWS SNS subscription to Sumo Logic AWS CloudTrail source. |
48+
| aws\_sns\_topic | AWS SNS topic attached to the AWS S3 bucket. |
49+
| sumologic\_collector | Sumo Logic hosted collector. |
50+
| sumologic\_source | Sumo Logic AWS CloudTrail source. |

aws/cloudtrail/cloudtrail.tf

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}

aws/cloudtrail/data.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "aws_region" "current" {}
2+
3+
data "aws_caller_identity" "current" {}
4+
5+
data "sumologic_caller_identity" "current" {}

aws/cloudtrail/inputs.tf

Lines changed: 0 additions & 52 deletions
This file was deleted.

aws/cloudtrail/locals.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
locals {
2+
3+
aws_account_id = data.aws_caller_identity.current.account_id
4+
5+
aws_region = data.aws_region.current.id
6+
7+
# Get the default collector name if no collector name is provided.
8+
collector_name = var.collector_details.collector_name == "SumoLogic CloudTrail Collector <AWS Account Id>" ? "SumoLogic CloudTrail Collector ${local.aws_account_id}" : var.collector_details.collector_name
9+
10+
# Get the default bucket name when no bucket is provided and create_bucket is true.
11+
bucket_name = var.source_details.bucket_details.create_bucket && var.source_details.bucket_details.bucket_name == "cloudtrail-logs-accountid-region" ? "cloudtrail-logs-${local.aws_account_id}-${local.aws_region}" : var.source_details.bucket_details.bucket_name
12+
13+
# Create IAM role condition if no IAM ROLE ARN is provided.
14+
create_iam_role = var.source_details.iam_role_arn != "" ? false : true
15+
16+
# Create SNS topic condition if no SNS topic arn is provided.
17+
create_sns_topic = var.source_details.sns_topic_arn != "" ? false : true
18+
19+
# Trail should be created when we create the bucket. If we do not create the bucket, user should have capability to create and not create trail.
20+
create_trail = var.source_details.bucket_details.create_bucket ? true : var.create_trail
21+
22+
# If we create the bucket, then get the default PATH expression.
23+
logs_path_expression = var.source_details.bucket_details.create_bucket ? "AWSLogs/${local.aws_account_id}/CloudTrail/${local.aws_region}/*" : var.source_details.bucket_details.path_expression
24+
}

0 commit comments

Comments
 (0)