> ## 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.

# Reserved Instance Lease Expiration In The Next 7 Days

### More Info:

Lists all EC2 reserved instances expiring in the next 7 days.

### Risk Level

Low

### Address

Cost Optimisation

### Compliance Standards

CBP

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the "Reserved Instance Lease Expiration In The Next 7 Days" misconfiguration for AWS, you can follow the below steps:

        1. Log in to your AWS Management Console.
        2. Navigate to the EC2 Dashboard.
        3. Click on the "Reserved Instances" link in the left-hand menu.
        4. In the "Reserved Instances" page, you will see a list of your reserved instances.
        5. Look for the instances that have lease expiration in the next 7 days.
        6. Select the instance by clicking on the checkbox in front of it.
        7. Click on the "Modify Reserved Instances" button.
        8. In the "Modify Reserved Instances" page, you can choose to either modify the instance or exchange it for a new one.
        9. If you choose to modify the instance, you can change the instance type, availability zone, or platform.
        10. If you choose to exchange the instance, you can select a new instance type, availability zone, or platform.
        11. Once you have made your changes, click on the "Preview" button to review your changes.
        12. If you are satisfied with your changes, click on the "Modify Reserved Instances" button to apply the changes.

        By following these steps, you will be able to remediate the "Reserved Instance Lease Expiration In The Next 7 Days" misconfiguration for AWS.

        #
      </Accordion>

      <Accordion title="Using CLI">
        The Reserved Instance Lease Expiration In The Next 7 Days issue in AWS can be remediated by modifying the reservation period of the instance. Here are the steps to remediate this issue using AWS CLI:

        1. Open the AWS CLI on your local machine.

        2. Run the following command to get a list of all the reserved instances that have an expiration date within the next 7 days:

        ```
        aws ec2 describe-reserved-instances --filters "Name=state,Values=active" "Name=product-description,Values=*Linux/UNIX*" "Name=scope,Values=Availability Zone" --query 'ReservedInstances[*].[ReservedInstancesId,InstanceType,AvailabilityZone,InstanceCount,Start,End,Duration,State,Tags[?Key==`Name`].Value|[0]]' --output table --region <region>
        ```

        Note: Replace `<region>` with the region where your instances are located.

        3. Identify the instance that requires remediation.

        4. Run the following command to modify the reservation period:

        ```
        aws ec2 modify-reserved-instances --reserved-instances-id <reservation_id> --target-configuration "InstanceCount=<count>,End=`date -d '+1 year' +%Y-%m-%d`" --region <region>
        ```

        Note: Replace `<reservation_id>` with the ID of the reserved instance that requires remediation and `<count>` with the number of instances that you want to reserve. Also, ensure that the `End` date is set to at least 1 year from the current date.

        5. Verify that the reservation has been modified by running the first command again and checking that the expiration date is now more than 7 days away.

        By following these steps, you can remediate the Reserved Instance Lease Expiration In The Next 7 Days issue in AWS using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the issue of Reserved Instance Lease Expiration in the next 7 days in AWS using Python, follow these steps:

        1. Import the necessary AWS SDK boto3:

        ```
        import boto3
        ```

        2. Create an EC2 client object:

        ```
        ec2 = boto3.client('ec2')
        ```

        3. Use the describe\_reserved\_instances() method to get the list of all reserved instances:

        ```
        reserved_instances = ec2.describe_reserved_instances()
        ```

        4. Loop through the list of reserved instances and check if any of them are expiring within the next 7 days:

        ```
        for reserved_instance in reserved_instances['ReservedInstances']:
            expiration_time = reserved_instance['End']
            remaining_days = (expiration_time.date() - datetime.date.today()).days
            if remaining_days <= 7:
                # Do something to remediate the issue
        ```

        5. To remediate the issue, you can either renew the reserved instance or purchase a new one. To renew the reserved instance, use the modify\_reserved\_instances() method:

        ```
        response = ec2.modify_reserved_instances(
            ReservedInstancesIds=[
                'string',
            ],
            TargetConfigurations=[
                {
                    'AvailabilityZone': 'string',
                    'InstanceCount': 123,
                    'InstanceType': 'string',
                    'Platform': 'string',
                    'Scope': 'string'
                },
            ]
        )
        ```

        6. Alternatively, you can purchase a new reserved instance using the purchase\_reserved\_instances\_offering() method:

        ```
        response = ec2.purchase_reserved_instances_offering(
            InstanceCount=123,
            ReservedInstancesOfferingId='string',
            DryRun=True|False,
            LimitPrice={
                'Amount': 'string',
                'CurrencyCode': 'string'
            }
        )
        ```

        7. Finally, you can schedule a Lambda function to run this script periodically and remediate the issue automatically.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-reserved-instances.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-reserved-instances.html)
