Mastering AWS with Ansible

10 Must-Know Commands for Cloud Automation As someone who recently dove into Ansible for managing AWS infrastructure (shoutout to my lab with Amazon Linux instances!), I’ve seen firsthand how this tool transforms cloud management. Ansible, an open-source automation powerhouse, simplifies AWS tasks with its agentless design and YAML-based playbooks. In this post, I’ll share 10 essential Ansible commands that have leveled up my AWS game, perfect for DevOps folks, cloud admins, or developers eager to automate. This guide is for anyone looking to streamline AWS infrastructure management. Let’s explore why Ansible rocks for AWS and the commands that make it shine. Why Ansible for AWS? Ansible’s magic lies in its: No agents needed: Manage nodes without installing software, keeping things lightweight. AWS-native modules: Built-in support for EC2, S3, RDS, IAM, and beyond. Simple YAML playbooks: Write readable code to define infrastructure states. Idempotent execution: Run tasks repeatedly without breaking things. Vibrant community: Tons of AWS-focused modules and playbooks to tap into. From spinning up EC2 instances to securing S3 buckets, Ansible makes AWS management a breeze. Below are 10 commands I’ve found indispensable for AWS automation. Prerequisites Before diving in, ensure: Ansible is installed (pip install ansible or sudo apt install ansible). AWS CLI is set up with credentials (aws configure). boto3 is installed for AWS modules (pip install boto3). An IAM role with permissions for your tasks (learned this the hard way in my lab!). 10 Essential Ansible Commands for AWS 1. Spin Up an EC2 Instance The ec2_instance module launches EC2 instances with custom specs. - name: Launch EC2 instance amazon.aws.ec2_instance: name: "app-server" key_name: "my-key-pair" instance_type: "t2.micro" image_id: "ami-0c55b159cbfafe1f0" region: "us-east-1" vpc_subnet_id: "subnet-12345678" security_group_ids: ["sg-12345678"] state: present register: ec2 Use case: Quickly deploy servers for apps or testing. 2. Terminate an EC2 Instance Clean up resources by terminating instances. - name: Terminate EC2 instance amazon.aws.ec2_instance: instance_ids: ["i-1234567890abcdef0"] region: "us-east-1" state: absent Use case: Shut down unused instances to save costs. 3. Create an S3 Bucket The s3_bucket module sets up S3 buckets for storage. - name: Create S3 bucket amazon.aws.s3_bucket: name: "my-bucket-2025-orange" state: present region: "us-west-2" versioning: true Use case: Store backups, host static sites, or manage data lakes. 4. Upload Files to S3 Push files to S3 with the aws_s3 module. - name: Upload file to S3 amazon.aws.aws_s3: bucket: "my-bucket-2025-orange" object: "configs/app.yaml" src: "/local/configs/app.yaml" mode: put region: "us-west-2" Use case: Automate uploads for configs or assets. 5. Set Up a Security Group Define network rules with ec2_security_group. - name: Create security group amazon.aws.ec2_security_group: name: "app-sg" description: "Security group for app servers" region: "us-east-1" rules: - proto: tcp from_port: 80 to_port: 80 cidr_ip: "0.0.0.0/0" - proto: tcp from_port: 22 to_port: 22 cidr_ip: "203.0.113.0/24" state: present Use case: Lock down EC2 instances with precise access rules. 6. Create IAM Roles The iam_role module manages IAM roles for AWS resources. - name: Create IAM role amazon.aws.iam_role: name: "AppS3AccessRole" assume_role_policy_document: "{{ lookup('file', 'trust-policy.json') }}" managed_policy_arns: - "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess" state: present region: "us-east-1" Use case: Enable EC2 or Lambda to access S3 securely. 7. Deploy an RDS Database The rds_instance module provisions managed databases. - name: Create RDS instance amazon.aws.rds_instance: db_instance_identifier: "app-db" engine: "mysql" instance_class: "db.t3.micro" allocated_storage: 20 master_username: "admin" master_user_password: "SecurePass123" region: "us-east-1" state: present Use case: Set up databases for apps with minimal effort. 8. Start/Stop EC2 Instances Manage instance states to optimize costs. - name: Stop EC2 instance amazon.aws.ec2_instance: instance_ids: ["i-1234567890abcdef0"] region: "us-east-1" state: stopped Use case: Pause dev environments during downtime. 9. Configure Auto Scaling The autoscaling_group module ensures app scalability. - name: Create Auto Scaling group amazon.aws.autoscaling_group: name: "app-asg" launch_template: launch_template_name: "app-templa

Apr 25, 2025 - 20:28
 0
Mastering AWS with Ansible

10 Must-Know Commands for Cloud Automation

As someone who recently dove into Ansible for managing AWS infrastructure (shoutout to my lab with Amazon Linux instances!), I’ve seen firsthand how this tool transforms cloud management. Ansible, an open-source automation powerhouse, simplifies AWS tasks with its agentless design and YAML-based playbooks. In this post, I’ll share 10 essential Ansible commands that have leveled up my AWS game, perfect for DevOps folks, cloud admins, or developers eager to automate.

This guide is for anyone looking to streamline AWS infrastructure management. Let’s explore why Ansible rocks for AWS and the commands that make it shine.

Why Ansible for AWS?

