For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-aws-cloudwatch-unauthorized-access-defined-alarm-missing.md.
A documentation index is available at /llms.txt.
This check validates that a CloudWatch metric alarm is properly configured to monitor unauthorized API calls, which could indicate potential security breaches or application errors. A properly configured alarm requires the metric_name to correctly reference the corresponding metric filter ID. When this reference is incorrect (for example, using a placeholder such as ‘XXXX NOT YOUR FILTER XXXX’ instead of the actual metric filter ID), the alarm will not trigger when unauthorized access attempts occur, leaving your AWS infrastructure vulnerable to undetected attacks.
To fix this issue, ensure the metric_name references the correct metric filter ID, as shown in this example:
resource"aws_cloudwatch_metric_alarm""cis_unauthorized_api_calls_cw_alarm"{alarm_name="CIS-3.1-UnauthorizedAPICalls"comparison_operator="GreaterThanOrEqualToThreshold"evaluation_periods="1"metric_name=aws_cloudwatch_log_metric_filter.cis_unauthorized_api_calls_metric_filter.idnamespace="CIS_Metric_Alarm_Namespace"period="300"statistic="Sum"threshold="1"alarm_description="Monitoring unauthorized API calls will help reveal application errors and may reduce time to detect malicious activity."alarm_actions=[aws_sns_topic.CIS_Alerts_SNS_Topic.arn]insufficient_data_actions=[]}resource"aws_cloudwatch_log_metric_filter""cis_unauthorized_api_calls_metric_filter"{name="CIS-UnauthorizedAPICalls"pattern="{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }"log_group_name=aws_cloudwatch_log_group.CIS_CloudWatch_LogsGroup.namemetric_transformation{name="CIS-UnauthorizedAPICalls"namespace="CIS_Metric_Alarm_Namespace"value="1"}}
Non-Compliant Code Examples
resource"aws_cloudwatch_metric_alarm""cis_unauthorized_api_calls_cw_alarm"{alarm_name="CIS-3.1-UnauthorizedAPICalls"comparison_operator="GreaterThanOrEqualToThreshold"evaluation_periods="1"metric_name="XXXX NOT YOUR FILTER XXXX"namespace="CIS_Metric_Alarm_Namespace"period="300"statistic="Sum"threshold="1"alarm_description="Monitoring unauthorized API calls will help reveal application errors and may reduce time to detect malicious activity."alarm_actions=[aws_sns_topic.CIS_Alerts_SNS_Topic.arn]insufficient_data_actions=[]}resource"aws_cloudwatch_log_metric_filter""cis_unauthorized_api_calls_metric_filter"{name="CIS-UnauthorizedAPICalls"pattern="{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }"log_group_name=aws_cloudwatch_log_group.CIS_CloudWatch_LogsGroup.namemetric_transformation{name="CIS-UnauthorizedAPICalls"namespace="CIS_Metric_Alarm_Namespace"value="1"}}
resource"aws_cloudwatch_log_metric_filter""cis_no_mfa_console_signin_metric_filter"{name="CIS-ConsoleSigninWithoutMFA"pattern="{ ($.eventName = \"ConsoleLogin\") && ($.additionalEventData.MFAUsed != \"Yes\") }"log_group_name=aws_cloudwatch_log_group.CIS_CloudWatch_LogsGroup.namemetric_transformation{name="CIS-ConsoleSigninWithoutMFA"namespace="CIS_Metric_Alarm_Namespace"value="1"}}resource"aws_cloudwatch_metric_alarm""cis_no_mfa_console_signin_cw_alarm"{alarm_name="CIS-3.2-ConsoleSigninWithoutMFA"comparison_operator="GreaterThanOrEqualToThreshold"evaluation_periods="1"metric_name=aws_cloudwatch_log_metric_filter.cis_no_mfa_console_signin_metric_filter.idnamespace="CIS_Metric_Alarm_Namespace"period="300"statistic="Sum"threshold="1"alarm_description="Monitoring for single-factor console logins will increase visibility into accounts that are not protected by MFA."alarm_actions=[aws_sns_topic.CIS_Alerts_SNS_Topic.arn]insufficient_data_actions=[]}
resource"aws_cloudwatch_metric_alarm""cis_unauthorized_api_calls_cw_alarm"{alarm_name="CIS-3.1-UnauthorizedAPICalls"comparison_operator="GreaterThanOrEqualToThreshold"evaluation_periods="1"metric_name=aws_cloudwatch_log_metric_filter.cis_unauthorized_api_calls_metric_filter.idnamespace="CIS_Metric_Alarm_Namespace"period="300"statistic="Sum"threshold="1"alarm_description="Monitoring unauthorized API calls will help reveal application errors and may reduce time to detect malicious activity."alarm_actions=[aws_sns_topic.CIS_Alerts_SNS_Topic.arn]insufficient_data_actions=[]}resource"aws_cloudwatch_log_metric_filter""cis_unauthorized_api_calls_metric_filter"{name="CIS-UnauthorizedAPICalls"pattern="{ $.errorCode = \"AccessDenied*\" }"log_group_name=aws_cloudwatch_log_group.CIS_CloudWatch_LogsGroup.namemetric_transformation{name="CIS-UnauthorizedAPICalls"namespace="CIS_Metric_Alarm_Namespace"value="1"}}
1
2
rulesets:- Terraform / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.