LoginSignup
3
3

More than 5 years have passed since last update.

[JAWS-UG CLI] S3:#10 CDP案 Log Storage

Last updated at Posted at 2016-03-13

CDPを意識したハンズオンを行います。

ハンズオンの対象となるCDP(案)

前提条件

S3への権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.10.11
コマンド
aws --version
結果(例)

      aws-cli/1.10.4 Python/2.7.5 Darwin/13.4.0 botocore/1.3.26

0. 準備

0.1. リージョンの決定

作成するS3バケットのリージョンを決めます。

(カレントユーザが利用するカレントリージョンも切り変わります。)

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-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. 事前作業

コンテンツ用バケット名の決定

今回のハンズオンでは、バケット名に組織名とプロジェクト名を埋め込みます。

変数の設定
ORG_NAME=<組織名>
PRJ_NAME='handson'

注釈: (英小文字と数字が使えます。S3上でユニークである必要があります。英大 文字は使用できません。)

変数の設定
S3_BUCKET_NAME="${ORG_NAME}-${PRJ_NAME}-$(date +%Y%m%d%H)" \
        && echo ${S3_BUCKET_NAME}

同名バケットが存在しないことを確認します。

コマンド
aws s3 ls s3://${S3_BUCKET_NAME}/
結果(例)
      A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist
変数の設定
S3_BUCKET_ORIGIN="${S3_BUCKET_NAME}"
S3_BUCKET_NAME="accesslog-${S3_BUCKET_ORIGIN}"
S3_BUCKET_LOG="${S3_BUCKET_NAME}"

2. バケットの作成

変数の確認
cat << ETX

        S3_BUCKET_NAME:     ${S3_BUCKET_NAME}
        AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}

ETX
コマンド
aws s3api create-bucket \
        --bucket ${S3_BUCKET_NAME} \
        --create-bucket-configuration "LocationConstraint=${AWS_DEFAULT_REGION}"
結果(例)

      {
        "Location": "http://accesslog-corp-handson-20160309.s3.amazonaws.com/"
      }
コマンド
aws s3api get-bucket-location \
        --bucket ${S3_BUCKET_NAME}
結果(例)

      {
        "LocationConstraint": "ap-northeast-1"
      }

3. ACL設定

3.1. ACLの確認

バケットを作成したら、ACLを確認してみましょう。

コマンド
aws s3api get-bucket-acl \
        --bucket ${S3_BUCKET_NAME}
結果(例)

      {
        "Owner": {
          "DisplayName": "corp",
          "ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        },
        "Grants": [
          {
              "Grantee": {
                  "Type": "CanonicalUser",
                  "DisplayName": "corp",
                  "ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
              },
              "Permission": "FULL_CONTROL"
          }
        ]
      }

管理者にフル制御の権限が割り当てられていますが、それ以外のリソースから
はアクセスできない状態になっています。

3.2. ACLの変更

S3のログが書き込めるようにACLの設定を変更します。

変数の確認
cat << ETX

        S3_BUCKET_NAME: ${S3_BUCKET_NAME}

ETX
コマンド
aws s3api put-bucket-acl \
        --bucket ${S3_BUCKET_NAME} \
        --grant-write 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"' \
        --grant-read-acp 'URI="http://acs.amazonaws.com/groups/s3/LogDelivery"'
結果(例)

      (戻り値なし)

注釈: デフォルトで存在する自分自身のFULL_CONTROLを維持したい場合は、下記の パラメータが必要になります。($(MAILADDR_AWS)はログイン用メールアドレスです。)

パラメータ
     --grant-full-control "EmailAddress=${MAILADDR_AWS}"

3.3. ACLの確認

変更をしたら、ACLの内容を確認します。

変数の確認
cat << ETX

        S3_BUCKET_NAME: ${S3_BUCKET_NAME}

ETX
コマンド
aws s3api get-bucket-acl \
        --bucket ${S3_BUCKET_NAME}
結果(例)

      {
        "Owner": {
          "DisplayName": "corp",
          "ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        },
        "Grants": [
          {
              "Grantee": {
                  "DisplayName": "corp",
                  "ID": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
              },
              "Permission": "FULL_CONTROL"
          },
          {
              "Grantee": {
                  "URI": "http://acs.amazonaws.com/groups/s3/LogDelivery"
              },
              "Permission": "WRITE"
          },
          {
              "Grantee": {
                  "URI": "http://acs.amazonaws.com/groups/s3/LogDelivery"
              },
              "Permission": "READ_ACP"
          }
        ]
      }

S3のログについて、バケットへの書き込み(WRITE)およびACL情報の読み取り(READ_ACP)が許可されていることがわかります。

完了

3
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
3
3