これで今年は迷わない! Cloud9でCodeCommitの利用環境構築

![スクリーンショット 2019-08-04 12.43.13.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/62413/0dc78de9-b394-b2fd-2aeb-721e9aee285c.png)

で

![スクリーンショット 2019-08-04 12.43.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/62413/1f15d16c-bf8a-d0a4-acac-bd755dc00f00.png)

CodeCommit


## 目標
 - Cloud9にてSSHを使ってCodeCommitにアクセス
 - リポジトリの作成 / Clone / Push の実行まで

## 手順

### 1. IAMユーザを準備.

1. 適当なIAMユーザを作成します。
2. **aws configure** 等を利用して**aws-cli**の設定をしておきます

### 2. IAMユーザに **AWSCodeCommitFullAccess** ポリーシをアタッチ
**AWSCodeCommitFullAccess** ポリシーがないとCodeCommitにアクセスできません。


### 3. Cloud9にて、**ssh-keygen** を実行

**~/.ssh** 直下に鍵ファイルが作成されます。

```bash

$ ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): <空白>
Enter passphrase (empty for no passphrase): <空白>
Enter same passphrase again: <空白>

Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zQlTv/U3V3sUqNMMxxxxxxxxxxxxxxxxqFCfM ubuntu@ip-172-31-25-169
The key's randomart image is:
+---[RSA 2048]----+
|        ..*+=+B+o|
|       . B.B+*oo.|
|        = *o%E+ +|
|         B B.*.+=|
|        S + o  ==|
|                =|
|                 |
|                 |
|                 |
+----[SHA256]-----+

 $ cd ~/.ssh
nohara:~/.ssh $ ls
id_rsa  id_rsa.pub  known_hosts
```

### 4. aws-cliを使って, **公開鍵をアップロード**

GUIだとコピペに失敗することがあるので、awscli経由でちゃちゃっとやります

```bash

$ aws iam upload-ssh-public-key ¥
                 --user-name terraformer ¥
                 --ssh-public-key-body file://id_rsa.pub

{
    "SSHPublicKey": {
        "UserName": "terraformer",
        "SSHPublicKeyId": "APKAxxxxxxxxxx",
        "Fingerprint": "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx",
        "SSHPublicKeyBody": "ssh-rsa AAAAB3NzaC1yc2ExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxHkxVD+ifmz ubuntu@ip-172-31-25-169\n",
        "Status": "Active",
        "UploadDate": "2019-08-04T03:14:27Z"
    }
}

```


### 5. SSHの接続テスト


```bash

$ ssh APKAxxxxxxxxxx@git-codecommit.ap-northeast-1.amazonaws.com

You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. Interactive shells are not supported.Connection to git-codecommit.ap-northeast-1.amazonaws.com closed by remote host.
Connection to git-codecommit.ap-northeast-1.amazonaws.com closed.

```

### 6. ssh/configにHostを登録

```bash
$ vim ~/.ssh/config

Host git-codecommit.*.amazonaws.com
  User APK*********HA
  IdentityFile ~/.ssh/id_rsa

```

### 7. configにパーミッションを設定 (重要)

```bash
$ chmod 600 ~/.ssh/config
```

設定は以上です。

残りは、gitの操作になります。参考まで。


### 既存のリモートリポジトリにpushする場合


```bash

# リモートリポジトリをclone
$ git clone ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/your-remote-repository

# ..ローカルで変更を add & commit 

# リモートリポジトリにプッシュ
$ git push origin master ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/your-remote-repository

```

以上

### 新規にリポジトリを作成する場合

**aws-cli** でリモートリポジトリを新規作成

```bash
$ aws codecommit create-repository --repository-name new-your-repository

{
    "repositoryMetadata": {
        "accountId": "869853594551",
        "repositoryId": "16c9c7a2-2928-4a02-8e0e-377165a2509a",
        "repositoryName": "new-your-repository",
        "lastModifiedDate": 1564889728.186,
        "creationDate": 1564889728.186,
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/new-your-repository",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/new-your-repository",
        "Arn": "arn:aws:codecommit:ap-northeast-1:1234567890:MyDemoRepo"
    }
}
```

上から**cloneUrlSsh**を**コピ-**する

```bash

$ git clone ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/new-your-repository
Cloning into 'MyDemoRepo2'...
warning: You appear to have cloned an empty repository.

$ cd MyDemoRepo2/

$ git remote -v
origin  ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyDemoRepo2 (fetch)
origin  ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyDemoRepo2 (push)
```

以上

## Tips: Gitにリモートリポジトリ関連コマンド

```bash

$ git remote add origin ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/your-remote-repository

$ git remote rm origin

$ git remote -v
```

## 参考

 - [AWS Cloud9 から CodeCommit を使うための設定 - Qiita](https://qiita.com/acecrc/items/8e17b08c4fb0b4d9c9f9)