[JAWS-UG CLI] CloudFront:#1 ディストリビューションの作成

cloudfrontの更新には15分くらいかかります...

CLIは、プレビュー版のため別途設定が必要です。


前提条件
========

CloudFrontへの権限
------------------

CloudFrontに対してフル権限があること。


AWS CLIのバージョン
-------------------

以下のバージョンで動作確認済

* AWS CLI 1.10.11

```bash:コマンド:
aws --version
```

```bash:結果(例):
aws-cli/1.10.11 Python/2.7.5 Darwin/13.4.0 botocore/1.4.2
```


0. 準備
=======


0.1. リージョンの決定
---------------------


0.2. 変数の確認
---------------

プロファイルが想定のものになっていることを確認します。

```bash:コマンド:
aws configure list
```

```bash:結果(例)

            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile         s3Full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                         ap-northeast-1  env    AWS_DEFAULT_REGION
```

AssumeRoleを利用している場合はprofileが '<not set>'と表示されます。 そ
れ以外のときにprofileが '<not set>' と表示される場合は、以下を実行して
ください。

```bash:変数の設定:
export AWS_DEFAULT_PROFILE=<IAMユーザ名>
```


1. 事前作業
===========

オリジンの指定
--------------

オリジンサーバとなるS3バケットを指定します。

```bash:変数の設定:
CFRONT_ORIGIN_NAME="${S3_BUCKET_ENDPOINT}" \
  && echo ${CFRONT_ORIGIN_NAME}
```



配信用ディストリビューション名の指定
------------------------------------

```bash:変数の設定:
CFRONT_DIST_NAME="handson-$(date +%Y%m%d)" \
        && echo ${CFRONT_DIST_NAME}
```

同名のディストリビューションが存在しないことを確認します。

```bash:コマンド:
aws cloudfront list-distributions \
        --query "DistributionList.Items[].Origins.Items[?contains(DomainName, \`${CFRONT_ORIGIN_NAME}\`)]"
```

```bash:結果:
(戻り値なし)
```




ルートオブジェクトの指定
------------------------

```bash:変数の設定:
CFRONT_ROOT_OBJECT="index.html"
```


2. ディストリビューションの作成
===============================

```bash:変数の確認:
cat << ETX

        CFRONT_ORIGIN_NAME: ${CFRONT_ORIGIN_NAME}
        CFRONT_ROOT_OBJECT: ${CFRONT_ROOT_OBJECT}

ETX
```

```bash:コマンド:
aws cloudfront create-distribution \
        --origin-domain-name ${CFRONT_ORIGIN_NAME} \
        --default-root-object ${CFRONT_ROOT_OBJECT}
```

```bash:結果(例):
```



3. 事後確認
===========

```bash:コマンド:
aws cloudfront list-distributions 
```

```json:結果(例):

      {
        "DistributionList": {
          "Items": [
              {
                  "Status": "InProgress",
                  "CacheBehaviors": {
                      "Quantity": 0
                  },
                  "WebACLId": "",
                  "Origins": {
                      "Items": [
                          {
                              "S3OriginConfig": {
                                  "OriginAccessIdentity": ""
                              },
                              "OriginPath": "",
                              "CustomHeaders": {
                                  "Items": [],
                                  "Quantity": 0
                              },
                              "Id": "S3-handson-20160314",
                              "DomainName": "handson-20160314.s3.amazonaws.com"
                          }
                      ],
                      "Quantity": 1
                  },
                  "DomainName": "xxxxxxxxxxxxxx.cloudfront.net",
                  "PriceClass": "PriceClass_200",
                  "Enabled": true,
                  "DefaultCacheBehavior": {
                      "TrustedSigners": {
                          "Enabled": false,
                          "Quantity": 0
                      },
                      "TargetOriginId": "S3-handson-20160314",
                      "ViewerProtocolPolicy": "allow-all",
                      "ForwardedValues": {
                          "Headers": {
                              "Quantity": 0
                          },
                          "Cookies": {
                              "Forward": "none"
                          },
                          "QueryString": false
                      },
                      "MaxTTL": 31536000,
                      "SmoothStreaming": false,
                      "DefaultTTL": 86400,
                      "AllowedMethods": {
                          "Items": [
                              "HEAD",
                              "GET"
                          ],
                          "CachedMethods": {
                              "Items": [
                                  "HEAD",
                                  "GET"
                              ],
                              "Quantity": 2
                          },
                          "Quantity": 2
                      },
                      "MinTTL": 0,
                      "Compress": false
                  },
                  "Comment": "sample distribution",
                  "ViewerCertificate": {
                      "CloudFrontDefaultCertificate": true,
                      "MinimumProtocolVersion": "SSLv3",
                      "CertificateSource": "cloudfront"
                  },
                  "CustomErrorResponses": {
                      "Quantity": 0
                  },
                  "LastModifiedTime": "2016-03-14T07:51:02.752Z",
                  "Id": "EUTXXXXXXXXXX",
                  "Restrictions": {
                      "GeoRestriction": {
                          "RestrictionType": "none",
                          "Quantity": 0
                      }
                  },
                  "Aliases": {
                      "Quantity": 0
                  }
              }
          ]
        }
      }
```

