Review EC2 VPC subnet selection

Verify that EC2 uses the intended subnet and network access policies.

Description

When you omit vpc_subnet_id, EC2 may launch the instances in a default subnet. Launch can fail if no suitable default subnet exists.

Potential impact

An unintended subnet can allow connections between test and production.

Remediation

Set vpc_subnet_id to the intended subnet ID. Check the VPC’s security group, subnet routes, and public IP assignment against the connections the workload needs.

Examples

Use a collection that supports the legacy amazon.aws.ec2 module. Supply an AMI, key pair, subnet, and a webserver security group in the target VPC.

Before

yaml
- name: Launch EC2 instances
  amazon.aws.ec2:
    key_name: mykey
    instance_type: t2.micro
    image: "{{ ec2_image_id }}"
    wait: yes
    group: webserver
    count: 3
    assign_public_ip: yes

After

yaml
- name: Launch EC2 instances
  amazon.aws.ec2:
    key_name: mykey
    instance_type: t2.micro
    image: "{{ ec2_image_id }}"
    wait: yes
    group: webserver
    count: 3
    vpc_subnet_id: subnet-29e63245
    assign_public_ip: yes

Both examples request public IPv4 addresses with assign_public_ip: yes.

References