> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Ec2 for retirement remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        The following are the step-by-step instructions to remediate the "Scheduled Events for EC2 Instances" misconfiguration in AWS:

        1. Log in to the AWS Management Console.
        2. Go to the EC2 dashboard.
        3. Click on the "Instances" link in the left-hand navigation pane.
        4. Select the instance(s) that have scheduled events.
        5. Click on the "Actions" button and select "Instance Settings" from the dropdown menu.
        6. Click on "Modify Scheduled Events".
        7. In the "Modify Scheduled Events" dialog box, select the scheduled event(s) that you want to modify or delete.
        8. To modify the event, update the details as desired and click "Save". To delete the event, click "Delete".
        9. Repeat steps 4-8 for any additional instances that have scheduled events.

        By following these steps, you should be able to remediate the "Scheduled Events for EC2 Instances" misconfiguration in AWS using the AWS console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        Scheduled Events for EC2 Instances is a misconfiguration that occurs when there are scheduled events for EC2 instances that are not required or are outdated. These events can include instance retirement, system maintenance, and other events that can impact the availability of the instance.

        To remediate this misconfiguration for AWS using AWS CLI, follow these steps:

        1. Open the AWS CLI on your local machine or EC2 instance.

        2. Run the following command to list all the scheduled events for your EC2 instances:

           ```
           aws ec2 describe-instance-status --include-all-instances --query 'InstanceStatuses[*].Events[*]'
           ```

        3. Identify the scheduled events that are not required or are outdated.

        4. To cancel a scheduled event, run the following command:

           ```
           aws ec2 cancel-scheduled-instance-termination --instance-id <instance-id> --dry-run
           ```

           Replace `<instance-id>` with the ID of the instance for which you want to cancel the scheduled event.

           Note: The `--dry-run` option will simulate the command but will not make any changes. Remove this option to execute the command.

        5. To modify the scheduled event, run the following command:

           ```
           aws ec2 modify-instance-event-attribute --instance-id <instance-id> --instance-event-id <event-id> --not-before <new-time> --dry-run
           ```

           Replace `<instance-id>` with the ID of the instance for which you want to modify the scheduled event, `<event-id>` with the ID of the event, and `<new-time>` with the new time for the event.

           Note: The `--dry-run` option will simulate the command but will not make any changes. Remove this option to execute the command.

        6. Repeat steps 4 and 5 for all the scheduled events that need to be remediated.

        7. Verify that all the scheduled events have been remediated by running the command in step 2 again.

        8. Close the AWS CLI.

        By following these steps, you can remediate the Scheduled Events for EC2 Instances misconfiguration for AWS using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        The following are the step by step instructions to remediate the "Scheduled Events for EC2 Instances" misconfiguration for AWS using Python:

        1. Import the necessary AWS SDK for Python (Boto3) modules:

        ```python theme={null}
        import boto3
        from botocore.exceptions import ClientError
        ```

        2. Initialize the EC2 client:

        ```python theme={null}
        ec2 = boto3.client('ec2')
        ```

        3. Retrieve the list of EC2 instances using the `describe_instances()` method:

        ```python theme={null}
        instances = ec2.describe_instances()
        ```

        4. Loop through each instance and check if it has any scheduled events using the `describe_instance_status()` method:

        ```python theme={null}
        for instance in instances['Reservations']:
            instance_id = instance['Instances'][0]['InstanceId']
            try:
                response = ec2.describe_instance_status(InstanceIds=[instance_id])
                if 'ScheduledEvents' in response['InstanceStatuses'][0]:
                    # There are scheduled events for this instance
                    print(f"Scheduled events found for instance {instance_id}")
                    # Remediate the scheduled events by stopping the instance
                    ec2.stop_instances(InstanceIds=[instance_id])
                    print(f"Instance {instance_id} stopped successfully")
            except ClientError as e:
                print(f"Error retrieving instance status for {instance_id}: {e}")
        ```

        5. If the instance has any scheduled events, stop the instance using the `stop_instances()` method.

        6. Verify that the instance has been stopped successfully.

        Note: This code will stop the instance to remediate the scheduled events. You can modify it to suit your specific requirements.
      </Accordion>

      <Accordion title="Using Terraform">
        Terraform cannot remediate scheduled EC2 instance events (stop/start/reboot) because these are runtime operations, not configuration changes exposed by the `aws_instance` resource or any other Terraform-managed resource.

        Use the AWS CLI or Console exactly as in the verified remediation, from outside Terraform:

        ```bash theme={null}
        # 1. Check the scheduled event details
        aws ec2 describe-instance-status \
          --instance-ids INSTANCE_ID_HERE \
          --region REGION_CODE_HERE \
          --include-all-instances

        # 2a. For instance-retirement or events requiring instance-stop:
        aws ec2 stop-instances \
          --instance-ids INSTANCE_ID_HERE \
          --region REGION_CODE_HERE

        aws ec2 start-instances \
          --instance-ids INSTANCE_ID_HERE \
          --region REGION_CODE_HERE

        # 2b. For system-reboot events:
        aws ec2 reboot-instances \
          --instance-ids INSTANCE_ID_HERE \
          --region REGION_CODE_HERE
        ```

        Warnings from the verified fix still apply: this causes downtime, may change public IPs unless using Elastic IPs, clears instance-store data on stop, and reboot causes a brief interruption.

        There is no Terraform argument or resource that will make `terraform plan`/`apply` show these runtime actions; you must verify remediation by re-running the AWS CLI check or checking the EC2 console scheduled events view.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
