【AWS】CloudFormation ~ S3 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2021/10/26/224812
https://dk521123.hatenablog.com/entry/2021/12/01/170326

の続き。

仕事で、CloudFormation を触ることになった。
今回は、S3バケットについて、ちょくちょくまとめていく。

目次

【1】S3バケット
 1)BucketName
 2)PublicAccessBlockConfiguration
 3)BucketEncryption
 4)LoggingConfiguration
 5)NotificationConfiguration
【2】バケットポリシー
 1)Effect
 2)Resource
 3)Principal
 4)Action
【3】サンプル
 例1:S3バケット作成
 例2:S3バケット+ポリシー作成

【1】S3バケット

* 「Type: AWS::S3::Bucket」を使う

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html

1)BucketName

* バケット名を指定(そのまんま)

2)PublicAccessBlockConfiguration

* パブリックアクセスブロックの設定

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-publicaccessblockconfiguration.html

* 具体的には、以下の4パラメータをON/OFFする
 + BlockPublicAcls
 + BlockPublicPolicy
 + IgnorePublicAcls
 + RestrictPublicBuckets
 => 各パラメータの仕様は、以下の公式サイトを参照。

https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/userguide/access-control-block-public-access.html

3)BucketEncryption

* バケットの暗号化

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-bucketencryption.html
イメージ

# SSE-S3 (SSE: Server Side Encryption)
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256

# SSE-KMS
      BucketEncryption:
        ServerSideEncryptionConfiguration:
        - ServerSideEncryptionByDefault:
            SSEAlgorithm: aws:kms

4)LoggingConfiguration

* S3アクセスログを設定

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-loggingconfig.html
例:S3バケット名「your-s3-bucket-name」を指定した場合

      LoggingConfiguration:
        DestinationBucketName: 'your-s3-bucket-name'
        LogFilePrefix: !Sub 's3/${AWS::StackName}'

5)NotificationConfiguration

* S3イベントによる通知設定

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig.html

例:S3イベント「ObjectCreated」が発生したらSNSトピックで通知

      NotificationConfiguration:
        TopicConfigurations:
          - Topic: 'arn:aws:sns:us-east-1:123456789012:TestTopic'
            Event: 's3:ObjectCreated:*'

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket-notificationconfig-topicconfig.html

【2】バケットポリシー

* 「Type: AWS::S3::BucketPolicy」を使う

https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-s3-policy.html

* 基本的に、S3バケットポリシーに従って記述すればよさそう

https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/userguide/access-policy-language-overview.html

1)Effect

* Allow (許可) or Deny (拒否) を指定

2)Resource

* ARN 形式(arn:aws:s3:::bucket_name/key_name) で指定
 => e.g. Resource: arn:aws:s3:::bucket-name/*

3)Principal

* アクセス許可/拒否するユーザー、アカウントなどを指定

cf. Principal (プリンシパル) = 支配者, 主役

4)Action

* 許可/拒否するユーザアクション
 => e.g. s3:PutObject, s3:DeleteObject

【3】サンプル

例1:S3バケット作成

Resources:
  HelloBucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: PublicRead
      WebsiteConfiguration:
        IndexDocument: index.html
        ErrorDocument: error.html

例2:S3バケット+ポリシー作成

AWSTemplateFormatVersion: '2010-09-09'
Description: "This is a sample CloudFormation for s3"

Parameters:
  YourBucketName:
    Type: String
    Default: your-s3-bucket
Resources:
  # [1] s3 bucket 作成
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref YourBucketName
      PublicAccessBlockConfiguration:
        BlockPublicAcls: True
        BlockPublicPolicy: True
        IgnorePublicAcls: True
        RestrictPublicBuckets: True
  # [2] s3 bucket policy
  MyBucketPolicy:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref MyBucket
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Sid: ReadOnly
            Action:
              - 's3:GetObject'
            Effect: Allow
            Resource: !Sub arn:aws:s3:::${YourBucketName}/*
            Principal: 'arn:aws:iam::987654321098:user/userA'
Outputs:
  Value: !Ref MyBucket
  Description: Created S3 bucket name 

参考文献

https://dev.classmethod.jp/articles/cloudformation-s3bucket-type/

AWS公式
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/quickref-s3.html

関連記事

CloudFormation ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2021/10/26/224812
CloudFormation ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2021/12/01/170326
CloudFormation ~ 組み込み関数 ~
https://dk521123.hatenablog.com/entry/2021/12/04/202519
CloudFormation ~ 疑似パラメータ ~
https://dk521123.hatenablog.com/entry/2021/12/05/134313
CloudFormation ~ 条件分岐 ~
https://dk521123.hatenablog.com/entry/2022/07/02/214543
CloudFormation ~ DeletionPolicy 属性 ~
https://dk521123.hatenablog.com/entry/2021/12/27/211328
CloudFormation ~ KMS ~
https://dk521123.hatenablog.com/entry/2022/05/26/112627
CloudFormation ~ IAM ~
https://dk521123.hatenablog.com/entry/2022/05/27/100820
CloudFormation で Github/CodePipeline/CodeBuild を構築する
https://dk521123.hatenablog.com/entry/2021/12/26/155956
CloudFormation でのトラブル
https://dk521123.hatenablog.com/entry/2022/05/30/191507
AWS認定 ~ アソシエイト/ソリューションアーキテクト ~
https://dk521123.hatenablog.com/entry/2022/03/01/000000