Skip to content

Instantly share code, notes, and snippets.

@akjprajapaty
Last active March 5, 2020 11:25
Show Gist options
  • Select an option

  • Save akjprajapaty/918938641c280dd5559d184ae18fed62 to your computer and use it in GitHub Desktop.

Select an option

Save akjprajapaty/918938641c280dd5559d184ae18fed62 to your computer and use it in GitHub Desktop.
# Install ec2-instance-connect
apt-get install ec2-instance-connect
#Create an IAM user with below policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect":"Allow",
"Action":[
"ec2:Describe*",
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:RebootInstances"
],
"Resource":"*"
},
{
"Effect": "Allow",
"Action": "ec2-instance-connect:SendSSHPublicKey",
"Resource": "arn:aws:ec2:region:account-id:instance/i-1234567890abcdef0"
}
]
}
# Configure AWS CLI.
aws configure --profile ec2-instance-connect
AWS Access Key ID [****************7G6U]:
AWS Secret Access Key [****************eE/M]:
Default region name [ap-south-1]:
Default output format [json]:
# Push the ssh public key into the instance using above configured profile.
aws ec2-instance-connect send-ssh-public-key --profile ec2-instance-connect --instance-id i-0021969c8c4a0772d --instance-os-user ec2-user --region ap-south-1 --availability-zone ap-south-1a --ssh-public-key file:///root/.ssh/id_rsa.pub
#We can also export the profile if we don't want to make the command lengthy.
export AWS_DEFAULT_PROFILE=ec2-instance-connect
#Sample Output will be some thing like below one.
#{
# "RequestId": "3cf99cc1-3fb9-453b-xxxx-e5f480asdfgh",
# "Success": true
#}
Reference: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-set-up.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment