How do you install SSE-S3 or SSE-KMS encryption on S3 buckets using a cloud-based development template?

I am trying to use the CloudFormation template to deploy an S3 bucket in AWS. One of the requirements for this project is that the bucket will be encrypted in place. I am trying to find a way to install this using the CloudFormation template (I have read all the documentation I can get for SSE-S3, KMS, CFT and S3s ...). But all the signs indicate that they are only available through the console.

I'm worried, I just missed something obvious, and I wondered if anyone knew how I could use the CloudFormation template (or at least something automatic) to set the default S3 Bucket encryption to SSE- S3 or SSE-KMS

+17
source share
3 answers

AWS added this feature on January 24, 2018 :

Use the BucketEncryption property to specify default encryption for the basket using server-side encryption with keys managed by Amazon S3, SSE-S3, or AWS KMS-managed keys (SSE-KMS).

Json

{
  "Resources": {
    "MyBucket": {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "BucketEncryption": {
          "ServerSideEncryptionConfiguration": [
            {
              "ServerSideEncryptionByDefault": {
                "SSEAlgorithm": "AES256"
              }
            }
          ]
        }
      }
    }
  }
}

Yaml

Resources:
  MyBucket:
    Type: "AWS::S3::Bucket"
    Properties: 
      BucketEncryption: 
        ServerSideEncryptionConfiguration: 
        - ServerSideEncryptionByDefault:
            SSEAlgorithm: AES256

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

+22
source

You can also use the option ForceEncryption:

AWSTemplateFormatVersion: '2010-09-09'
Description: Amazon S3 Bucket with 

Resources:
  CodeFlexS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      AccessControl: Private
      BucketName: !Join ["-", ["codeflex-example", Ref: "AWS::Region"]]

  ForceEncryption:
    Type: AWS::S3::BucketPolicy
    Properties:
      Bucket: !Ref CodeFlexS3Bucket
      PolicyDocument:
        Version: "2008-10-17"
        Statement:
          - Sid: DenyUnEncryptedObjectUploads
            Effect: Deny
            Principal: "*"
            Action:
              - s3:PutObject
            Resource:
              - !Join ["", ["arn:aws:s3:::", !Ref CodeFlexS3Bucket, "/*"]]
            Condition:
              StringNotEquals:
                "s3:x-amz-server-side-encryption":
                  - "aws:kms"
    DependsOn: CodeFlexS3Bucket

: S3 Bucket KMS CloudFormation

+1

If you have a specific KMS key, use the following

  ConfigBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: "mytestbucketwithkmsencryptionkey"
      AccessControl: PublicRead
      BucketEncryption: 
        ServerSideEncryptionConfiguration: 
        - ServerSideEncryptionByDefault:
            SSEAlgorithm: aws:kms
            KMSMasterKeyID: "YOUR KMS KEY ARN"     
0
source

All Articles