The aws_ec2 Inventory Plugin
Pull EC2 hosts by tag automatically.
The aws_ec2 Inventory Plugin is a free Ansible Academy lesson on CoddyKit — lesson 2 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.
Meet aws_ec2
The amazon.aws.aws_ec2 plugin queries the EC2 API and turns your running instances into Ansible hosts automatically.
Install the Collection First
The plugin lives in the amazon.aws collection. Install it from Galaxy before you can enable or use it.
ansible-galaxy collection install amazon.awsIt Needs boto3
Under the hood aws_ec2 uses the AWS SDK for Python. Install boto3 and botocore so the plugin can talk to AWS.
pip install boto3 botocoreThe Minimal Config
A working config just needs the plugin key and the regions to scan. Ansible does the rest on every inventory load.
plugin: amazon.aws.aws_ec2
regions:
- eu-west-1Credentials Come from AWS
The plugin reads standard AWS credentials, from environment variables, ~/.aws config, or an instance role, just like the AWS CLI does.
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...Filter Which Instances Appear
Use filters to limit results, for example only running instances, so terminated boxes never enter your inventory.
filters:
instance-state-name: runningDefault Hostnames Are Ugly
By default hosts may appear as raw IPs or IDs. The hostnames key lets you choose a friendlier name source.
hostnames:
- tag:Name
- private-ip-addressEvery Tag Becomes a Variable
EC2 tags and instance attributes arrive as host variables, so a task can read tags.Environment straight off each host.
Connect via Private or Public IP
Set the connection address with hostnames or compose. Pick private IPs inside a VPC, public IPs when reaching in from outside.
Verify the Discovery
Run ansible-inventory with --graph to see every instance the plugin found, grouped and named, before you launch a play.
ansible-inventory -i prod.aws_ec2.yml --graphOne Config Per Environment
Keep separate plugin files like prod.aws_ec2.yml and staging.aws_ec2.yml, each scoped by region or filters, to isolate environments.
Quick Check
Recall what the aws_ec2 plugin depends on.
Recap
The aws_ec2 plugin needs the amazon.aws collection plus boto3, reads AWS credentials, and turns filtered EC2 instances into named hosts. 🚀
Frequently asked questions
Is the “The aws_ec2 Inventory Plugin” lesson free?
Yes — the full text of “The aws_ec2 Inventory Plugin” 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 “The aws_ec2 Inventory Plugin”?
Pull EC2 hosts by tag automatically. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The aws_ec2 Inventory Plugin” 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
- Static Limits & the Inventory Plugin Model
- The aws_ec2 Inventory Plugin
- constructed: Group Hosts by Tags
- Caching & Multiple Inventory Sources