Networking: VPCs, Subnets & Security Groups
Stand up cloud network resources.
Networking: VPCs, Subnets & Security Groups is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Servers Need a Network
Before an instance can talk to anything, it needs a network home: a private network, a subnet, and firewall rules. Ansible builds all three. 🌐
Start With a VPC
A VPC is your isolated private network in AWS. The ec2_vpc_net module creates one from a CIDR block you choose.
amazon.aws.ec2_vpc_net:
name: app-vpc
cidr_block: 10.0.0.0/16CIDR Defines the Address Range
The cidr_block sets how many private IPs the VPC owns. A /16 gives roughly 65,000 addresses to carve up.
Carve Out a Subnet
A subnet is a slice of the VPC, usually one per availability zone. ec2_vpc_subnet creates it inside the VPC.
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpc.vpc.id }}"
cidr: 10.0.1.0/24Public vs Private Subnets
A subnet is public when its routes reach an internet gateway. Otherwise it is private, hidden from the open internet.
Security Groups Are Firewalls
A security group is a stateful firewall around your instances. ec2_security_group defines exactly which traffic is allowed in.
amazon.aws.ec2_security_group:
name: web-sg
description: web tier
vpc_id: "{{ vpc.vpc.id }}"Allow Inbound Rules
The rules list opens ports. Here you let HTTP reach the web tier from anywhere on the internet.
rules:
- proto: tcp
ports: [80]
cidr_ip: 0.0.0.0/0Lock Down SSH
Restrict port 22 to your own IP, not the whole world. A tight cidr_ip keeps attackers off your servers.
rules:
- proto: tcp
ports: [22]
cidr_ip: 203.0.113.5/32Wire It All Together
Register the VPC, pass its id to the subnet and security group, then attach that group to your EC2 instance. Everything connects by id.
amazon.aws.ec2_instance:
name: web-01
security_groups: [web-sg]
vpc_subnet_id: "{{ subnet.subnet.id }}"Order and Idempotency
Build network resources before the instance, and like all cloud modules they are idempotent, so re-running the play is safe.
Reach the Internet With a Gateway
An internet gateway lets a public subnet send traffic out. The ec2_vpc_igw module attaches one to your VPC.
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpc.vpc.id }}"
state: presentQuick Check
You want only HTTP traffic to reach your web servers. Which resource controls that?
Recap
You built the network: a VPC for isolation, a subnet for placement, and a security group firewall, then attached them to your instance. ✨
Frequently asked questions
Is the “Networking: VPCs, Subnets & Security Groups” lesson free?
Yes — the full text of “Networking: VPCs, Subnets & Security Groups” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Networking: VPCs, Subnets & Security Groups”?
Stand up cloud network resources. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Networking: VPCs, Subnets & Security Groups” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Cloud Collections & Authentication
- Launch EC2 Instances Declaratively
- Networking: VPCs, Subnets & Security Groups
- Provision Then Configure in One Flow