```bash:コマンド:
CFRONT_DIST_ID=$( \
        aws cloudfront list-distributions \
          --query "DistributionList.Items[?contains(Origins.Items[].DomainName, \`$CFRONT_ORIGIN_NAME\`)].Id" \
          --output text \
) \
        && echo ${CFRONT_DIST_ID}
```

```bash:コマンド:
aws cloudfront get-distribution \
        --id ${CFRONT_DIST_ID}
```

```json:結果(例):

      {
        "Distribution": {
          "Status": "Deployed",
          "DomainName": "d2yhs6oup1drdc.cloudfront.net",
          "InProgressInvalidationBatches": 0,
          "DistributionConfig": {
              "Comment": "sample distribution",
              "CacheBehaviors": {
                  "Quantity": 0
              },
              "Logging": {
                  "Bucket": "accesslog-opelab-handson-2016031016.s3.amazonaws.com",
                  "Prefix": "cfLogs/",
                  "Enabled": true,
                  "IncludeCookies": false
              },
              "WebACLId": "",
              "Origins": {
                  "Items": [
                      {
                          "S3OriginConfig": {
                              "OriginAccessIdentity": ""
                          },
                          "OriginPath": "",
                          "CustomHeaders": {
                              "Items": [],
                              "Quantity": 0
                          },
                          "Id": "S3-opelab-handson-2016031016",
                          "DomainName": "opelab-handson-2016031016.s3.amazonaws.com"
                      }
                  ],
                  "Quantity": 1
              },
              "DefaultRootObject": "index.html",
              "PriceClass": "PriceClass_200",
              "Enabled": true,
              "DefaultCacheBehavior": {
                  "TrustedSigners": {
                      "Enabled": false,
                      "Quantity": 0
                  },
                  "TargetOriginId": "S3-opelab-handson-2016031016",
                  "ViewerProtocolPolicy": "allow-all",
                  "ForwardedValues": {
                      "Headers": {
                          "Quantity": 0
                      },
                      "Cookies": {
                          "Forward": "none"
                      },
                      "QueryString": false
                  },
                  "MaxTTL": 31536000,
                  "SmoothStreaming": false,
                  "DefaultTTL": 86400,
                  "AllowedMethods": {
                      "Items": [
                          "HEAD",
                          "GET"
                      ],
                      "CachedMethods": {
                          "Items": [
                              "HEAD",
                              "GET"
                          ],
                          "Quantity": 2
                      },
                      "Quantity": 2
                  },
                  "MinTTL": 0,
                  "Compress": false
              },
              "CallerReference": "1457596264125",
              "ViewerCertificate": {
                  "CloudFrontDefaultCertificate": true,
                  "MinimumProtocolVersion": "SSLv3",
                  "CertificateSource": "cloudfront"
              },
              "CustomErrorResponses": {
                  "Quantity": 0
              },
              "Restrictions": {
                  "GeoRestriction": {
                      "RestrictionType": "none",
                      "Quantity": 0
                  }
              },
              "Aliases": {
                  "Quantity": 0
              }
          },
          "ActiveTrustedSigners": {
              "Enabled": false,
              "Quantity": 0
          },
          "LastModifiedTime": "2016-03-10T09:52:52.418Z",
          "Id": "EUTX0MFN0TQHP"
        },
        "ETag": "E1QO82W6C0PLFV"
      }
```

```bash:コマンド:
aws cloudfront get-distribution-config \
        --id ${CFRONT_DIST_ID}
```

