LoginSignup
2
2

More than 5 years have passed since last update.

[JAWS-UG CLI] S3:#8 Trail用S3バケットの作成

Last updated at Posted at 2015-05-11

AWS CLIを利用して、CloudTrail用のS3バケットを作成してみます。

前提条件

S3への権限

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

AWS CLIのバージョン

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

    • AWS CLI 1.7.26
コマンド
aws --version
結果(例)
      aws-cli/1.7.26 Python/2.7.5 Darwin/13.4.0

0. 準備

0.1. リージョンの決定

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

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

コマンド(アイルランドリージョンの場合)
export AWS_DEFAULT_REGION='eu-west-1'

0.2. 変数の確認

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

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       s3Full-prjZ-mbp13iamFull-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                eu-west-1              env    AWS_DEFAULT_REGION

1. 事前作業

1.1. AWS IDの取得

コマンド
AWS_ID=`aws iam get-user --query 'User.Arn' --output text | sed 's/^.*:://' | sed 's/:.*$//'` \
        && echo ${AWS_ID}
結果(例)
      XXXXXXXXXXXX
コマンド(IAMに権限が無い場合)
AWS_ID=`aws iam get-user --query 'User.Arn' --output text  2>&1 | sed 's/^.* User: arn:aws:iam:://' |sed 's/:.*$//' | tr -d '\n'` \
        && echo ${AWS_ID}
結果(例)
      XXXXXXXXXXXX

1.2. S3バケット名の決定

コマンド
S3_BUCKET_NAME="trail-${AWS_ID}-${AWS_DEFAULT_REGION}" \
        && 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

2. 本作業

2.1. S3バケットの作成

コマンド
cat << ETX

        S3_BUCKET_NAME: ${S3_BUCKET_NAME}

ETX
コマンド
aws s3 mb s3://${S3_BUCKET_NAME}
結果(例)
      make_bucket: s3://trail-XXXXXXXXXXXX-eu-west-1/

2.2. バケットポリシーファイルの作成

バケットポリシーファイルを作成します。

コマンド
FILE_S3_BUCKET_POLICY="BucketPolicy-CloudTrail-${AWS_DEFAULT_REGION}.json"
コマンド
cat << ETX

        FILE_S3_BUCKET_POLICY: ${FILE_S3_BUCKET_POLICY}
        S3_BUCKET_NAME:        ${S3_BUCKET_NAME}
        AWS_ID:                ${AWS_ID}

ETX
コマンド
cat << EOF > ${FILE_S3_BUCKET_POLICY}
{
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Sid": "AWSCloudTrailAclCheck20131101",
                  "Effect": "Allow",
                  "Principal": {
                      "AWS": [
                          "arn:aws:iam::903692715234:root",
                          "arn:aws:iam::035351147821:root",
                          "arn:aws:iam::859597730677:root",
                          "arn:aws:iam::814480443879:root",
                          "arn:aws:iam::216624486486:root",
                          "arn:aws:iam::086441151436:root",
                          "arn:aws:iam::388731089494:root",
                          "arn:aws:iam::284668455005:root",
                          "arn:aws:iam::113285607260:root"
                      ]
                  },
                  "Action": "s3:GetBucketAcl",
                  "Resource": "arn:aws:s3:::${S3_BUCKET_NAME}"
              },
              {
                  "Sid": "AWSCloudTrailWrite20131101",
                  "Effect": "Allow",
                  "Principal": {
                      "AWS": [
                          "arn:aws:iam::903692715234:root",
                          "arn:aws:iam::035351147821:root",
                          "arn:aws:iam::859597730677:root",
                          "arn:aws:iam::814480443879:root",
                          "arn:aws:iam::216624486486:root",
                          "arn:aws:iam::086441151436:root",
                          "arn:aws:iam::388731089494:root",
                          "arn:aws:iam::284668455005:root",
                          "arn:aws:iam::113285607260:root"
                      ]
                  },
                  "Action": "s3:PutObject",
                  "Resource": "arn:aws:s3:::${S3_BUCKET_NAME}/AWSLogs/${AWS_ID}/*",
                  "Condition": {
                      "StringEquals": {
                          "s3:x-amz-acl": "bucket-owner-full-control"
                      }
                  }
              }
          ]
}
EOF

JSONファイルを作成したら、フォーマットが壊れてないか必ず確認します。

コマンド
jsonlint -q ${FILE_S3_BUCKET_POLICY}

2.3. バケットポリシーの適用

S3バケットにバケットポリシーを適用します。

コマンド
aws s3api put-bucket-policy \
        --bucket ${S3_BUCKET_NAME} \
        --policy file://${FILE_S3_BUCKET_POLICY}
結果
      (戻り値なし)

2.4. バケットポリシーの確認

コマンド
aws s3api get-bucket-policy --bucket ${S3_BUCKET_NAME} |\
        sed 's/\\//g' |\
        sed 's/"{/{/' |\
        sed 's/}"/}/' |\
        jq .
結果(例)
      {
        "Policy": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Sid": "AWSCloudTrailAclCheck20131101",
              "Effect": "Allow",
              "Principal": {
                "AWS": [
                  "arn:aws:iam::903692715234:root",
                  "arn:aws:iam::035351147821:root",
                  "arn:aws:iam::859597730677:root",
                  "arn:aws:iam::814480443879:root",
                  "arn:aws:iam::216624486486:root",
                  "arn:aws:iam::086441151436:root",
                  "arn:aws:iam::388731089494:root",
                  "arn:aws:iam::284668455005:root",
                  "arn:aws:iam::113285607260:root"
                ]
              },
              "Action": "s3:GetBucketAcl",
              "Resource": "arn:aws:s3:::trail-XXXXXXXXXXXX-eu-west-1"
            },
            {
              "Sid": "AWSCloudTrailWrite20131101",
              "Effect": "Allow",
              "Principal": {
                "AWS": [
                  "arn:aws:iam::903692715234:root",
                  "arn:aws:iam::035351147821:root",
                  "arn:aws:iam::859597730677:root",
                  "arn:aws:iam::814480443879:root",
                  "arn:aws:iam::216624486486:root",
                  "arn:aws:iam::086441151436:root",
                  "arn:aws:iam::388731089494:root",
                  "arn:aws:iam::284668455005:root",
                  "arn:aws:iam::113285607260:root"
                ]
              },
              "Action": "s3:PutObject",
              "Resource": "arn:aws:s3:::trail-XXXXXXXXXXXX-eu-west-1/AWSLogs/XXXXXXXXXXXX/*",
              "Condition": {
                "StringEquals": {
                  "s3:x-amz-acl": "bucket-owner-full-control"
                }
              }
            }
          ]
        }
      }

完了

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