title: Allocate EC2 through Ansible
body: <p>When creating an EC2 instance through Ansible, how do you specify the security group (this is for a Amazon VPC environment that is not the account default)?  In my case, I am attempting to assign a security group (that currently exists) to my webserver EC2 instance that restricts traffic to only the traffic coming from the ELB that sits in front of it.  If I try the following:</p>
  
  <pre><code>- name: Create webserver instance
    ec2: 
      key_name: "{{ project_name }}-{{ env }}-key"
      image: "{{ image }}"
      instance_type: "{{ instance_type }}"
      instance_tags: '{"Name":"{{ project_name }}-{{ env }}-{{ webserver_name }}","Owner":"{{ project_name }}", "Type":"{{ webserver_name }}","Environment":"{{ env }}"}'
      region: "{{ aws_region }}"
      group: "{{ security_group_name }}"
      wait: true
    register: ec2
  </code></pre>
  
  <p>where {{ security_group_name }} is the 'Group Name' found in the AWS console, I receive the following error: 'Value () for parameter groupId is invalid. The value cannot be empty'</p>
  
  <p>If I try the following:</p>
  
  <pre><code>- name: Create webserver instance
    ec2: 
      key_name: "{{ project_name }}-{{ env }}-key"
      image: "{{ image }}"
      instance_type: "{{ instance_type }}"
      instance_tags: '{"Name":"{{ project_name }}-{{ env }}-{{ webserver_name }}","Owner":"{{ project_name }}", "Type":"{{ webserver_name }}","Environment":"{{ env }}"}'
      region: "{{ aws_region }}"
      group_id: "{{ security_group_id }}"
      wait: true
    register: ec2
  </code></pre>
  
  <p>where {{ security_group_id }} is the 'Group Id' found in the AWS console (such as sg-xxxxxx), I receive the same error.  The Ansible documentation stated that 'group' and 'group_id' are for the specification of the security group (<a href="http://docs.ansible.com/ansible/ec2_module.html" rel="nofollow">http://docs.ansible.com/ansible/ec2_module.html</a>).</p>
  
  <p>The only thing I can think of is that AWS cannot find the security group because I am creating it and it does not know the VPC to place it in or it is placing it in my default VPC and cannot find the security group as it is in a different VPC.</p>
  
  <p>So, maybe a better question is, how to I specify the VPC for a particular EC2 instance (when I have multiple VPCs in a region)?</p>