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

# Unused Virtual Private Gateways Should Be Removed

### More Info:

Unused Amazon Virtual Private Gateways should be removed in order to adhere to best practices and to avoid reaching the service limit.

### Risk Level

Low

### Address

Operational Maturity, Security

### Compliance Standards

CBP

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the issue of Unused Virtual Private Gateways in AWS, you can follow these steps using the AWS Management Console:

        1. **Login to AWS Console**: Navigate to the AWS Management Console at [https://aws.amazon.com/](https://aws.amazon.com/) and login with your credentials.

        2. **Access VPC Dashboard**: Go to the VPC dashboard by selecting the "Services" dropdown menu at the top left corner, then selecting "VPC" under the Networking & Content Delivery section.

        3. **Identify Unused Virtual Private Gateways**:
           * In the VPC dashboard, click on "Virtual Private Gateways" on the left-hand side menu.
           * Review the list of Virtual Private Gateways to identify any that are not associated with any VPCs or are no longer in use.

        4. **Dissociate and Delete Unused Virtual Private Gateways**:
           * Select the unused Virtual Private Gateway that you want to remove.
           * Click on the "Actions" dropdown menu and choose "Detach from VPC" to dissociate the Virtual Private Gateway from the VPC.
           * Once detached, select the Virtual Private Gateway again and click on the "Actions" dropdown menu, then choose "Delete Virtual Private Gateway" to remove it completely.

        5. **Confirm Deletion**:
           * A confirmation dialog will appear asking you to confirm the deletion of the Virtual Private Gateway. Confirm the action to proceed with the deletion.

        6. **Verify Removal**:
           * After deleting the Virtual Private Gateway, verify that it has been successfully removed from the list of Virtual Private Gateways in the VPC dashboard.

        By following these steps, you can remediate the issue of Unused Virtual Private Gateways in AWS by identifying and removing any Virtual Private Gateways that are no longer in use.
      </Accordion>

      <Accordion title="Using CLI">
        #

        To remediate the issue of unused Virtual Private Gateways in AWS, you can follow the steps below using AWS CLI:

        1. List all the Virtual Private Gateways in your AWS account:

        ```
        aws ec2 describe-vpn-gateways --query 'VpnGateways[?State == `available`].{ID:VpnGatewayId}'
        ```

        2. Identify the Virtual Private Gateways that are not associated with any VPC. These are the ones that are unused.

        3. To detach the Virtual Private Gateway from a VPC, you can use the following command:

        ```
        aws ec2 detach-vpn-gateway --vpn-gateway-id <vpn-gateway-id> --vpc-id <vpc-id>
        ```

        4. Once you have detached the Virtual Private Gateway from all VPCs, you can delete the Virtual Private Gateway using the following command:

        ```
        aws ec2 delete-vpn-gateway --vpn-gateway-id <vpn-gateway-id>
        ```

        5. Confirm that the Virtual Private Gateway has been deleted by listing all the Virtual Private Gateways again:

        ```
        aws ec2 describe-vpn-gateways --query 'VpnGateways[?State == `available`].{ID:VpnGatewayId}'
        ```

        By following these steps, you can identify and remove any unused Virtual Private Gateways in your AWS account using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the issue of unused Virtual Private Gateways in AWS using Python, you can follow these steps:

        1. Use Boto3, the AWS SDK for Python, to list all the Virtual Private Gateways in your AWS account.
        2. Use Boto3 to list all the VPCs in your AWS account.
        3. Compare the Virtual Private Gateways with the VPCs to identify any unused Virtual Private Gateways.
        4. If any Virtual Private Gateways are found to be unused, delete them using Boto3.

        Here is a sample Python script to achieve this:

        ```python theme={null}
        import boto3

        # Initialize the Boto3 client
        ec2_client = boto3.client('ec2')

        # List all Virtual Private Gateways
        response = ec2_client.describe_vpn_gateways()

        # List all VPCs
        vpcs = ec2_client.describe_vpcs()

        # Extract the VPC IDs from the VPCs response
        vpc_ids = [vpc['VpcId'] for vpc in vpcs['Vpcs']]

        # Identify the Virtual Private Gateways that are not associated with any VPC
        unused_vpn_gateways = [vpn_gateway['VpnGatewayId'] for vpn_gateway in response['VpnGateways'] if 'VpcAttachments' not in vpn_gateway or len(vpn_gateway['VpcAttachments']) == 0]

        # Delete the unused Virtual Private Gateways
        for vpn_gateway_id in unused_vpn_gateways:
            ec2_client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id)
            print(f"Deleted Virtual Private Gateway: {vpn_gateway_id}")
        ```

        Make sure you have the necessary permissions in your AWS IAM role to delete Virtual Private Gateways before running this script. Also, ensure you have installed the Boto3 library (`pip install boto3`) and configured your AWS credentials.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>

### Additional Reading:

* [https://docs.aws.amazon.com/vpn/latest/s2svpn/delete-vpn.html](https://docs.aws.amazon.com/vpn/latest/s2svpn/delete-vpn.html)
