# aws ec2 instance Permission denied (publickey) after created

**URL:** <https://forum.ansible.com/t/aws-ec2-instance-permission-denied-publickey-after-created/24213>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [April 13, 2017, 12:11am UTC](https://forum.ansible.com/t/aws-ec2-instance-permission-denied-publickey-after-created/24213 "2017-04-13T00:11:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Li\_Zheng](https://avatars.discourse-cdn.com/v4/letter/l/71c47a/32.png) [@Li\_Zheng](https://forum.ansible.com/u/Li_Zheng)\
**Post date:** [April 13, 2017, 12:11am UTC](https://forum.ansible.com/t/aws-ec2-instance-permission-denied-publickey-after-created/24213/1 "2017-04-13T00:11:56Z")

</div>

I am following ec2 module for Ansible example on link [http://docs.ansible.com/ansible/ec2\_module.html](http://docs.ansible.com/ansible/ec2_module.html)

instance is created correctly. However, during **Configure instance** Stage I tried to install apache. It return an error  
fatal: [52.90.207.199]: UNREACHABLE! =\> {“changed”: false, “msg”: “Failed to connect to the host via ssh: Warning: Permanently added.207.199’ (ECDSA) to the list of known hosts.\r\nPermission denied (publickey).\r\n”, “unreachable”: true}

```auto
# Launch instances, runs some tasks
# and then terminate them

- name: Create a sandbox instance
  hosts: localhost
  gather_facts: False
  vars:
    key_name: my_keypair
    instance_type: m1.small
    security_group: my_securitygroup
    image: my_ami_id
    region: us-east-1
  tasks:
    - name: Launch instance
      ec2:
         key_name: "{{ keypair }}"
         group: "{{ security_group }}"
         instance_type: "{{ instance_type }}"
         image: "{{ image }}"
         wait: true
         region: "{{ region }}"
         vpc_subnet_id: subnet-29e63245
         assign_public_ip: yes
      register: ec2

    - name: Add new instance to host group
      add_host:
        hostname: "{{ item.public_ip }}"
        groupname: launched
      with_items: "{{ ec2.instances }}"

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

- name: Configure instance(s)
  hosts: launched
  become: True
  gather_facts: True
  tasks:
    - name: install packages (apache2, git)
      apt: name={{ item }} update_cache=yes state=latest
      with_items:
        - apache2
        - git

```

---

<div class="post-metadata">

**Author:** ![Anderson\_Goulart](https://avatars.discourse-cdn.com/v4/letter/a/ebca7d/32.png) [@Anderson\_Goulart](https://forum.ansible.com/u/Anderson_Goulart)\
**Post date:** [April 14, 2017, 10:10am UTC](https://forum.ansible.com/t/aws-ec2-instance-permission-denied-publickey-after-created/24213/2 "2017-04-14T10:10:24Z")

</div>

Hi,

Ansible is trying to connect (ssh) to the instance with a different user/key that you used to launch the instance. Check for ansible\_ssh\_private\_key\_file

**[http://docs.ansible.com/ansible/intro\_inventory.html](http://docs.ansible.com/ansible/intro_inventory.html)**

**// Anderson**

---

<div class="post-metadata">

**Author:** ![Sudhanshu\_Sharma](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Sudhanshu\_Sharma](https://forum.ansible.com/u/Sudhanshu_Sharma)\
**Post date:** [April 15, 2017, 7:18pm UTC](https://forum.ansible.com/t/aws-ec2-instance-permission-denied-publickey-after-created/24213/3 "2017-04-15T19:18:01Z")

</div>

Try after deleteing **\*/.\*ssh/Known\_Hosts file.**
