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

# Internet gateway authorized vpc only remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of an Internet Gateway being attached to an unauthorized Virtual Private Cloud (VPC) in AWS, follow these steps using the AWS Management Console:

        1. **Sign in to the AWS Management Console**: Go to [https://aws.amazon.com/](https://aws.amazon.com/) and sign in to your AWS account.

        2. **Navigate to the VPC Dashboard**:
           * In the AWS Management Console, under the "Services" tab, select "VPC" under the Networking & Content Delivery section.

        3. **Identify the unauthorized VPC**:
           * In the VPC Dashboard, locate the Internet Gateway that is attached to the unauthorized VPC. The unauthorized VPC will be the one that should not have the Internet Gateway attached to it.

        4. **Detaching the Internet Gateway**:
           * Click on the "Internet Gateways" option in the VPC Dashboard.
           * Select the Internet Gateway that is attached to the unauthorized VPC.
           * Click on the "Actions" dropdown menu and select "Detach from VPC".
           * In the confirmation dialog box, click on "Detach".

        5. **Attach the Internet Gateway to the authorized VPC**:
           * Click on the "Internet Gateways" option in the VPC Dashboard.
           * Select the Internet Gateway that you just detached.
           * Click on the "Actions" dropdown menu and select "Attach to VPC".
           * Select the authorized VPC from the dropdown list.
           * Click on "Attach".

        6. **Verify the Configuration**:
           * Go back to the VPC Dashboard and confirm that the Internet Gateway is now attached to the authorized VPC.

        By following these steps, you have successfully remediated the misconfiguration of an Internet Gateway being attached to an unauthorized Virtual Private Cloud in AWS.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of an Internet Gateway being attached to unauthorized Virtual Private Clouds in AWS using AWS CLI, follow these steps:

        1. List all the Internet Gateways in your AWS account:

        ```bash theme={null}
        aws ec2 describe-internet-gateways
        ```

        2. Identify the Internet Gateway that is attached to unauthorized Virtual Private Clouds.

        3. Detach the Internet Gateway from the unauthorized Virtual Private Cloud:

        ```bash theme={null}
        aws ec2 detach-internet-gateway --internet-gateway-id <internet-gateway-id> --vpc-id <unauthorized-vpc-id>
        ```

        Replace `<internet-gateway-id>` with the ID of the Internet Gateway and `<unauthorized-vpc-id>` with the ID of the unauthorized Virtual Private Cloud.

        4. Confirm that the Internet Gateway is detached from the unauthorized Virtual Private Cloud by listing the Internet Gateway attachments:

        ```bash theme={null}
        aws ec2 describe-internet-gateways --internet-gateway-ids <internet-gateway-id>
        ```

        5. If needed, delete the Internet Gateway:

        ```bash theme={null}
        aws ec2 delete-internet-gateway --internet-gateway-id <internet-gateway-id>
        ```

        Replace `<internet-gateway-id>` with the ID of the Internet Gateway.

        By following these steps, you can remediate the misconfiguration of an Internet Gateway being attached to unauthorized Virtual Private Clouds in AWS using AWS CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of an Internet Gateway being attached to an unauthorized Virtual Private Cloud (VPC) in AWS using Python, you can follow these steps:

        1. Install the Boto3 library: Boto3 is the AWS SDK for Python and allows you to interact with AWS services. You can install it using pip with the following command:
           ```
           pip install boto3
           ```

        2. Use the following Python script to detach the Internet Gateway from the unauthorized VPC and attach it to the correct VPC:

        ```python theme={null}
        import boto3

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

        # Define the IDs of the unauthorized VPC and the correct VPC
        unauthorized_vpc_id = 'YOUR_UNAUTHORIZED_VPC_ID'
        correct_vpc_id = 'YOUR_CORRECT_VPC_ID'

        # Get the Internet Gateway ID attached to the unauthorized VPC
        response = ec2_client.describe_internet_gateways(Filters=[{'Name': 'attachment.vpc-id', 'Values': [unauthorized_vpc_id]}])
        igw_id = response['InternetGateways'][0]['InternetGatewayId']

        # Detach the Internet Gateway from the unauthorized VPC
        ec2_client.detach_internet_gateway(InternetGatewayId=igw_id, VpcId=unauthorized_vpc_id)

        # Attach the Internet Gateway to the correct VPC
        ec2_client.attach_internet_gateway(InternetGatewayId=igw_id, VpcId=correct_vpc_id)

        print('Internet Gateway has been successfully attached to the correct VPC.')
        ```

        3. Replace `'YOUR_UNAUTHORIZED_VPC_ID'` and `'YOUR_CORRECT_VPC_ID'` with the actual IDs of the unauthorized VPC and the correct VPC, respectively.

        4. Run the Python script. This script will detach the Internet Gateway from the unauthorized VPC and attach it to the correct VPC.

        By following these steps, you can remediate the misconfiguration of an Internet Gateway being attached to an unauthorized VPC in AWS using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Internet Gateway remains in the account
        resource "aws_internet_gateway" "this" {
          vpc_id = null # IGW will be attached via aws_internet_gateway_attachment

          tags = {
            Name = "IGW_FOR_AUTHORIZED_VPC" # replace as needed
          }
        }

        # ATTACHMENT TO AUTHORIZED VPC ONLY
        # Replace AUTHORIZED_VPC_ID with the ID of the VPC that is allowed
        resource "aws_internet_gateway_attachment" "authorized" {
          internet_gateway_id = aws_internet_gateway.this.id
          vpc_id              = "AUTHORIZED_VPC_ID" # substitute the authorized VPC ID
        }
        ```

        To remediate an existing IGW that is attached to an unauthorized VPC, remove (or update) the `aws_internet_gateway_attachment` that currently points at the unauthorized VPC ID; Terraform will then:

        * destroy the old attachment (detaching the IGW from the unauthorized VPC), and
        * optionally create the new attachment shown above to the authorized VPC.

        WARNING: Detaching the internet gateway from a VPC removes inbound and outbound internet access for all resources in public subnets that use that gateway. This is a highly disruptive change; apply only during a planned maintenance window after assessing impact.

        This change forces replacement of the `aws_internet_gateway_attachment` resource (detach from old VPC, attach to new or leave unattached), but not the internet gateway itself.

        Verification: `terraform plan` should show the `aws_internet_gateway_attachment` associated with the unauthorized VPC being destroyed (and, if you added the new one, a new attachment to `AUTHORIZED_VPC_ID` being created).
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
