前提
・AWS CLIがインストールされて、初期設定が完了できていること
・実行OS: Windows
背景
aws s3コンソール上でポチポチs3バケットをすぐ作成できますが、操作ミスを防ぐためにコード化したいと思って、CloudFormationのテンプレートを作成してみました。
まず、S3 サーバーアクセスログを作成してみたいと思います。
なぜなら、ログの保存先として存在しないと、ほかのバケット作成するときに指定できないです。
S3 サーバーアクセスログとは?
(AWS公式より抜粋)
サーバーアクセスのログには、Amazon S3 バケットに対するリクエストの詳細が記録されます。サーバーアクセスのログは、多くのアプリケーションに役立ちます。たとえば、アクセスのログ情報は、セキュリティやアクセスの監査に役立ちます。また、顧客基盤について知り、Amazon S3 の請求を理解することにも役立ちます。
デフォルトでは、Amazon S3 によってサーバーアクセスログは収集されません。ログ記録を有効にすると、Amazon S3 は、ソースバケットのアクセスログを選択されたターゲットバケットに配信します。ターゲットバケットは、ソースバケットと同じ AWS リージョン内になければならず、デフォルトの保存期間設定を持ってはいけません。
S3 サーバーアクセスログの作成コード
テンプレート
S3-ServerAccessLog-Template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'S3Bucket'
Parameters:
Environment:
Type: String
Description: "Environment"
S3BucketName:
Type: String
Description: Type of this BacketName.
Resources:
ServerAccessLog:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref S3BucketName
#ログ配信オブジェクトの書き込みとバケットのアクセス権限の読み取りを許可します。
#https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/dev/acl-overview.html#canned-acl
AccessControl: LogDeliveryWrite
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
LifecycleConfiguration:
Rules:
- Id: cpi-dev-tyo-s3-kby-serveraccesslog-s3lr-test01
Status: Enabled
Transitions:
- TransitionInDays: '180'
StorageClass: STANDARD_IA
NoncurrentVersionTransitions:
- TransitionInDays: '180'
StorageClass: STANDARD_IA
# 365日経過したら削除(現行バージョン)
ExpirationInDays: 365
# バージョンニングされているデータは365日経過したら削除(以前のバージョン)
NoncurrentVersionExpirationInDays: 365
# 不完全なマルチパートアップロードをクリーンアップする
AbortIncompleteMultipartUpload:
DaysAfterInitiation: 7
# バージョンニングの有効化
VersioningConfiguration:
Status: Enabled
# サーバーアクセスのログ記録(混乱を避けるために、ログ用のバケット自身のログを記録しない)
# LoggingConfiguration:
# DestinationBucketName:
# LogFilePrefix:
# デフォルト暗号化
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
Tags:
- Key: Env
Value: !Ref Environment
Outputs:
ServerAccessLog:
Value: !Ref ServerAccessLog
#Ref: ServerAccessLog
Export:
Name: !Ref S3BucketName
パラメーター(Inputファイル)
cpi-dev-tyo-s3-kby-serveraccesslog.yaml
StackName: 'cpi-dev-tyo-s3-kby-serveraccesslog'
Parameters:
- ParameterKey: 'Environment'
ParameterValue: 'cpi-dev'
- ParameterKey: 'S3BucketName'
ParameterValue: 'cpi-dev-tyo-s3-kby-serveraccesslog'
実行コマンド(Windows Bat)
create_cpi-dev-tyo-s3-kby-serveraccesslog.bat
@echo off
set CFN_PATH=D:\AWS-CPI-DEV\90_Template\s3
set INPUT_PATH=D:\AWS-CPI-DEV\90_Template\InputFiles
set TEMPLATE_NAME=S3-ServerAccessLog-Template.yaml
set INPUT_NAME=cpi-dev-tyo-s3-kby-serveraccesslog.yaml
aws cloudformation create-stack --template-body file://%CFN_PATH%\%TEMPLATE_NAME% --cli-input-yaml file://%INPUT_PATH%\%INPUT_NAME%
実行例:
create_cpi-dev-tyo-s3-kby-serveraccesslog.bat
{
"StackId": "arn:aws:cloudformation:ap-northeast-1:268673644828:stack/cpi-dev-tyo-s3-kby-serveraccesslog/9cf221b0-a5c0-11eb-a12a-0aab69920bd1"
}
注意点
テンプレート「S3-ServerAccessLog-Template.yaml」日本語のコメントが入っています。この場合、下記のようなエラーができ可能性があります。ファイルの文字コードをShift-JISに変更して保存すれば、解決できます。
Error parsing parameter '--template-body': Unable to load paramfile (D:\AWS-CPI-DEV\90_Template\s3\S3-ServerAccessLog-Template.yaml), text contents could not be decoded. If this is a binary file, please use the fileb:// prefix instead of the file:// prefix.
確認
確認1:CloudFormationコンソール上での確認
確認2:s3コンソール上での確認
参考
https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/userguide/enable-server-access-logging.html
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html