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

# Elasticsearch Reserved Instance Lease Expiration In The Next 30 Days

### More Info:

Your AWS Elasticsearch Reserved Instances (RIs) should be renewed before expiration in order to get a significant discount on the hourly charges.

### 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 Elasticsearch Reserved Instance Lease Expiration in the next 30 days issue for AWS using the AWS console, follow these steps:

        1. Login to your AWS console.
        2. Go to the Elasticsearch service.
        3. Click on the "Reserved Instances" option in the left-hand menu.
        4. Identify the Elasticsearch Reserved Instance that is expiring in the next 30 days.
        5. Click on the "Modify Reserved Instance" button.
        6. Select the "Extend the term of this Reserved Instance" option.
        7. Choose the desired term length for the extension.
        8. Review the changes and click on the "Purchase" button to complete the reservation extension.

        By following these steps, you will have successfully remediated the Elasticsearch Reserved Instance Lease Expiration in the next 30 days issue on AWS.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the Elasticsearch Reserved Instance Lease Expiration issue in 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 Elasticsearch Reserved Instances in your AWS account:

           ```
           aws es describe-reserved-elasticsearch-instances
           ```

        3. Identify the Reserved Instance that has an expiration date within the next 30 days.

        4. Run the following command to modify the Reserved Instance:

           ```
           aws es modify-reserved-elasticsearch-instance --reserved-elasticsearch-instance-id <Reserved Instance ID> --reserved-elasticsearch-instance-offering-id <New Offering ID>
           ```

           Replace `<Reserved Instance ID>` with the ID of the Reserved Instance that needs to be modified, and `<New Offering ID>` with the ID of the new Elasticsearch Reserved Instance offering that you want to use.

           Note: You can use the `aws es describe-elasticsearch-instance-type-offerings` command to list all the Elasticsearch Reserved Instance offerings available in your account.

        5. Verify that the Reserved Instance has been modified by running the following command:

           ```
           aws es describe-reserved-elasticsearch-instances --reserved-elasticsearch-instance-id <Reserved Instance ID>
           ```

           Replace `<Reserved Instance ID>` with the ID of the Reserved Instance that you modified.

        6. Repeat steps 3 to 5 for all the Elasticsearch Reserved Instances that have an expiration date within the next 30 days.

        By following these steps, you can remediate the Elasticsearch Reserved Instance Lease Expiration issue in AWS using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the Elasticsearch Reserved Instance Lease Expiration in the next 30 days issue on AWS using Python, you can follow these steps:

        1. Import the necessary AWS SDK modules in Python:

        ```python theme={null}
        import boto3
        from datetime import datetime, timedelta
        ```

        2. Set up the AWS credentials and region:

        ```python theme={null}
        session = boto3.Session(
            aws_access_key_id='YOUR_ACCESS_KEY',
            aws_secret_access_key='YOUR_SECRET_KEY',
            region_name='YOUR_REGION'
        )
        ```

        3. Create an Elasticsearch client:

        ```python theme={null}
        es_client = session.client('es')
        ```

        4. Retrieve the list of Elasticsearch domains:

        ```python theme={null}
        domains = es_client.list_domain_names()['DomainNames']
        ```

        5. Loop through each domain and check if there are any reserved instances expiring in the next 30 days:

        ```python theme={null}
        for domain in domains:
            domain_name = domain['DomainName']
            reserved_instances = es_client.list_reserved_elasticsearch_instances(
                ElasticsearchInstanceType='m4.large.elasticsearch',
                MaxResults=100,
                NextToken=''
            )['ReservedElasticsearchInstances']
            for ri in reserved_instances:
                expiration_date = ri['StartTime'] + timedelta(days=ri['Duration'])
                if expiration_date <= datetime.now() + timedelta(days=30):
                    print(f"Reserved instance for {domain_name} is expiring on {expiration_date}.")
        ```

        6. To remediate the issue, you can either renew the reserved instance or purchase a new one. To renew the reserved instance, use the following code:

        ```python theme={null}
        es_client.purchase_reserved_elasticsearch_instance_offering(
            ReservedElasticsearchInstanceOfferingId='OFFERING_ID',
            ReservationName='RESERVATION_NAME',
            InstanceCount=1
        )
        ```

        Replace `OFFERING_ID` with the ID of the reserved instance offering, and `RESERVATION_NAME` with a name for the reservation.

        7. If you want to purchase a new reserved instance, use the following code:

        ```python theme={null}
        es_client.purchase_reserved_elasticsearch_instance_offering(
            ReservedElasticsearchInstanceOfferingId='OFFERING_ID',
            InstanceCount=1,
            ReservationName='RESERVATION_NAME'
        )
        ```

        Replace `OFFERING_ID` with the ID of the reserved instance offering, and `RESERVATION_NAME` with a name for the reservation.

        By following these steps, you should be able to remediate the Elasticsearch Reserved Instance Lease Expiration in the next 30 days issue on AWS using Python.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-ri.html](https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/aes-ri.html)
