Ensure a log metric filter and alarm exist for EC2 Large instance changes
Real-time monitoring of API calls can be achieved by directing CloudTrail Logs to CloudWatch Logs and establishing corresponding metric filters and alarms. It helps to manage your heavy compute resources and avoid any unexpected charges
Risk Level: Medium
Cloud Entity: CloudTrail
CloudGuard Rule ID: D9.AWS.MON.27
Covered by Spectral: No
Category: Management Tools
GSL LOGIC
List<CloudTrail> should have items with [hasSNSSubscriber='true' and metricFilters with [filterPattern isFilterPatternEqual('{ ($.eventName = RunInstances) && (($.requestParameters.instanceType = *.8xlarge) || ($.requestParameters.instanceType = *.4xlarge)) }')] length() > 0]
REMEDIATION
From Portal:
Perform the following steps to setup the metric filter, alarm, SNS topic, and subscription:
- Create a metric filter based on filter pattern relevant for this check.
- Create an SNS topic that the alarm will notify.
- Create an SNS subscription to the topic created in step 2.
- Create an alarm that is associated with the CloudWatch Logs Metric Filter created in step 1 and an SNS topic created in step 2.
From Command Line:
Perform the following to setup the metric filter, alarm, SNS topic, and subscription:
- Create a metric filter based on filter pattern provided which checks for EC2 instance changes. Use following command:
aws logs put-metric-filter --log-group-name cloudtrail_log_group_name --filter-name EC2_Large_Instance_Launch --metric-transformations metricName=EC2LargeInstanceLaunch,metricNamespace=EC2_Large_Instance_Launch,metricValue=1 --filter-pattern '{ ($.eventName = RunInstances) && (($.requestParameters.instanceType = *.8xlarge) || ($.requestParameters.instanceType = *.4xlarge)) }'
Note: You can choose your own metricName and metricNamespace strings. Using the same metricNamespace for all Foundations Benchmark metrics will group them together.
- Create an SNS topic that the alarm will notify
aws sns create-topic --name sns_topic_name
Note: you can execute this command once and then re-use the same topic for all monitoring alarms.
- Create an SNS subscription to the topic created in step 2
aws sns subscribe --topic-arn sns_topic_arn --protocol protocol_for_sns --notification-endpoint sns_subscription_endpoints
Note: you can execute this command once and then re-use the SNS subscription for all monitoring alarms.
- Create an alarm that is associated with the CloudWatch Logs Metric Filter created in step 1 and an SNS topic created in step 2
aws cloudwatch put-metric-alarm --alarm-name EC2_Large_Instance_Launch --metric-name EC2LargeInstanceLaunch --statistic Sum --period 300 --threshold 1 --comparison-operator GreaterThanOrEqualToThreshold --evaluation-periods 1 --namespace 'EC2_Large_Instance_Launch' --alarm-actions sns_topic_arn
References:
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/US_SetupSNS.html
- https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudwatch-alarms-for-cloudtrail.html
- https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html
- https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudwatch/put-metric-alarm.html
- https://awscli.amazonaws.com/v2/documentation/api/latest/reference/logs/put-metric-filter.html
- https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/create-topic.html
- https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/subscribe.html
CloudTrail
AWS CloudTrail is a service that enables governance, compliance, operational auditing, and risk auditing of your AWS account. With CloudTrail, you can log, continuously monitor, and retain account activity related to actions across your AWS infrastructure. CloudTrail provides event history of your AWS account activity, including actions taken through the AWS Management Console, AWS SDKs, command line tools, and other AWS services. This event history simplifies security analysis, resource change tracking, and troubleshooting.
Compliance Frameworks
- AWS CloudGuard Best Practices
- AWS NIST 800-53 Rev 5
- CloudGuard AWS All Rules Ruleset
Updated about 1 year ago