DevOps Bootcamp · Lesson

Launch EC2 Instances Declaratively

Provision compute with amazon.aws.

Lesson 2 of 413 steps

Launch EC2 Instances Declaratively is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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. ✨

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Launch EC2 Instances Declaratively” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Launch EC2 Instances Declaratively”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Launch EC2 Instances Declaratively”?

Provision compute with amazon.aws. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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 DevOps Bootcamp