LoginSignup
0
0

CloudformationでS3バケット作成

Posted at

S3バケットを作成するCloudformationテンプレートを作成しました。

コード

コンソール上から、バケットを作成するときに設定する値を定義しています。

AWSTemplateFormatVersion: "2010-09-09"

Description: S3 Stack

Parameters:
  BucketName:
    Type: String

Resources:
  S3:
    Type: "AWS::S3::Bucket"
    Properties:
      BucketName: !Ref BucketName
      # オブジェクト所有者は、全てのオブジェクトは「バケットを所有しているアカウント」
      OwnershipControls:
        Rules:
          - ObjectOwnership: BucketOwnerEnforced
      # 暗号化は、デフォルトのAmazon S3 マネージドキーを使用したサーバー側の暗号化 (SSE-S3)
      # BucketEncryption:
        # BucketKeyEnabled: true
      BucketEncryption:
        ServerSideEncryptionConfiguration:
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: AES256
      # パブリックアクセス無効
      PublicAccessBlockConfiguration:
        BlockPublicAcls: true
        BlockPublicPolicy: true
        IgnorePublicAcls: true
        RestrictPublicBuckets: true
      # バージョニング無効
      VersioningConfiguration:
        Status: Suspended
    DeletionPolicy: Delete

  # S3BucketPolicy:

Outputs:
  S3:
    Value: !GetAtt S3.DomainName
    Export: 
      Name: S3

実行

aws cloudformation create-stack --stack-name {stack_name} --template-body file://cfn_s3.yml --parameters ParameterKey=BucketName,ParameterValue={backet_name}

ツマリポイント

各項目で、''などで囲う必要がある部分とそうでない部分があり、少しハマりました。
たとえば、AES256は囲わなくて良いが、"AWS::S3::Bucket"は囲う必要がある。
このあたりが慣れていなく、ドキュメントを見ながら進めました。
(ちなみに、Cloudformationテンプレートはjsonでも書けるので、jsonでは囲われていて、ymlでは囲われていないみたいなこともあって、ちょっと罠でした。)

BucketEncryption:
  ServerSideEncryptionConfiguration:
    - ServerSideEncryptionByDefault:
        SSEAlgorithm: AES256

また、cloudformationに文法チェックのコマンドもあるので、これを利用するとどこが問題か確認できるので便利でした。

aws cloudformation validate-template --template-body file://cfn_s3.ym

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