- name: Create AWS resources
  hosts: localhost
  connection: local
  gather_facts: False

  collections:
    - amazon.aws

  tasks:

  - name: Setting the correct AMI per us-east-1
    set_fact:
      ami_id: ami-096fda3c22c1c990a
    when: aws_region == "us-east-1"   

  - name: Setting the correct AMI per us-east-1
    set_fact:
      ami_id: ami-09d9c5cdcfb8fc655
    when: aws_region == "us-west-1" 

  - name: create a new ec2 key pair
    ec2_key:
      name: "{{ keypair }}"
      region: "{{ aws_region }}"

  - name: Create VPC 
    ec2_vpc_net:
      name: "{{ vpc_name }}"
      cidr_block: 10.10.0.0/16
      region: "{{ aws_region }}" 
    register: my_vpc  

  - name: Create a security group
    ec2_group:
      name: ansible
      description: "Ansible Security Group"
      region: "{{ aws_region }}"
      vpc_id: "{{ my_vpc.vpc.id }}"
      rules:
        - proto: all
          cidr_ip: 10.10.0.0/16
        - proto: all
          group_name: ansible
      rules_egress:
        - proto: all
          cidr_ip: 0.0.0.0/0
    register: firewall

  - name: Create an EC2 instance
    ec2_instance:
      key_name: "{{ keypair }}"
      region: "{{ aws_region }}"
      security_group: "{{ firewall.group_id }}"
      instance_type: "{{ instance_type }}"
      image_id: "{{ ami_id }}"
      wait: yes
      name: "{{ instance_name }}"
    register: ec2