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

# No ec2 classic remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the EC2 Classic misconfiguration in AWS, follow these steps:

        1. Login to the AWS Management Console.
        2. Navigate to the EC2 dashboard.
        3. In the left-hand navigation menu, select "Classic Wizard".
        4. Click on "Launch Instance" to launch a new instance.
        5. Choose the appropriate AMI and instance type for your needs.
        6. In the "Configure Instance Details" section, select the VPC that you want to launch the instance in.
        7. In the "Advanced Details" section, expand the "Network Interfaces" section and select the appropriate subnet.
        8. Click "Next" to proceed to the "Add Storage" section and configure your storage needs.
        9. Continue through the remaining configuration steps until you reach the "Review Instance Launch" page.
        10. Review your instance settings and click "Launch" to launch the instance in your selected VPC.

        Note: If you have existing EC2 Classic instances, you should migrate them to a VPC as soon as possible. AWS provides a migration wizard to help with this process.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the EC2 Classic misconfiguration 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 describe your VPCs:

        ```
        aws ec2 describe-vpcs
        ```

        3. Identify the VPC that you want to use for your EC2 instances.

        4. Run the following command to create a new security group in the VPC:

        ```
        aws ec2 create-security-group --group-name my-security-group --description "My security group" --vpc-id <vpc-id>
        ```

        Replace `<vpc-id>` with the ID of the VPC that you want to use.

        5. Run the following command to authorize inbound traffic to the security group:

        ```
        aws ec2 authorize-security-group-ingress --group-id <security-group-id> --protocol tcp --port 22 --cidr 0.0.0.0/0
        ```

        Replace `<security-group-id>` with the ID of the security group that you created in step 4.

        6. Launch your EC2 instance in the VPC that you identified in step 3, and specify the security group that you created in step 4.

        7. Verify that your EC2 instance is running in the VPC by running the following command:

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

        Replace `<instance-id>` with the ID of your EC2 instance.

        8. Once you have verified that your EC2 instance is running in the correct VPC, terminate any instances that are running in EC2 Classic.

        Note: Before terminating any instances, make sure that you have migrated any data or applications to the new VPC.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the "EC2 Classic Should Not Be Used" misconfiguration in AWS using Python, you can follow the below steps:

        1. Identify all the EC2 instances in the account that are running in the EC2 Classic environment. You can use the boto3 library to list all the instances and check their VPC ID.

        ```python theme={null}
        import boto3

        ec2_client = boto3.client('ec2')

        response = ec2_client.describe_instances(
            Filters=[
                {
                    'Name': 'instance-state-name',
                    'Values': ['running']
                }
            ]
        )

        for reservation in response['Reservations']:
            for instance in reservation['Instances']:
                if 'VpcId' not in instance:
                    # This instance is running in the EC2 Classic environment
                    # Add remediation steps for this instance
        ```

        2. For each EC2 Classic instance, launch a new instance in a VPC. You can use the `run_instances` method of the EC2 client to launch a new instance. Make sure to specify the correct VPC ID and subnet ID in the `NetworkInterfaces` parameter.

        ```python theme={null}
        response = ec2_client.run_instances(
            ImageId='ami-0c55b159cbfafe1f0',
            InstanceType='t2.micro',
            KeyName='my-key-pair',
            MaxCount=1,
            MinCount=1,
            NetworkInterfaces=[
                {
                    'DeviceIndex': 0,
                    'AssociatePublicIpAddress': True,
                    'SubnetId': 'subnet-12345678',
                    'Groups': ['sg-0123456789abcdef0'],
                    'DeleteOnTermination': True
                }
            ]
        )

        new_instance_id = response['Instances'][0]['InstanceId']
        ```

        3. Once the new instance is launched, you can terminate the old instance running in the EC2 Classic environment. You can use the `terminate_instances` method of the EC2 client to terminate an instance.

        ```python theme={null}
        ec2_client.terminate_instances(
            InstanceIds=['i-0123456789abcdef0']
        )
        ```

        4. Repeat steps 2 and 3 for all the EC2 Classic instances in the account.

        Note: Before implementing any remediation steps, it is recommended to test them in a non-production environment and ensure that they do not cause any unintended side effects.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # New EC2 instance in a VPC launched from the migration AMI you created via CLI
        resource "aws_instance" "vpc_migrated_instance" {
          ami                         = "MIGRATION_AMI_ID_FROM_CREATE_IMAGE" # replace with AMI ID from `aws ec2 create-image`
          instance_type               = "INSTANCE_TYPE_FROM_CLASSIC_INSTANCE" # e.g., "t3.micro"
          subnet_id                   = "YOUR_VPC_SUBNET_ID"                  # replace with your target VPC subnet ID
          vpc_security_group_ids      = ["YOUR_VPC_SECURITY_GROUP_ID"]        # replace with one or more VPC security group IDs
          key_name                    = "YOUR_KEY_PAIR_NAME"                  # replace with your EC2 key pair name

          # Optionally copy over tags from the original instance
          tags = {
            Name = "MIGRATED_INSTANCE_NAME"  # replace with desired name/identifier
          }
        }
        ```

        Terraform cannot create an AMI from an unmanaged EC2-Classic instance or terminate that unmanaged instance; you must still run the verified CLI steps (`create-image` from the EC2-Classic instance, then `terminate-instances` after validation) exactly as given. The Terraform snippet above implements the “run-instances” step by defining the new VPC-backed EC2 instance using the AMI ID and VPC parameters you provide.

        This change does not replace any existing Terraform-managed resource; `terraform plan` should show only one new `aws_instance.vpc_migrated_instance` to be created and no destroys, and after you manually terminate the EC2-Classic instance via CLI, it will disappear from outside Terraform’s state.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
