Cloudtrail Integrated Cloudwatch Remediation
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the misconfiguration "CloudTrail Events Should Be Monitored By CloudWatch Logs" in AWS using the AWS console, follow these steps:
-
Open the AWS Management Console and navigate to the CloudTrail service.
-
Select the trail for which you want to enable CloudWatch Logs monitoring.
-
Click on the "Edit" button in the "CloudWatch Logs" section.
-
Select the option "Yes" for "Enable CloudWatch Logs".
-
Choose a CloudWatch Logs log group to which you want to send the CloudTrail events.
-
Click on the "Save" button to save the changes.
-
Verify that the CloudWatch Logs integration is working by checking the log group for the CloudTrail events.
By following these steps, you will have successfully remediated the misconfiguration "CloudTrail Events Should Be Monitored By CloudWatch Logs" in AWS.
Using CLI
To remediate the misconfiguration "CloudTrail Events Should Be Monitored By CloudWatch Logs" for AWS using AWS CLI, follow these steps:
-
Open the AWS CLI on your local machine or EC2 instance.
-
Run the following command to create a new CloudWatch Logs group:
aws logs create-log-group --log-group-name my-log-group-nameReplace
my-log-group-namewith the name you want to give to your CloudWatch Logs group. -
Run the following command to create a new CloudWatch Logs stream:
aws logs create-log-stream --log-group-name my-log-group-name --log-stream-name my-log-stream-nameReplace
my-log-group-namewith the name of the CloudWatch Logs group you created in step 2, and replacemy-log-stream-namewith the name you want to give to your CloudWatch Logs stream. -
Run the following command to enable CloudTrail log file validation:
aws cloudtrail update-trail --name my-trail-name --enable-log-file-validationReplace
my-trail-namewith the name of the CloudTrail trail you want to enable log file validation for. -
Run the following command to configure CloudTrail to send events to your CloudWatch Logs group:
aws cloudtrail update-trail --name my-trail-name --cloud-watch-logs-log-group-arn arn:aws:logs:us-east-1:123456789012:log-group:my-log-group-name --cloud-watch-logs-role-arn arn:aws:iam::123456789012:role/my-cloudtrail-roleReplace
my-trail-namewith the name of the CloudTrail trail you want to configure, replacearn:aws:logs:us-east-1:123456789012:log-group:my-log-group-namewith the ARN of the CloudWatch Logs group you created in step 2, and replacearn:aws:iam::123456789012:role/my-cloudtrail-rolewith the ARN of the IAM role you want to use for CloudTrail.Note: You need to have the required permissions to create CloudWatch Logs group, stream and IAM role.
-
Verify that CloudTrail events are being sent to your CloudWatch Logs group by checking the logs in the CloudWatch Logs console.
Congratulations! You have successfully remediated the misconfiguration "CloudTrail Events Should Be Monitored By CloudWatch Logs" for AWS using AWS CLI.
Using Python
To remediate this misconfiguration in AWS, you can use the following steps in Python:
- Import the necessary modules:
import boto3
- Create a CloudWatch Logs client:
cloudwatch_logs = boto3.client('logs')
- Get a list of all the existing CloudTrail logs:
cloudtrail_logs = boto3.client('cloudtrail')
logs = cloudtrail_logs.describe_trails()
- For each CloudTrail log, check if it is already being monitored by CloudWatch Logs:
for log in logs['trailList']:
log_name = log['Name']
response = cloudwatch_logs.describe_log_groups(logGroupNamePrefix=log_name)
if not response['logGroups']:
# Create a new log group for the CloudTrail log
cloudwatch_logs.create_log_group(logGroupName=log_name)
# Create a CloudWatch Logs subscription filter for the CloudTrail log
cloudwatch_logs.put_subscription_filter(
logGroupName=log_name,
filterName='CloudTrailFilter',
filterPattern='',
destinationArn=''
)
-
Replace the
destinationArnparameter with the ARN of the CloudWatch Logs destination you want to use. -
Save the Python script and run it to remediate the misconfiguration.
This script will create a new CloudWatch Logs log group for each CloudTrail log that is not already being monitored, and then create a CloudWatch Logs subscription filter for that log group. This will ensure that all CloudTrail events are monitored by CloudWatch Logs.
Using Terraform
# CloudWatch Logs log group to receive CloudTrail events
resource "aws_cloudwatch_log_group" "cloudtrail" {
name = "CloudTrail/${var.TRAIL_NAME}-Logs" # substitute var or hard-code your trail name
retention_in_days = 365 # adjust to your log retention policy
}
# IAM role that CloudTrail assumes to write to CloudWatch Logs
resource "aws_iam_role" "cloudtrail_cloudwatch_logs" {
name = "CLOUDTRAIL_LOGS_ROLE_NAME" # replace with your approved IAM role name
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Principal = {
Service = "cloudtrail.amazonaws.com"
}
Action = "sts:AssumeRole"
}
]
})
}
# Permissions for CloudTrail to write to the log group
resource "aws_iam_role_policy" "cloudtrail_cloudwatch_logs" {
name = "cloudtrail-to-cloudwatch-logs"
role = aws_iam_role.cloudtrail_cloudwatch_logs.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowCloudTrailToCreateAndWriteLogs"
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
]
Resource = aws_cloudwatch_log_group.cloudtrail.arn
}
]
})
}
# CloudTrail trail integrated with CloudWatch Logs
resource "aws_cloudtrail" "this" {
name = var.TRAIL_NAME # replace with your trail name or hard-code
s3_bucket_name = "TRAIL_S3_BUCKET_NAME" # replace with your CloudTrail S3 bucket name
# These two arguments implement the CLI `update-trail --cloud-watch-logs-log-group-arn ... [--cloud-watch-logs-role-arn ...]`
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.cloudtrail.arn}:*"
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_cloudwatch_logs.arn
# ...keep your existing trail settings here (is_multi_region_trail, kms_key_id, etc.)
}
Substitute:
var.TRAIL_NAME(or"TRAIL_NAME") with the name of the existing CloudTrail trail ({{asset_label}}in your CLI example).TRAIL_S3_BUCKET_NAMEwith the S3 bucket used by the trail.CLOUDTRAIL_LOGS_ROLE_NAMEwith your approved IAM role name, if you have naming standards.
This change is in-place for the trail (no forced replacement). It will create a new log group and IAM role/policy.
To verify, terraform plan should show:
+creation ofaws_cloudwatch_log_group.cloudtrail+creation ofaws_iam_role.cloudtrail_cloudwatch_logsandaws_iam_role_policy.cloudtrail_cloudwatch_logs~update toaws_cloudtrail.thisaddingcloud_watch_logs_group_arnandcloud_watch_logs_role_arn.