Launch an Amazon AWS instance with Ansible

It is also possible to use Ansible to create an instance on Amazon AWS. I have used the Ansible EC2 Cloud Module to create a CentOS 7 Linux instance. See http://docs.ansible.com/ansible/list_of_cloud_modules.html.

I first set some environment variables for AWS. Like this:

export EC2_URL=https://ec2.amazonaws.com
export AWS_ACCESS_KEY_ID="XXXX"
export AWS_SECRET_ACCESS_KEY="XXXXXXXXXX"
export AWS_REGION="eu-west-1"

Then I created a playbook create-aws-centos.yml to launch a new AWS instance with CentOS.

- hosts: localhost
  connection: local
  gather_facts: yes
  tasks:
  - name: Create image
    ec2:
      key_name: xxxxx-keypair-eu-ie
      group: default
      instance_type: t2.micro
      image: ami-7abd0209
      wait: yes
      wait_timeout: 500
      count: 1
      region: eu-west-1
      state: present
      volumes:
      - device_name: /dev/sda1
        volume_type: gp2
        volume_size: 8
      vpc_subnet_id: subnet-0ed78757
      assign_public_ip: yes
    register: ec2

  - debug:
      var: ec2

The command used to launch the playbook:

ansible-playbook create-aws-centos.yml -vvv

Now you can verify the result in the EC2 Management Console at the Instances.

References:
https://aws.amazon.com/
https://docs.ansible.com/ansible/