Visual Studio Code で Remote SSH



# EC2インスタンスの構築
## 

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/925121b4-cf2e-8816-bc29-777037b61d78.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/528f9c06-63ff-dd02-c1d0-1765f7b5fcca.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/8c2f7813-11d8-b102-caae-e6ca435d8c3a.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/0e273bca-66d2-f78d-992b-6c858ab3069b.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/c65069b9-9393-6625-b4be-2c0ba75d487a.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/c3ef0ad6-4543-2cf5-24da-7e2d31c7fe6b.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/06ed3624-c120-83c4-62a6-c0cee27c5504.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/ed0d7bc6-fe7e-e539-5739-fe6687707b1e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/b1f4a01c-a202-ae13-390b-c208ada2ea5c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/6b7a2003-6a4e-d0bc-6c4e-2df337b6640c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/09e144ad-859b-f731-38f5-b6b1b80e595c.png)


# Remote SSHの前準備
```Terminal:Terminal
mv Downloads/remote_ssh.pem .ssh/
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/bf0691dc-b3ad-74d3-07de-f8bfccbfb6a3.png)


```Terminal:Terminal
[~]$ cd .ssh
[.ssh]$ cat config
# Test
Host Test-EC2
  HostName 54.95.77.94
  IdentityFile ~/.ssh/rails_react_todo_app.pem
  User ec2-user

Host Remote-SSH
  HostName 54.249.53.47
  IdentityFile ~/.ssh/remote_ssh.pem
  User ec2-user
```
`.ssh/config`に図の内容を追加してください。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/ffda2f1b-0748-7184-f5d8-e274fa35444b.png)


HostNameはパブリックIPです。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/59a5c158-7721-b07c-46ff-a456c18d2b3a.png)

```
[.ssh]$ ssh -i ~/.ssh/remote_ssh.pem ec2-user@54.249.53.47
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/XXXXXXXXXXXXX/.ssh/remote_ssh.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/XXXXXXXXXXXXX/.ssh/remote_ssh.pem": bad permissions
ec2-user@54.249.53.47: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
```
上記のメッセージが出力されるはず。権限の変更を行う。

```
$ sudo chmod 600 ~/.ssh/remote_ssh.pem
```

# VS code Remote SSHのインストール
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/aa610528-2adb-4583-5958-95862b0db7a3.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/8dc23750-f057-ba70-1ae4-72135d21e49c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/99cd8903-cdd8-2f3c-f174-89c962f54e16.png)

Open Folderして画像のようになっていたら繋がっているはず

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/645146/3b1120ec-0468-39f0-f463-36a104dfa206.png)