0Pricing
Ansible Academy · Lesson

Launch EC2 Instances Declaratively

Provision compute with amazon.aws.

Launch EC2 Instances Declaratively 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.

From Clicking to Declaring

Instead of clicking through the AWS console, you describe the instance you want and let Ansible make reality match it.

The ec2_instance Module

The amazon.aws.ec2_instance module creates and manages EC2 virtual machines straight from a playbook task.

- name: Launch a server
  amazon.aws.ec2_instance:
    name: web-01
    state: present

Choose an AMI

An image_id tells AWS which base image to boot, like a specific Ubuntu or Amazon Linux AMI in your region.

amazon.aws.ec2_instance:
  name: web-01
  image_id: ami-0abcdef1234567890

Pick an Instance Type

The instance_type sets the size and cost, such as t3.micro for small workloads or m5.large for heavier ones.

amazon.aws.ec2_instance:
  name: web-01
  instance_type: t3.micro

Attach an SSH Key

Set key_name to an existing EC2 key pair so you can SSH into the new box once it boots.

amazon.aws.ec2_instance:
  name: web-01
  key_name: my-keypair

Tag Everything

Add tags to label instances by role or environment. Tags power billing, search, and dynamic inventory grouping later.

amazon.aws.ec2_instance:
  name: web-01
  tags:
    role: web
    env: prod

State Drives Behavior

The state key controls the lifecycle: present to ensure it exists, running to start it, or absent to terminate it.

amazon.aws.ec2_instance:
  name: web-01
  state: running

It Is Idempotent

Run the task twice and Ansible matches on the instance name or tags. The second run reports ok and creates nothing new.

Launch Several at Once

The count parameter spins up multiple identical instances in one task, perfect for a small fleet of web nodes.

amazon.aws.ec2_instance:
  name: web
  count: 3
  instance_type: t3.micro

Capture the Result

register saves the module output so you can read back the new public IPs and instance IDs in later tasks.

- amazon.aws.ec2_instance:
    name: web-01
  register: ec2

Terminate When Done

Cleanup is just another declared state. Set state: absent and Ansible terminates the matching instance for you.

amazon.aws.ec2_instance:
  name: web-01
  state: absent

Quick Check

You want exactly one EC2 instance to exist, no matter how often you run the play. Which key ensures that?

Recap

You launched EC2 declaratively with amazon.aws.ec2_instance: pick an AMI, type and key, tag it, set state, and register the result. ✨

Frequently asked questions

Is the “Launch EC2 Instances Declaratively” lesson free?

Yes — the full text of “Launch EC2 Instances Declaratively” 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 “Launch EC2 Instances Declaratively”?

Provision compute with amazon.aws. 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 “Launch EC2 Instances Declaratively” 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

  1. Cloud Collections & Authentication
  2. Launch EC2 Instances Declaratively
  3. Networking: VPCs, Subnets & Security Groups
  4. Provision Then Configure in One Flow
← Back to Ansible Academy