Ansible’s magic lies in its:

  • No agents needed: Manage nodes without installing software, keeping things lightweight.
  • AWS-native modules: Built-in support for EC2, S3, RDS, IAM, and beyond.
  • Simple YAML playbooks: Write readable code to define infrastructure states.
  • Idempotent execution: Run tasks repeatedly without breaking things.
  • Vibrant community: Tons of AWS-focused modules and playbooks to tap into.

From spinning up EC2 instances to securing S3 buckets, Ansible makes AWS management a breeze. Below are 10 commands I’ve found indispensable for AWS automation.

Prerequisites

Before diving in, ensure:

  • Ansible is installed (pip install ansible or sudo apt install ansible).
  • AWS CLI is set up with credentials (aws configure).
  • boto3 is installed for AWS modules (pip install boto3).
  • An IAM role with permissions for your tasks (learned this the hard way in my lab!).

10 Essential Ansible Commands for AWS

1. Spin Up an EC2 Instance

The ec2_instance module launches EC2 instances with custom specs.

- name: Launch EC2 instance
  amazon.aws.ec2_instance:
    name: "app-server"
    key_name: "my-key-pair"
    instance_type: "t2.micro"
    image_id: "ami-0c55b159cbfafe1f0"
    region: "us-east-1"
    vpc_subnet_id: "subnet-12345678"
    security_group_ids: ["sg-12345678"]
    state: present
  register: ec2

Use case: Quickly deploy servers for apps or testing.

2. Terminate an EC2 Instance

Clean up resources by terminating instances.

- name: Terminate EC2 instance
  amazon.aws.ec2_instance:
    instance_ids: ["i-1234567890abcdef0"]
    region: "us-east-1"
    state: absent

Use case: Shut down unused instances to save costs.

3. Create an S3 Bucket

The s3_bucket module sets up S3 buckets for storage.

- name: Create S3 bucket
  amazon.aws.s3_bucket:
    name: "my-bucket-2025-orange"
    state: present
    region: "us-west-2"
    versioning: true

Use case: Store backups, host static sites, or manage data lakes.

4. Upload Files to S3

Push files to S3 with the aws_s3 module.

- name: Upload file to S3
  amazon.aws.aws_s3:
    bucket: "my-bucket-2025-orange"
    object: "configs/app.yaml"
    src: "/local/configs/app.yaml"
    mode: put
    region: "us-west-2"

Use case: Automate uploads for configs or assets.

5. Set Up a Security Group

Define network rules with ec2_security_group.

- name: Create security group
  amazon.aws.ec2_security_group:
    name: "app-sg"
    description: "Security group for app servers"
    region: "us-east-1"
    rules:
      - proto: tcp
        from_port: 80
        to_port: 80
        cidr_ip: "0.0.0.0/0"
      - proto: tcp
        from_port: 22
        to_port: 22
        cidr_ip: "203.0.113.0/24"
    state: present

Use case: Lock down EC2 instances with precise access rules.

6. Create IAM Roles

The iam_role module manages IAM roles for AWS resources.

- name: Create IAM role
  amazon.aws.iam_role:
    name: "AppS3AccessRole"
    assume_role_policy_document: "{{ lookup('file', 'trust-policy.json') }}"
    managed_policy_arns:
      - "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
    state: present
    region: "us-east-1"

Use case: Enable EC2 or Lambda to access S3 securely.

7. Deploy an RDS Database

The rds_instance module provisions managed databases.

- name: Create RDS instance
  amazon.aws.rds_instance:
    db_instance_identifier: "app-db"
    engine: "mysql"
    instance_class: "db.t3.micro"
    allocated_storage: 20
    master_username: "admin"
    master_user_password: "SecurePass123"
    region: "us-east-1"
    state: present

Use case: Set up databases for apps with minimal effort.

8. Start/Stop EC2 Instances

Manage instance states to optimize costs.

- name: Stop EC2 instance
  amazon.aws.ec2_instance:
    instance_ids: ["i-1234567890abcdef0"]
    region: "us-east-1"
    state: stopped

Use case: Pause dev environments during downtime.

9. Configure Auto Scaling

The autoscaling_group module ensures app scalability.

- name: Create Auto Scaling group
  amazon.aws.autoscaling_group:
    name: "app-asg"
    launch_template:
      launch_template_name: "app-template"
      version: "$Latest"
    min_size: 2
    max_size: 4
    desired_capacity: 2
    vpc_zone_identifier: ["subnet-12345678", "subnet-87654321"]
    region: "us-east-1"
    state: present

Use case: Keep web apps available under varying loads.

10. Execute Commands on EC2

Run shell commands on instances via the command module.

- name: Update EC2 packages
  ansible.builtin.command: "sudo yum update -y"
  delegate_to: "{{ ec2_instance_public_ip }}"
  become: true

Use case: Automate system updates or software installs.

Best Practices for Ansible on AWS

  • Modularize with roles: Structure playbooks for reuse.
  • Secure secrets: Use Ansible Vault or IAM roles for credentials.
  • Dry-run playbooks: Test with --check to avoid surprises.
  • Tag everything: Track AWS resources with consistent tags.
  • Audit actions: Pair with CloudTrail for change monitoring.

Conclusion

After setting up my first Ansible lab on AWS, I’m hooked on its power to automate cloud tasks. These 10 commands—from launching EC2 instances to scaling apps—have made managing AWS infrastructure faster and more reliable. Try them in your next project and share your results in the comments! For more DevOps adventures, follow me on Dev.to.

Happy automating from oringejooz!