Automating AWS EC2 Instance Launch with Ansible: A Step-by-Step Guide

Automating AWS EC2 Instance Launch with Ansible: A Step-by-Step Guide

Ansible to launch an AWS EC2 instance and provide you with a practical example.

---
- name: Launch EC2 Instance
  hosts: localhost
  connection: local
  gather_facts: false

  vars:
    aws_region: us-west-2
    instance_type: t2.micro
    ami_id: ami-0c55b159cbfafe1f0  # Amazon Linux 2 AMI (may vary by region)
    key_name: your_key_pair_name
    security_group: your_security_group_id
    subnet_id: your_subnet_id

  tasks:
    - name: Provision EC2 instance
      amazon.aws.ec2_instance:
        instance_type: "{{ instance_type }}"
        image_id: "{{ ami_id }}"
        region: "{{ aws_region }}"
        key_name: "{{ key_name }}"
        vpc_subnet_id: "{{ subnet_id }}"
        security_group: "{{ security_group }}"
        network:
          assign_public_ip: yes
        wait: yes
        state: present
      register: ec2

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip_address }}"
        groupname: launched_ec2
      loop: "{{ ec2.instances }}"

    - name: Wait for SSH to come up
      wait_for:
        host: "{{ item.public_dns_name }}"
        port: 22
        delay: 60
        timeout: 320
        state: started
      loop: "{{ ec2.instances }}"

    - name: Print EC2 information
      debug:
        msg: "EC2 instance launched. Public IP: {{ item.public_ip_address }}, Public DNS: {{ item.public_dns_name }}"
      loop: "{{ ec2.instances }}"

To use Ansible to launch an AWS EC2 instance, follow these steps:

  1. Install required packages:

    • Ensure you have Ansible installed on your local machine.

    • Install the Amazon AWS collection for Ansible:

        ansible-galaxy collection install amazon.aws
      
    • Install the required Python packages:

        pip install boto3 botocore
      
  2. Set up AWS credentials:

    • Configure your AWS credentials using environment variables, AWS CLI configuration, or by explicitly providing them in the playbook.
  3. Create the Ansible playbook:

    • Save the playbook content from the artifact above into a file named launch_ec2.yml.
  4. Customize the playbook:

    • Replace the placeholder values in the vars section with your specific AWS configuration:

      • aws_region: The region where you want to launch the instance

      • instance_type: The type of EC2 instance you want to launch

      • ami_id: The ID of the Amazon Machine Image (AMI) you want to use

      • key_name: The name of your EC2 key pair

      • security_group: The ID of the security group to use

      • subnet_id: The ID of the subnet where you want to launch the instance

  5. Run the playbook:

     ansible-playbook launch_ec2.yml
    

This playbook does the following:

  1. Defines the necessary variables for launching an EC2 instance.

  2. Uses the amazon.aws.ec2_instance module to provision the EC2 instance with the specified parameters.

  3. Adds the new instance to a host group for potential further configuration.

  4. Waits for the SSH port to become available, ensuring the instance is fully operational.

  5. Prints out the public IP address and DNS name of the launched instance.

Benefits of using Ansible for launching EC2 instances:

  • Reproducibility: You can version control your infrastructure code.

  • Automation: Easily integrate with CI/CD pipelines for automated deployments.

  • Flexibility: Modify parameters easily to launch different types of instances.

  • Idempotency: Ansible ensures the desired state, so running the playbook multiple times won't create duplicate instances.

Remember to always follow AWS best practices for security and cost management when launching EC2 instances. This includes using appropriate security groups, managing SSH keys securely, and terminating instances when they're no longer needed.