Ansible is a powerhouse for managing AWS infrastructure, turning complex cloud tasks into simple, automated workflows. Its agentless design and YAML-based playbooks make it perfect for provisioning and configuring AWS resources like EC2, S3, IAM, and VPCs. In this guide, I’ll share 10 essential Ansible commands that focus on critical AWS management tasks, from launching EC2 instances to setting up load balancers. These are must-knows for DevOps engineers, cloud admins, or developers looking to streamline their AWS operations.

Let’s dive into why Ansible is ideal for AWS and explore these powerful commands!


Why Ansible for AWS?

Ansible excels for AWS management because of:

  • Agentless architecture: No software needed on managed nodes.
  • AWS-native modules: Built-in support for EC2, S3, IAM, RDS, and more.
  • Readable YAML: Playbooks are easy to write and understand.
  • Idempotent execution: Safe to rerun tasks without side effects.
  • Community resources: Extensive AWS modules and playbooks available.

Below are 10 essential commands to automate key AWS tasks efficiently.


🛠 Prerequisites

Before you begin, ensure:

Ansible installed: pip install ansible or sudo apt install ansible.
AWS CLI configured: Run aws configure with access key, secret key, and region.
boto3 installed: pip install boto3 for AWS modules.
IAM permissions: IAM role/user with access to EC2, S3, IAM, etc.
SSH key pair: For EC2 management.
Terminal: Ready for commands.


🧠 10 Essential Ansible Commands for AWS

Here are 10 essential Ansible commands for managing AWS resources, each with a playbook task and use case.

1.Launch EC2 Instance with Custom Tags
Provision an EC2 instance with specific tags for organization.

- name: Launch EC2 instance
  amazon.aws.ec2_instance:
    name: "web-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"]
    tags:
      Environment: "prod"
      App: "web"
    state: present
  register: ec2

Use case: Deploy servers for production web applications.


2.Create S3 Bucket with Encryption
Set up an S3 bucket with server-side encryption.

- name: Create S3 bucket
  amazon.aws.s3_bucket:
    name: "my-secure-bucket-2025"
    state: present
    region: "us-east-1"
    encryption: "AES256"
    versioning: true

Use case: Store sensitive data with encryption and version control.


3.Upload File to S3 with Metadata
Upload a file to S3 with custom metadata.

- name: Upload file to S3
  amazon.aws.aws_s3:
    bucket: "my-secure-bucket-2025"
    object: "app/config.yaml"
    src: "/local/app/config.yaml"
    mode: put
    metadata:
      Environment: "prod"
      Owner: "devops"
    region: "us-east-1"

Use case: Automate configuration file uploads with metadata for tracking.


4.Create IAM Role with Custom Policy
Define an IAM role with a custom policy for S3 access.

- name: Create IAM role
  amazon.aws.iam_role:
    name: "AppS3AccessRole"
    assume_role_policy_document: "{{ lookup('file', 'trust-policy.json') }}"
    inline_policies:
      S3Access:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Action: ["s3:GetObject", "s3:PutObject"]
            Resource: "arn:aws:s3:::my-secure-bucket-2025/*"
    state: present
    region: "us-east-1"

Use case: Grant EC2 instances specific S3 permissions securely.


5.Provision RDS Instance with Backup
Deploy a PostgreSQL RDS instance with automated backups.

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

Use case: Set up a reliable database with backup for applications.


6.Configure Security Group with Egress Rules
Create a security group with inbound and outbound rules.

- name: Create security group
  amazon.aws.ec2_security_group:
    name: "web-sg"
    description: "Security group for web 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"
    rules_egress:
      - proto: all
        cidr_ip: "0.0.0.0/0"
    state: present

Use case: Secure web servers with controlled network access.


7.Set Up Auto Scaling Group
Create an Auto Scaling group for dynamic scaling.

- name: Create Auto Scaling group
  amazon.aws.autoscaling_group:
    name: "web-asg"
    launch_template:
      launch_template_name: "web-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: Ensure application availability under varying traffic.


8.Attach EBS Volume to EC2
Attach an EBS volume to an EC2 instance for storage.

- name: Attach EBS volume
  amazon.aws.ec2_vol:
    instance: "i-1234567890abcdef0"
    device_name: "/dev/xvdf"
    volume_size: 10
    volume_type: "gp3"
    region: "us-east-1"
    state: present

Use case: Add persistent storage for application data.


9.Create VPC with Subnet
Provision a VPC with a public subnet.

- name: Create VPC
  amazon.aws.ec2_vpc_net:
    name: "my-vpc"
    cidr_block: "10.0.0.0/16"
    region: "us-east-1"
    state: present
  register: vpc
- name: Create subnet
  amazon.aws.ec2_vpc_subnet:
    vpc_id: "{{ vpc.vpc.id }}"
    cidr: "10.0.1.0/24"
    region: "us-east-1"
    state: present

Use case: Build isolated network environments for applications.


10.Deploy Application Load Balancer
Set up an ALB for traffic distribution.

- name: Create Application Load Balancer
  amazon.aws.elb_application_lb:
    name: "web-alb"
    subnets: ["subnet-12345678", "subnet-87654321"]
    security_groups: ["sg-12345678"]
    scheme: "internet-facing"
    region: "us-east-1"
    state: present

Use case: Distribute traffic across EC2 instances for scalability.


💡 Best Practices

Use roles: Modularize playbooks for reusability.
Secure credentials: Use Ansible Vault or AWS Secrets Manager.
Test with --check: Preview changes before applying.
Tag resources: Ensure consistent tagging for tracking.
Audit with CloudTrail: Monitor changes for compliance.


Conclusion

These 10 essential Ansible commands make AWS management efficient and scalable, from provisioning EC2 and RDS to configuring VPCs and load balancers. They’ve streamlined my cloud workflows, and I hope they do the same for you. Try them in your AWS environment and share your automation journey in the comments! For more DevOps adventures, follow me on Dev.to.

You can also connect with me on LinkedIn to know more about me and my journey till now.

Happy automating from Aanidhay! 🧑‍💻⚙️🌍