2021-01-26 21:40:02 +08:00
resource " aws_cloudwatch_log_group " " ct-cwl " {
2022-09-05 13:52:21 +08:00
name_prefix = " cloudtrail/ "
2021-01-27 09:42:51 +08:00
retention_in_days = var . cloudtrail - retain - days
2022-09-05 13:52:21 +08:00
kms_key_id = aws_kms_key . ctbucket - key . arn
tags = var . default - tags
2021-01-26 21:40:02 +08:00
}
resource " aws_cloudwatch_log_metric_filter " " cwl-metric-filter-cis11 " {
name = " cis11-rootaccess-filter "
2022-09-05 13:52:21 +08:00
pattern = < < EOT
2021-01-26 21:40:02 +08:00
{ $ . userIdentity . type =" Root " && $ . userIdentity . invokedBy NOT EXISTS && $ . eventType ! = " AwsServiceEvent " }
EOT
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
metric_transformation {
name = " cis11-rootaccess-metric "
namespace = " LogMetrics "
value = " 1 "
}
}
resource " aws_cloudwatch_metric_alarm " " cis11-rootaccess-alarm " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis11-rootaccess-alarm "
comparison_operator = " GreaterThanOrEqualToThreshold "
evaluation_periods = " 1 "
metric_name = " cis11-rootaccess-metric "
namespace = " LogMetrics "
period = " 300 "
statistic = " Average "
threshold = " 1 "
alarm_description = " Root access is detected from cloudtrail "
treat_missing_data = " notBreaching "
2021-01-26 21:40:02 +08:00
// alarm_actions = []
}
2021-01-29 14:39:58 +08:00
// CIS 3.x benchmark from asecure.cloud https://asecure.cloud/p/monitoring_cis_benchmark/
resource " aws_cloudwatch_metric_alarm " " CwAlarm2 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-unauthorized_api_calls "
alarm_description = " A CloudWatch Alarm that triggers if Multiple unauthorized actions or logins attempted. "
metric_name = " UnauthorizedAttemptCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 60 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter2 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .errorCode = \ " * UnauthorizedOperation \ " ) || ( $ .errorCode = \ " AccessDenied * \ " ) } "
name = " UnauthorizedAttemptCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " UnauthorizedAttemptCount "
value = " 1 "
namespace = " CloudTrailMetrics "
2021-01-29 14:39:58 +08:00
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm3 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-no_mfa_console_logins "
alarm_description = " A CloudWatch Alarm that triggers if there is a Management Console sign-in without MFA. "
metric_name = " ConsoleSigninWithoutMFA "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 60 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter3 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " {( $ .eventName = \ " ConsoleLogin \ " ) && ( $ .additionalEventData.MFAUsed != \ " Yes \ " ) && ( $ .responseElements.ConsoleLogin != \ " Failure \ " ) && ( $ .additionalEventData.SamlProviderArn NOT EXISTS) } "
name = " ConsoleSigninWithoutMFA "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " ConsoleSigninWithoutMFA "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm4 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-iam_policy_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to IAM policies. Events include IAM policy creation/deletion/update operations as well as attaching/detaching policies from IAM users, roles or groups. "
metric_name = " IAMPolicyEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter4 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " {( $ .eventName=DeleteGroupPolicy)||( $ .eventName=DeleteRolePolicy)||( $ .eventName=DeleteUserPolicy)||( $ .eventName=PutGroupPolicy)||( $ .eventName=PutRolePolicy)||( $ .eventName=PutUserPolicy)||( $ .eventName=CreatePolicy)||( $ .eventName=DeletePolicy)||( $ .eventName=CreatePolicyVersion)||( $ .eventName=DeletePolicyVersion)||( $ .eventName=AttachRolePolicy)||( $ .eventName=DetachRolePolicy)||( $ .eventName=AttachUserPolicy)||( $ .eventName=DetachUserPolicy)||( $ .eventName=AttachGroupPolicy)||( $ .eventName=DetachGroupPolicy)} "
name = " IAMPolicyEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " IAMPolicyEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm5 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-cloudtrail_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to CloudTrail. "
metric_name = " CloudTrailEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter5 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = CreateTrail) || ( $ .eventName = UpdateTrail) || ( $ .eventName = DeleteTrail) || ( $ .eventName = StartLogging) || ( $ .eventName = StopLogging) } "
name = " CloudTrailEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " CloudTrailEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm6 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-failed_console_logins "
alarm_description = " A CloudWatch Alarm that triggers if there are AWS Management Console authentication failures. "
metric_name = " ConsoleLoginFailures "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter6 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = ConsoleLogin) && ( $ .errorMessage = \ " Failed authentication \ " ) } "
name = " ConsoleLoginFailures "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " ConsoleLoginFailures "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm7 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-disabled_deleted_cmks "
alarm_description = " A CloudWatch Alarm that triggers if customer created CMKs get disabled or scheduled for deletion. "
metric_name = " KMSCustomerKeyDeletion "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 60 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter7 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventSource = kms.amazonaws.com) && (( $ .eventName=DisableKey) || ( $ .eventName=ScheduleKeyDeletion)) } "
name = " KMSCustomerKeyDeletion "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " KMSCustomerKeyDeletion "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm8 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-s3_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to an S3 Bucket. "
metric_name = " S3BucketActivityEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter8 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventSource = s3.amazonaws.com) && (( $ .eventName = PutBucketAcl) || ( $ .eventName = PutBucketPolicy) || ( $ .eventName = PutBucketCors) || ( $ .eventName = PutBucketLifecycle) || ( $ .eventName = PutBucketReplication) || ( $ .eventName = DeleteBucketPolicy) || ( $ .eventName = DeleteBucketCors) || ( $ .eventName = DeleteBucketLifecycle) || ( $ .eventName = DeleteBucketReplication)) } "
name = " S3BucketActivityEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " S3BucketActivityEventCount "
value = " 1 "
namespace = " CloudTrailMetrics "
2021-01-29 14:39:58 +08:00
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm9 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-config_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to AWS Config. "
metric_name = " CloudTrailEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter9 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = PutConfigurationRecorder) || ( $ .eventName = StopConfigurationRecorder) || ( $ .eventName = DeleteDeliveryChannel) || ( $ .eventName = PutDeliveryChannel) } "
name = " CloudTrailEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " CloudTrailEventCount "
value = " 1 "
namespace = " CloudTrailMetrics "
2021-01-29 14:39:58 +08:00
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm10 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-securitygroup_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to Security Groups. "
metric_name = " SecurityGroupEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter10 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = AuthorizeSecurityGroupIngress) || ( $ .eventName = AuthorizeSecurityGroupEgress) || ( $ .eventName = RevokeSecurityGroupIngress) || ( $ .eventName = RevokeSecurityGroupEgress) || ( $ .eventName = CreateSecurityGroup) || ( $ .eventName = DeleteSecurityGroup) } "
name = " SecurityGroupEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " SecurityGroupEventCount "
value = " 1 "
namespace = " CloudTrailMetrics "
2021-01-29 14:39:58 +08:00
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm11 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-nacl_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to Network ACLs. "
metric_name = " NetworkAclEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
2022-09-05 13:52:21 +08:00
}
2021-01-29 14:39:58 +08:00
resource " aws_cloudwatch_log_metric_filter " " MetricFilter11 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = CreateNetworkAcl) || ( $ .eventName = CreateNetworkAclEntry) || ( $ .eventName = DeleteNetworkAcl) || ( $ .eventName = DeleteNetworkAclEntry) || ( $ .eventName = ReplaceNetworkAclEntry) || ( $ .eventName = ReplaceNetworkAclAssociation) } "
name = " NetworkAclEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " NetworkAclEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm12 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-igw_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to an Internet Gateway in a VPC. "
metric_name = " GatewayEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter12 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = CreateCustomerGateway) || ( $ .eventName = DeleteCustomerGateway) || ( $ .eventName = AttachInternetGateway) || ( $ .eventName = CreateInternetGateway) || ( $ .eventName = DeleteInternetGateway) || ( $ .eventName = DetachInternetGateway) } "
name = " GatewayEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " GatewayEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm13 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-vpc_routetable_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to a VPC's Route Table. "
metric_name = " VpcRouteTableEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter13 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = AssociateRouteTable) || ( $ .eventName = CreateRoute) || ( $ .eventName = CreateRouteTable) || ( $ .eventName = DeleteRoute) || ( $ .eventName = DeleteRouteTable) || ( $ .eventName = ReplaceRoute) || ( $ .eventName = ReplaceRouteTableAssociation) || ( $ .eventName = DisassociateRouteTable) } "
name = " VpcRouteTableEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " VpcRouteTableEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}
resource " aws_cloudwatch_metric_alarm " " CwAlarm14 " {
2022-09-05 13:52:21 +08:00
alarm_name = " cis-vpc_changes "
alarm_description = " A CloudWatch Alarm that triggers when changes are made to a VPC. "
metric_name = " VpcEventCount "
namespace = " CloudTrailMetrics "
statistic = " Sum "
period = " 300 "
threshold = " 1 "
evaluation_periods = " 1 "
2021-01-29 14:39:58 +08:00
comparison_operator = " GreaterThanOrEqualToThreshold "
// alarm_actions = [""]
treat_missing_data = " notBreaching "
}
resource " aws_cloudwatch_log_metric_filter " " MetricFilter14 " {
log_group_name = aws_cloudwatch_log_group . ct - cwl . name
2022-09-05 13:52:21 +08:00
pattern = " { ( $ .eventName = CreateVpc) || ( $ .eventName = DeleteVpc) || ( $ .eventName = ModifyVpcAttribute) || ( $ .eventName = AcceptVpcPeeringConnection) || ( $ .eventName = CreateVpcPeeringConnection) || ( $ .eventName = DeleteVpcPeeringConnection) || ( $ .eventName = RejectVpcPeeringConnection) || ( $ .eventName = AttachClassicLinkVpc) || ( $ .eventName = DetachClassicLinkVpc) || ( $ .eventName = DisableVpcClassicLink) || ( $ .eventName = EnableVpcClassicLink) } "
name = " VpcEventCount "
2021-01-29 14:39:58 +08:00
metric_transformation {
2022-09-05 13:52:21 +08:00
name = " VpcEventCount "
value = " 1 "
2021-01-29 14:39:58 +08:00
namespace = " CloudTrailMetrics "
}
}