```json:結果(例):
      {
        "ETag": "E1QO82W6C0PLFV",
        "DistributionConfig": {
          "Comment": "sample distribution",
          "CacheBehaviors": {
              "Quantity": 0
          },
          "Logging": {
              "Bucket": "accesslog-opelab-handson-2016031016.s3.amazonaws.com",
              "Prefix": "cfLogs/",
              "Enabled": true,
              "IncludeCookies": false
          },
          "WebACLId": "",
          "Origins": {
              "Items": [
                  {
                      "S3OriginConfig": {
                          "OriginAccessIdentity": ""
                      },
                      "OriginPath": "",
                      "CustomHeaders": {
                          "Items": [],
                          "Quantity": 0
                      },
                      "Id": "S3-opelab-handson-2016031016",
                      "DomainName": "opelab-handson-2016031016.s3.amazonaws.com"
                  }
              ],
              "Quantity": 1
          },
          "DefaultRootObject": "index.html",
          "PriceClass": "PriceClass_200",
          "Enabled": true,
          "DefaultCacheBehavior": {
              "TrustedSigners": {
                  "Enabled": false,
                  "Quantity": 0
              },
              "TargetOriginId": "S3-opelab-handson-2016031016",
              "ViewerProtocolPolicy": "allow-all",
              "ForwardedValues": {
                  "Headers": {
                      "Quantity": 0
                  },
                  "Cookies": {
                      "Forward": "none"
                  },
                  "QueryString": false
              },
              "MaxTTL": 31536000,
              "SmoothStreaming": false,
              "DefaultTTL": 86400,
              "AllowedMethods": {
                  "Items": [
                      "HEAD",
                      "GET"
                  ],
                  "CachedMethods": {
                      "Items": [
                          "HEAD",
                          "GET"
                      ],
                      "Quantity": 2
                  },
                  "Quantity": 2
              },
              "MinTTL": 0,
              "Compress": false
          },
          "CallerReference": "1457596264125",
          "ViewerCertificate": {
              "CloudFrontDefaultCertificate": true,
              "MinimumProtocolVersion": "SSLv3",
              "CertificateSource": "cloudfront"
          },
          "CustomErrorResponses": {
              "Quantity": 0
          },
          "Restrictions": {
              "GeoRestriction": {
                  "RestrictionType": "none",
                  "Quantity": 0
              }
          },
          "Aliases": {
              "Quantity": 0
          }
        }
      }
```


```bash:コマンド:
CFRONT_DIST_STATUS=$( \
   aws cloudfront list-distributions \
     --query "DistributionList.Items[?contains(Origins.Items[].DomainName, \`$CFRONT_ORIGIN_NAME\`)].Status" \
     --output text \
) \
  && echo ${CFRONT_DIST_STATUS}

```


```bash:コマンド:
CFRONT_DIST_URL=$( \
   aws cloudfront list-distributions \
     --query "DistributionList.Items[?contains(Origins.Items[].DomainName, \`$CFRONT_ORIGIN_NAME\`)].DomainName" \
     --output text \
) \
  && echo ${CFRONT_DIST_URL}

```


4. ディストリビューションの削除
===============================


4.1. 事前確認
-------------

```bash:コマンド:
aws cloudfront get-distribution-config \
     --id ${CFRONT_DIST_ID} \
     > distconfig-disabled.json
```

- "Enabled": trueを"Enabled": falseにする。
- ETagのキーとバリューを削除する。
- 一番外側のタグ("DistributionConfig":{})を削除する。


```bash:コマンド:
CFRONT_ETAG=$( \
     aws cloudfront get-distribution-config \
       --id ${CFRONT_DIST_ID} \
       --query 'ETag' \
       --output text \
) \
     && echo ${CFRONT_ETAG}
```

```bash:コマンド:
aws cloudfront update-distribution \
     --id ${CFRONT_DIST_ID} \
     --distribution-config file://distconfig-disabled.json \
     --if-match ${CFRONT_ETAG}
```



4.2. 削除
---------


```bash:コマンド:
CFRONT_ETAG=$( \
     aws cloudfront get-distribution-config \
       --id ${CFRONT_DIST_ID} \
       --query 'ETag' \
       --output text \
) \
     && echo ${CFRONT_ETAG}
```

```bash:コマンド:
aws cloudfront delete-distribution \
     --id ${CFRONT_DIST_ID} \
     --if-match ${CFRONT_ETAG}
```

```json:結果(例):
```



4.3. 事後確認
-------------


```bash:コマンド:
aws cloudfront get-distribution \
        --id ${CFRONT_DIST_ID}
```


完了
====