2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-03-14

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

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

前提条件

CloudFrontへの権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.10.11
コマンド
aws --version
結果(例)
aws-cli/1.10.11 Python/2.7.5 Darwin/13.4.0 botocore/1.4.2
  1. 準備
    =======

0.1. リージョンの決定

0.2. 変数の確認

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

コマンド
aws configure list
結果(例)

            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が ''と表示されます。 そ
れ以外のときにprofileが '' と表示される場合は、以下を実行して
ください。

変数の設定
export AWS_DEFAULT_PROFILE=<IAMユーザ名>
  1. 事前作業
    ===========

オリジンの指定

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

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

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

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

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

コマンド
aws cloudfront list-distributions \
        --query "DistributionList.Items[].Origins.Items[?contains(DomainName, \`${CFRONT_ORIGIN_NAME}\`)]"
結果
(戻り値なし)

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

変数の設定
CFRONT_ROOT_OBJECT="index.html"
  1. ディストリビューションの作成
    ===============================
変数の確認
cat << ETX

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

ETX
コマンド
aws cloudfront create-distribution \
        --origin-domain-name ${CFRONT_ORIGIN_NAME} \
        --default-root-object ${CFRONT_ROOT_OBJECT}
結果(例)
  1. 事後確認
    ===========
コマンド
aws cloudfront list-distributions 
結果(例)

      {
        "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
                  }
              }
          ]
        }
      }
コマンド
CFRONT_DIST_ID=$( \
        aws cloudfront list-distributions \
          --query "DistributionList.Items[?contains(Origins.Items[].DomainName, \`$CFRONT_ORIGIN_NAME\`)].Id" \
          --output text \
) \
        && echo ${CFRONT_DIST_ID}
コマンド
aws cloudfront get-distribution \
        --id ${CFRONT_DIST_ID}
結果(例)

      {
        "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"
      }
コマンド
aws cloudfront get-distribution-config \
        --id ${CFRONT_DIST_ID}
結果(例)
      {
        "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
          }
        }
      }
コマンド
CFRONT_DIST_STATUS=$( \
   aws cloudfront list-distributions \
     --query "DistributionList.Items[?contains(Origins.Items[].DomainName, \`$CFRONT_ORIGIN_NAME\`)].Status" \
     --output text \
) \
  && echo ${CFRONT_DIST_STATUS}

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

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

4.1. 事前確認

コマンド
aws cloudfront get-distribution-config \
     --id ${CFRONT_DIST_ID} \
     > distconfig-disabled.json
  • "Enabled": trueを"Enabled": falseにする。
  • ETagのキーとバリューを削除する。
  • 一番外側のタグ("DistributionConfig":{})を削除する。
コマンド
CFRONT_ETAG=$( \
     aws cloudfront get-distribution-config \
       --id ${CFRONT_DIST_ID} \
       --query 'ETag' \
       --output text \
) \
     && echo ${CFRONT_ETAG}
コマンド
aws cloudfront update-distribution \
     --id ${CFRONT_DIST_ID} \
     --distribution-config file://distconfig-disabled.json \
     --if-match ${CFRONT_ETAG}

4.2. 削除

コマンド
CFRONT_ETAG=$( \
     aws cloudfront get-distribution-config \
       --id ${CFRONT_DIST_ID} \
       --query 'ETag' \
       --output text \
) \
     && echo ${CFRONT_ETAG}
コマンド
aws cloudfront delete-distribution \
     --id ${CFRONT_DIST_ID} \
     --if-match ${CFRONT_ETAG}
結果(例)

4.3. 事後確認

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

完了

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?