Ubuntu EC2 instance connect for Cli SSH with Azure-AD SSO or for traditional SSH access.
SETTING-UP EC2 INSTANCE CONNECT FOR CLI SSH ACCESS UBUNTU
There are different ways of gaining SSH access for EC2 instances here we will see SSH access for EC2 instance through Cli. This method best suits if you use Azure-AD SSO with AWS.
Setting up EC2 instance connect:
EC2 instance connect needs followings,
1. Amazon Linux 2 of any version OR
2. Ubuntu 16.04 or later.
For Ubuntu:
First connect to your EC2 instance with Sudo access and become Sudo.
then update and upgrade your instance
sudo apt update -y && sudo apt upgrade -y
sudo apt install ec2-instance-connect -y
Now attach a policy like this to the IAM user in order to send the instance SSH key.
Replace the AWS-ACCOUNTID with your ID. You can also customise based on your need like access to certain instances. This role gives access to all instances in your account.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "ec2-instance-connect:SendSSHPublicKey",
"Resource": "arn:aws:ec2:us-east-1:AWS-AccountID:instance/*",
"Condition": {
"StringEquals": {
"ec2:osuser": [
"thuvaragesh"
]
}
}
}
]
}
For traditional SSH:
Now in your local system install AWS-Cli.
1. $ sudo apt-get update
2. $ sudo apt-get install awscli
3. check the version
$ aws --version
4. then configure aws-cli using
$ aws configure
you can give your AWS details there and configure once done use the following command to send your local computer ssh key to EC2 instance.
aws ec2-instance-connect send-ssh-public-key \
--instance-id i-0b35 \
--availability-zone us-west-1c \
--instance-os-user thuvaragesh \
--ssh-public-key file://~/.ssh/my_rsa_key.pub
This will send your SSH key to EC2 instance and the output will be TRUE.
Now SSH using below command,
ssh -i ~/.ssh/my_rsa_key.pub thuvaragesh@3.95.175.19 -p 50
For Azure-AD SSO:
First install AWS-Azure-login you can refer here [https://hibern8e.blogspot.com/2020/12/azure-ad-integration-with-aws-for.html]
Now configure AWS-Azure-login using
$ aws-azure-login
and provide all your Azure details. Then use the same command as above to send your SSH key to EC2 instance.
connect using SSH by,
ssh -i ~/.ssh/my_rsa_key.pub thuvaragesh@3.95.175.19 -p 50
Comments