[AWS] How to ssh into EC2 in VPC ?
3 min readAug 19, 2019
I need debug issue the program on EC2 which is in VPC
這是我的架構:
在這樣的架構下 EC2 都在 Elastic beanstalk 的 load balancer 後,以及和在 VPC 內。該如何連入 EC2 呢 ?
Step1. 為 VPC 的 EC2 設定 Public IP
- 進 AWS EC2 / NETWORK & SECURITY / Security Groups: 建立 security group, inbound 設定讓 SSH TCP 可 pass
- 進 AWS EC2 / NETWORK & SECURITY / Network Interfaces: 建立 interface
- 進 AWS EC2 / NETWORK & SECURITY / Elastic IPs: 建立 Public IP
- 進 AWS EC2 / NETWORK & SECURITY / Network Interfaces: 把此 interface associate 此 public ip
- 進 AWS EC2 / NETWORK & SECURITY / Network Interfaces: 設定此 interface 上面的 security group,設定為 SSH TCP 可 pass
- 進 AWS EC2 / NETWORK & SECURITY / instances: 對該 EC2 attach 此 interface
- 進 AWS EC2 / NETWORK & SECURITY / instances: 確認該 EC2 已獲得 public IP
Step2. 為 VPC 的 private network 設定 route
- 進 AWS VPC / Routing Tables / [private route] / Routes: 設定若目標為 [PC external ip/32] 則用 internet gateway routing 出去 (否則則是 nat routing 出去)
Step3. 幫 EC2 產生 key pair
AWS EC2 / NETWORK & SECURITY / Network Interfaces/ Key Pairs
Step4. 幫 EC2 設定 Key
- 若是由 Elastic beanstalk 設定,則可以在這邊指定 key
Elastic beanstalk / Configuration / Security (指定 key 需要重新 deploy)
Step5. 用 SSH 連入機器
ssh -i /root/.aws/dev-instance-key-pair.pem ec2-user@xx.xx.xx.xx