Access to AWS EC2 instance by local Linux machine

Guide for Linux Ubuntu

This method uses IAM credentials and does not require opening port 22 on your EC2 instance. This is the safest method to connect to an EC2 instance from a local machine.

AWS CLI

Install AWS CLI on your machine:

sudo apt install awscli

Check:

aws --version

session-manager-plugin

Download the .deb package of session-manager-plugin.

x86_64

curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"

Install it

sudo dpkg -i session-manager-plugin.deb

Verify the Installation

After installing, run this command to confirm it works:

session-manager-plugin

or check the version:

session-manager-plugin --version

AWS configure

Now we have to configure aws configure in the terminal on the local machine, but before that, we have to make sure we have created an IAM user and created an access key.

IAM user

Go to IAM > IAM users on AWS and create a user:

  • Give it a name, e.g. admin-user
  • Attach the policy: AdministratorAccess

Access key

After the IAM user is created, go to the created IAM user and go to the Security credentials tab.

  • Click Create access key and choose the CLI purpose
  • Go further and get your access and secret keys.

aws configure

Now, you can configure aws configure in the local terminal. Type:

aws configure

and fill in every step:

AWS Access Key ID [None]: your-access-key
AWS Secret Access Key [None]: your-secret-key
Default region name [None]: us-east-1
Default output format [None]: json

This creates two files on your machine:

  • ~/.aws/credentials
    — stores your keys
  • ~/.aws/config
    — stores the region and output format

Connect to EC2 from a local machine

At last, we are going to do a simple step to connect to the EC2 instance.

  • Copy the instance ID from EC2 > Instances (it starts with i-xxx....)

Replace i-xxxxxxxxxxxxxxxxx with your instance ID and run the command.

aws ssm start-session --target i-xxxxxxxxxxxxxxxxx

Congrats! You have connected to your AWS EC2 instance in the most secure way, without opening port 22 on your instance.

Leave a Reply

Your email address will not be published. Required fields are marked *.