Review EC2 default VPC use

Check whether the actual EC2 VPC and subnet meet network isolation requirements.

Description

Check which VPC owns the subnet where Ansible will launch your EC2 instances.

Potential impact

Sharing the default VPC can allow unnecessary connections between test and production.

Remediation

If production needs a separate VPC, create a subnet there and pass its ID to vpc_subnet_id. Check that the routes and security group rules allow only the required connections.

Examples

Use a collection that supports the legacy amazon.aws.ec2 module. Supply the VPC lookup results, key pair, AMI, and access rules. Choose an unused subnet CIDR within the VPC.

Before

yaml
- name: Create a subnet in the default VPC
  amazon.aws.ec2_vpc_subnet:
    state: present
    vpc_id: "{{ defaultVPC.vpcs.0.id }}"
    cidr: "{{ ec2_subnet_cidr }}"
    tags:
      Name: Database Subnet
  register: my_subnet

- name: Launch EC2 instances
  amazon.aws.ec2:
    key_name: mykey
    instance_type: t2.micro
    image: "{{ ec2_image_id }}"
    wait: yes
    count: 3
    vpc_subnet_id: "{{ my_subnet.subnet.id }}"
    assign_public_ip: yes

After

yaml
- name: Create a subnet in the application VPC
  amazon.aws.ec2_vpc_subnet:
    state: present
    vpc_id: "{{ myVPC.vpcs.0.id }}"
    cidr: "{{ ec2_subnet_cidr }}"
    tags:
      Name: Database Subnet
  register: my_subnet2

- name: Launch EC2 instances
  amazon.aws.ec2:
    key_name: mykey
    instance_type: t2.micro
    image: "{{ ec2_image_id }}"
    wait: yes
    count: 3
    vpc_subnet_id: "{{ my_subnet2.subnet.id }}"
    assign_public_ip: yes

Both examples still request a public IPv4 address with assign_public_ip: yes. Change that setting for internal instances.

References