Skip to main content

Triage and Remediation

Remediation

Using Console

To remediate the “EC2 Instance Count Should Not Exceed the Limit” misconfiguration in AWS using the AWS console, follow these steps:
  1. Log in to the AWS Management Console.
  2. Navigate to the EC2 Dashboard.
  3. Click on the “Limits” link in the left-hand menu.
  4. In the “Service” drop-down menu, select “EC2”.
  5. In the “Limit types” drop-down menu, select “Running On-Demand Instances”.
  6. Check the current limit for the region where the misconfiguration was detected.
  7. If the limit has been exceeded, click on the “Request limit increase” button.
  8. Fill out the form with the required information, including the new limit request and the reason for the increase.
  9. Submit the form and wait for AWS to review and approve the request.
  10. Once the request is approved, the limit will be increased, and you can launch additional EC2 instances within the new limit.
Note: It is important to regularly monitor your EC2 instance usage and request limit increases as needed to avoid exceeding the limits and incurring unexpected charges.

To remediate the misconfiguration of EC2 Instance Count Should Not Exceed the Limit in AWS using AWS CLI, you can follow the below steps:
  1. First, check the current EC2 instance count using the AWS CLI command:
  1. If the instance count is exceeding the limit, you need to stop or terminate some of the instances to bring the count below the limit.
  2. To stop an instance, use the AWS CLI command:
Here, replace <instance-id> with the actual ID of the instance that you want to stop.
  1. To terminate an instance, use the AWS CLI command:
Here, replace <instance-id> with the actual ID of the instance that you want to terminate.
  1. Repeat step 3 and 4 until the instance count is below the limit.
  2. Once the instance count is below the limit, you can monitor it using CloudWatch alarms and set up notifications to alert you if the count exceeds the limit again in the future.
Note: It is important to regularly monitor your AWS resources and set up alerts to avoid exceeding the limits and incurring unexpected charges.
To remediate the misconfiguration of EC2 instance count exceeding the limit in AWS using Python, follow the below steps:
  1. Import the necessary libraries:
  1. Set up an AWS session with the required credentials:
  1. Create an EC2 client using the session:
  1. Get the current instance count and the instance limit using the describe_account_attributes() method:
  1. Check if the current instance count exceeds the instance limit:
  1. If the current instance count exceeds the instance limit, terminate the excess instances:
  1. The final code should look like this:
Note: Make sure to replace the placeholders ‘YOUR_ACCESS_KEY_ID’, ‘YOUR_SECRET_ACCESS_KEY’, and ‘YOUR_REGION_NAME’ with the actual values.
If you remove an aws_instance resource (or reduce its count/remove an element from for_each), applying the plan will permanently terminate that EC2 instance; this is irreversible.For verification, terraform plan should show either:
  • a - destroy for the specific aws_instance you chose to terminate, and no unexpected changes to others, or
  • a + create for aws_servicequotas_service_quota.ec2_running_ondemand_standard with value = NEW_DESIRED_LIMIT.