# Access by ssh to an ec2 instance after creating it

**URL:** <https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [April 28, 2022, 12:10pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572 "2022-04-28T12:10:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emanuel\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/e/b3f665/32.png) [@Emanuel\_Gonzalez](https://forum.ansible.com/u/Emanuel_Gonzalez)\
**Post date:** [April 28, 2022, 12:10pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/1 "2022-04-28T12:10:58Z")

</div>

Hi, I am creating an ec2 instance in AWS, but after creating it I need to log in via SSH and run some tasks on it.

The instance takes a few minutes to be ready, as I do not know how long it takes to be ready and nor the IP assigned by Amazon, my question as SSH login to a newly created instance without knowing its IP and its creation status?  
Regards,

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [April 28, 2022, 12:22pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/2 "2022-04-28T12:22:27Z")

</div>

Hi there! That is more of an AWS question than an Ansible question. I might suggest posting the question on the AWS forums here:

[https://repost.aws/](https://repost.aws/)

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 28, 2022, 5:37pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/3 "2022-04-28T17:37:01Z")

</div>

The community.aws.ec2\_instance module returns facts about the ec2 including interfaces and IPs. Register these facts and you can access it.

```auto

- community.aws.ec2_instance:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
security_token: "{{ session_token }}"
name: "{{ ec2_name }}"
key_name: "{{ ec2_key }}"
vpc_subnet_id: "{{ subnet_id }}"
security_group: "{{ secgroup_id }}"
network:
assign_public_ip: false
image_id: "{{ ami_image_id }}"
instance_type: "{{ ec2_size }}"
volumes: "{{ disk_list }}"
region: "{{ aws_region }}"
register: newec2

- set_fact:

ip_addr: newec2.instances[0].network_interfaces[0].private_ip_address

```

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 28, 2022, 5:39pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/4 "2022-04-28T17:39:37Z")

</div>

Adding to my earlier reply … we then use a wait\_for task to wait for the EC2 to be ready for SSH.

```auto
- name: Wait for connection SSH port 22 to become open
wait_for:
port: 22
connect_timeout: 10
host: "{{ ip_addr }}"
search_regex: OpenSSH
# wait for 30 secs before starting to poll
delay: 30
# wait no more than 10 mins and fail
timeout: 600
sleep: 5
delegate_to: localhost
become: no

```

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 29, 2022, 11:51am UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/5 "2022-04-29T11:51:07Z")

</div>

We use the ec2\_instance module as well, but we use the build-in 'wait' parameter which works well:

[https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2\_instance\_module.html#parameter-wait](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_instance_module.html#parameter-wait)

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [April 29, 2022, 5:59pm UTC](https://forum.ansible.com/t/access-by-ssh-to-an-ec2-instance-after-creating-it/36572/6 "2022-04-29T17:59:19Z")

</div>

Our wait step is in a different playbook downstream in a workflow. The upstream step in the workflow can create an EC2 or an ESX VM. We did it this way to be universally applicable. We have a workflow step to create a vm, a workflow step to create a DNS record for that VM, then a workflow step to customize that VM based on parameters coming into the workflow - apache, nginx, tomcat, mysql, etc. The create-vm step is a wrapper playbook that sources other playbooks based on IaaS platform (location) the user selected. We support ESX and AWS, but with this method could easily add Azure and Google and other cloud service providers or hypervisor platforms.

CREATE-VM.YML
