> ## 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 Instances Should Use Latest Generation

### More Info:

Your AWS servers should be using the latest generation of EC2 instances for price-performance improvements.

### Risk Level

Low

### Address

Cost optimization

### Compliance Standards

CBP

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "EC2 Instances Should Use Latest Generation" for AWS using AWS console, follow these steps:

        1. Login to your AWS console.
        2. Navigate to the EC2 dashboard.
        3. Click on the "Instances" option from the left-hand side menu.
        4. Identify the instances that are not using the latest generation.
        5. Stop the instances by selecting them and clicking on the "Instance State" option from the top menu, then selecting "Stop".
        6. Once the instances are stopped, select them again and click on the "Actions" button from the top menu, then select "Instance Settings" and "Change Instance Type".
        7. Select the latest generation instance type from the list and click on "Apply".
        8. Start the instances again by selecting them and clicking on the "Instance State" option from the top menu, then selecting "Start".

        After completing these steps, your EC2 instances will be using the latest generation.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "EC2 Instances Should Use Latest Generation" for AWS using AWS CLI, follow the below steps:

        1. Open the AWS CLI on your local machine.

        2. Run the following command to list all the EC2 instances in your AWS account:

        ```
        aws ec2 describe-instances
        ```

        3. Identify the instances that are not using the latest generation.

        4. Stop the instance using the following command:

        ```
        aws ec2 stop-instances --instance-ids <instance-id>
        ```

        Make sure to replace `<instance-id>` with the actual ID of the instance that needs to be stopped.

        5. Once the instance is stopped, update the instance type to the latest generation using the following command:

        ```
        aws ec2 modify-instance-attribute --instance-id <instance-id> --instance-type <instance-type>
        ```

        Replace `<instance-id>` with the actual ID of the instance that needs to be updated, and `<instance-type>` with the latest generation instance type.

        6. Start the instance using the following command:

        ```
        aws ec2 start-instances --instance-ids <instance-id>
        ```

        Make sure to replace `<instance-id>` with the actual ID of the instance that needs to be started.

        7. Verify that the instance is running and using the latest generation instance type.

        Repeat the above steps for all the instances that are not using the latest generation.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the EC2 Instances Should Use Latest Generation misconfiguration for AWS using Python, you can follow these steps:

        1. Identify all the EC2 instances running in your AWS account that are not using the latest generation.

        2. Use the AWS SDK for Python (Boto3) to create a list of all the instances that are not using the latest generation.

        3. Use the Boto3 EC2 client to stop the instances that are not using the latest generation.

        4. Use the Boto3 EC2 client to modify the instance type to the latest generation.

        5. Use the Boto3 EC2 client to start the instances again.

        Here is a sample Python code that can be used to remediate the EC2 Instances Should Use Latest Generation misconfiguration in AWS:

        ```python theme={null}
        import boto3

        # Create a Boto3 EC2 client
        ec2 = boto3.client('ec2')

        # Get a list of all the instances in the account
        response = ec2.describe_instances()

        # Loop through all the instances and check if they are using the latest generation
        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                instance_type = instance['InstanceType']
                latest_generation_instance_types = ['t3', 'm5', 'c5', 'r5']
                if not any(instance_type.startswith(prefix) for prefix in latest_generation_instance_types):
                    instance_id = instance['InstanceId']
                    # Stop the instance
                    ec2.stop_instances(InstanceIds=[instance_id])
                    # Modify the instance type to the latest generation
                    ec2.modify_instance_attribute(InstanceId=instance_id, Attribute='instanceType', Value='t3.micro')
                    # Start the instance
                    ec2.start_instances(InstanceIds=[instance_id])
        ```

        Note: This code assumes that the latest generation instance type is `t3.micro`. You may need to modify the instance type value based on your specific requirements.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/migrating-latest-types.html](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/migrating-latest-types.html)
