Connect to EC2 using instance connect endpoint - A SIMPLE GUIDE
Tags: aws, beginners, tutorial, ec2
Published: 2023-09-29 20:47:53

EC2 Instance Connect Endpoint allows you to connect to an instance without requiring the instance to have a public IPv4 address. You can connect to any instances that support TCP.
For more info and its limitations please visit [this link](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/connect-using-eice.html).


To create an EC2 instance connect endpoint open VPC service from the management console, select Endpoints then Create endpoint.

![create endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/35aol2vst5h245pnie2p.jpg)

Fill in the endpoint name, choose EC2 Instance Connect Endpoint. then select the desired VPC.
![endpoint name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzimkrq6owqss5ggsjzg.jpg)

you can use the option "Preserve Client IP" if you want the endpoint to use your IP when establishing the connection (for example if you configured the EC2 instance's security group to only accept connections from your IP)

![Preserve Client IP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1u2tser8fi3dzjekf7x.jpg)

select desired subnet and endpoint security group, no need to open any ports in inbound rules. 
then click create endpoint. note that creation will take some time.
![ceate endpoint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az422fip06shlj0s5qj1.jpg)

To create an EC2 instance, in the management console go to EC2 service. Choose Instances then launch Instances.
![create an EC2 instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/55mkxjmm5i5bxqs2zxru.jpg)

fill in the EC2 instance name and choose the AMI
![ec2 name and ami](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otevgqbt3ta1pns7oteq.jpg)

choose the key pair if you want, it is optional. and select the instance type
![key pair & instance type](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgg6dxv1wal27mp4k9tq.jpg)

choose the VPC and subnet (subnet I choose is private). I disabled the auto assign public IP options as I don't need it. Select the security group and make sure the appropriate port is enabled in the inbound rules (22 for linux and port 3389 for windows). If you limited the source connection to your IP, you need to use an endpoint with option "Preserve Client IP" enabled.
![vpc,subnet & sg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fc4nb4brxy8tyibg9lnz.jpg)


My EC2 instance is now created, in a private subnet with no public IP and no key pair, still I can connect to it using the ec2 instance connect endpoint.

In EC2 service page select your instance and click connect
![select instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/razk3k146hfvg7tm7m29.jpg)

select EC2 Instance Connect, Connect using EC2 Instance Connect Endpoint, choose the end point you created then click connect.
![EC2 Instance Connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1bgv3st3znqu2s2ln9z.jpg)

you are now connected to the EC2 instance
![connected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k59vajpquvh9n4ej0jqf.jpg)

To connect using CLI, you have to install AWS CLI first (check this [link](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) ) , create an access key (check this [link](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html) ) and configure CLI using " aws configure "command.
Afer that use the following command to connect to your EC2 instance
" aws ec2-instance-connect ssh --instance-id i-1234567890example --connection-type eice "
replace i-1234567890example with your EC2 instance id
![CLI connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi27xiz1ag7wsja0wx46.jpg)

**If you face the following error:
aws: error: argument operation: Invalid choice, valid choices are:
send-ssh-public-key | send-serial-console-ssh-public-key help 
please update you AWS CLI and the problem will be solved.**