Google Cloud PlatformにMacからsshする方法

## 前提条件

* GoogleCloudPlatform利用開始済み
* プロジェクト作成済み

## Cloud SDK Install

pythonがインストールされているかを確認する

```
$ python -v
```

google-cloud-sdkをダウンロードする

https://cloud.google.com/sdk/docs/quickstart-mac-os-x?hl=ja

ダウンロードしたtarを任意の位置に配置して下記のコマンドを実行

```
$ ./google-cloud-sdk/install.sh
```

gcloudが実行できるか確認する

```
$ gcloud -v
Google Cloud SDK 180.0.0
bq 2.0.27
core 2017.11.10
gsutil 4.28
Updates are available for some Cloud SDK components.  To install them,
please run: gcloud components update
```

実行出来ない場合は、PATHを通すこと

```
$ vi ~/.bash_profile
```

```
export PATH=$PATH:{先ほど配置した場所}google-cloud-sdk/bin
```

下記を実行してPATHを反映する

```
$ source ~./bash_profile
```

gcloudコマンドが実行できることを確認したら下記コマンドにて初期設定を行う
ブラウザに認証ページが表示されたら、GCPアカウントで認証を行う

```
$ gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                                                                                                                                                        
Reachability Check passed.
Network diagnostic (1/1 checks) passed.

Choose the account you would like to use to perform operations for 
this configuration:
 [1] {作成済みアカウント}
 [2] Log in with a new account
Please enter your numeric choice:  1

You are logged in as: [{作成済みアカウント}].

Pick cloud project to use: 
 [1] {作成済みProject}
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1

Your current project has been set to: [{作成済みProject}].

Do you want to configure Google Compute Engine 
(https://cloud.google.com/compute) settings (Y/n)?  Y

Which Google Compute Engine zone would you like to use as project 
default?
If you do not specify a zone via a command line flag while working 
with Compute Engine resources, the default is assumed.
 [1] us-east1-b
 [2] us-east1-c
 [3] us-east1-d
 [4] us-east4-c
 [5] us-east4-b
 [6] us-east4-a
 [7] us-central1-c
 [8] us-central1-a
 [9] us-central1-f
 [10] us-central1-b
 [11] us-west1-b
 [12] us-west1-c
 [13] us-west1-a
 [14] europe-west4-a
 [15] europe-west4-b
 [16] europe-west4-c
 [17] europe-west1-b
 [18] europe-west1-d
 [19] europe-west1-c
 [20] europe-west3-b
 [21] europe-west3-c
 [22] europe-west3-a
 [23] europe-west2-c
 [24] europe-west2-b
 [25] europe-west2-a
 [26] asia-east1-b
 [27] asia-east1-a
 [28] asia-east1-c
 [29] asia-southeast1-b
 [30] asia-southeast1-a
 [31] asia-southeast1-c
 [32] asia-northeast1-b
 [33] asia-northeast1-c
 [34] asia-northeast1-a
 [35] asia-south1-c
 [36] asia-south1-b
 [37] asia-south1-a
 [38] australia-southeast1-b
 [39] australia-southeast1-c
 [40] australia-southeast1-a
 [41] southamerica-east1-b
 [42] southamerica-east1-c
 [43] southamerica-east1-a
 [44] europe-north1-a
 [45] europe-north1-b
 [46] europe-north1-c
 [47] northamerica-northeast1-a
 [48] northamerica-northeast1-b
 [49] northamerica-northeast1-c
 [50] us-west2-a
Did not print [3] options.
Too many options [53]. Enter "list" at prompt to print choices fully.
Please enter numeric choice or text value (must exactly match list 
item):  {タイムゾーン選択}
Please enter a value between 1 and 53, or a value present in the list:  26

Your project default Compute Engine zone has been set to [{作成したタイムゾーン}].
You can change it by running [gcloud config set compute/zone NAME].

Your project default Compute Engine region has been set to [{作成したタイムゾーン}].
You can change it by running [gcloud config set compute/region NAME].

Created a default .boto configuration file at [{保存先}]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use {作成済みアカウント} by default
* Commands will reference project `{作成済みProject}` by default
* Compute Engine commands will use region `{作成したタイムゾーン}` by default
* Compute Engine commands will use zone `{作成したタイムゾーン}` by default

Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic -h` to learn about advanced features of the SDK like arg files and output formatting
```

認証が完了すると下記コマンドにて認証情報が確認できる

```
$ gcloud auth list
    Credentialed Accounts
ACTIVE  ACCOUNT
*        {作成済みアカウント}

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
```

##  SSH接続

初回のみ下記コマンドで鍵を生成する

```
$ ssh-keygen -t rsa -C "{作成済みアカウント}"
Generating public/private rsa key pair.
Enter file in which to save the key ({HOME}.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in {HOME}.ssh/id_rsa.
Your public key has been saved in {HOME}.ssh/id_rsa.pub
```

GCEのVMインスタンス詳細画面→SSHキー→項目の追加を選択し、
先ほど生成したpublickeyの内容を設定する

<img width="490" alt="スクリーンショット 2018-10-07 17.37.35.png" src="https://qiita-image-store.s3.amazonaws.com/0/264627/12cfd974-7083-5d21-abdd-71c9e2300843.png">

上記の手順が全て完了したら、下記コマンドからSSHログインを行う

```
$ ssh -i ~/.ssh/id_rsa {作成済みアカウント}@{作成済みインスタンスの外部IP}
```

## 感想

インスタンス作成〜SSHログインまで10分少々で出来ちゃいます:joy:
無料期間もあるので是非利用してみてください！