SSH to EC2 with Auto Assigned Certificates
在開始使用 EC2 後,每次都要找到 EC2 的 certificate 帶入 ssh 的指令裡真ㄉ很麻煩,剛好在上次上課的時候老師教了 ssh 的設定,讓我們在之後連入 EC2 都可以使用原本電腦裡的 ssh key、並透過 ssh i-[EC2 id]
就能連到 EC2 裡ㄌ,比原本的方法方便許多!
Create qualified EC2
在 EC2 的部分,主要是需要將 Session Manager 的 role 指定給我們想要連線的 EC2
- Create new EC2 instances(optional)
- Assigning IAM role with Session Manager access
- Policy name:
AmazonEC2RoleforSSM
- Policy name:
新增使用者
同時我們必須要指定,今天連進 EC2 的是哪一個 user
- 在 AWS IAM 新增一個 user,該 user 要有
AdministratorAccess
-
aws configure --profile default
新增一個 default 的 aws user- USER_KEY =
[Access key ID]
- USER_SECRET =
[Secret access key]
- USER_KEY =
設定本地端 SSH
最後就是比較麻煩的 ssh 設定,參考下面的這個 script
- Ref: https://github.com/qoomon/aws-ssm-ec2-proxy-command
- 複製 aws-ssm-ec2-proxy-command.sh 到
.ssh
下 chmod +x ~/.ssh/aws-ssm-ec2-proxy-command.sh
-
實際使用:以 default IAM user 連進 EC2
AWS_PROFILE=default ssh ec2-user@i-0c45c33713e830c9d \ -i "~/.ssh/id_ed25519" \ -o ProxyCommand="~/.ssh/aws-ssm-ec2-proxy-command.sh %h %r %p ~/.ssh/id_ed25519.pub"
-
加入
.ssh/config
host i-* mi-* User ec2-user IdentityFile ~/.ssh/id_ed25519 ProxyCommand ~/.ssh/aws-ssm-ec2-proxy-command.sh %h %r %p ~/.ssh/id_ed25519.pub StrictHostKeyChecking no
- 設定完後與 EC2 連線:
ssh i-072f2dd63ae1189e9
完成啦!!!🥳🥳🥳🥳