<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Aanidhay Aggarwal</title>
    <description>The latest articles on Forem by Aanidhay Aggarwal (@aanidhay).</description>
    <link>https://forem.com/aanidhay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3093030%2Fd28cb08a-d234-44bd-b065-e1117c49d6b5.jpg</url>
      <title>Forem: Aanidhay Aggarwal</title>
      <link>https://forem.com/aanidhay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aanidhay"/>
    <language>en</language>
    <item>
      <title>Mastering AWS with Ansible: 10 Essential Commands for Cloud Automation 🚀</title>
      <dc:creator>Aanidhay Aggarwal</dc:creator>
      <pubDate>Sat, 26 Apr 2025 11:28:29 +0000</pubDate>
      <link>https://forem.com/aanidhay/mastering-aws-with-ansible-10-essential-commands-for-cloud-automation-3b63</link>
      <guid>https://forem.com/aanidhay/mastering-aws-with-ansible-10-essential-commands-for-cloud-automation-3b63</guid>
      <description>&lt;p&gt;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 &lt;strong&gt;10 essential Ansible commands&lt;/strong&gt; 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.&lt;/p&gt;

&lt;p&gt;Let’s dive into why Ansible is ideal for AWS and explore these powerful commands!&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why Ansible for AWS?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ansible excels for AWS management because of:&lt;/p&gt;

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

&lt;p&gt;Below are 10 essential commands to automate key AWS tasks efficiently.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠 &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before you begin, ensure:&lt;/p&gt;

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


&lt;h2&gt;
  
  
  🧠 &lt;strong&gt;10 Essential Ansible Commands for AWS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here are 10 essential Ansible commands for managing AWS resources, each with a playbook task and use case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Launch EC2 Instance with Custom Tags&lt;/strong&gt;&lt;br&gt;
Provision an EC2 instance with specific tags for organization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Deploy servers for production web applications.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;2.Create S3 Bucket with Encryption&lt;/strong&gt;&lt;br&gt;
Set up an S3 bucket with server-side encryption.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Create S3 bucket
  amazon.aws.s3_bucket:
    name: "my-secure-bucket-2025"
    state: present
    region: "us-east-1"
    encryption: "AES256"
    versioning: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Store sensitive data with encryption and version control.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;3.Upload File to S3 with Metadata&lt;/strong&gt;&lt;br&gt;
Upload a file to S3 with custom metadata.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Automate configuration file uploads with metadata for tracking.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;4.Create IAM Role with Custom Policy&lt;/strong&gt;&lt;br&gt;
Define an IAM role with a custom policy for S3 access.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Grant EC2 instances specific S3 permissions securely.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;5.Provision RDS Instance with Backup&lt;/strong&gt;&lt;br&gt;
Deploy a PostgreSQL RDS instance with automated backups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Set up a reliable database with backup for applications.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;6.Configure Security Group with Egress Rules&lt;/strong&gt;&lt;br&gt;
Create a security group with inbound and outbound rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Secure web servers with controlled network access.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;7.Set Up Auto Scaling Group&lt;/strong&gt;&lt;br&gt;
Create an Auto Scaling group for dynamic scaling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Ensure application availability under varying traffic.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;8.Attach EBS Volume to EC2&lt;/strong&gt;&lt;br&gt;
Attach an EBS volume to an EC2 instance for storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Add persistent storage for application data.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;9.Create VPC with Subnet&lt;/strong&gt;&lt;br&gt;
Provision a VPC with a public subnet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Build isolated network environments for applications.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;10.Deploy Application Load Balancer&lt;/strong&gt;&lt;br&gt;
Set up an ALB for traffic distribution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use case&lt;/strong&gt;: Distribute traffic across EC2 instances for scalability.&lt;/p&gt;




&lt;h2&gt;
  
  
  💡 &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use roles&lt;/strong&gt;: Modularize playbooks for reusability.&lt;br&gt;
&lt;strong&gt;Secure credentials&lt;/strong&gt;: Use Ansible Vault or AWS Secrets Manager.&lt;br&gt;
&lt;strong&gt;Test with &lt;code&gt;--check&lt;/code&gt;&lt;/strong&gt;: Preview changes before applying.&lt;br&gt;
&lt;strong&gt;Tag resources&lt;/strong&gt;: Ensure consistent tagging for tracking.&lt;br&gt;
&lt;strong&gt;Audit with CloudTrail&lt;/strong&gt;: Monitor changes for compliance.&lt;/p&gt;




&lt;h2&gt;
  
  
  ✨ &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;These &lt;strong&gt;10 essential Ansible commands&lt;/strong&gt; 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 &lt;a href="https://dev.to/aanidhay"&gt;Dev.to&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can also connect with me on &lt;a href="https://www.linkedin.com/in/aanidhay-aggarwal-15592b252" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; to know more about me and my journey till now.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy automating from Aanidhay!&lt;/em&gt; 🧑‍💻⚙️🌍&lt;/p&gt;

</description>
      <category>devops</category>
      <category>ansible</category>
      <category>cloud</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
