Encryption of data at rest is increasingly required by industry protocols, government regulations, and internal organizational security standards. Encryption helps you protect your stored data against unauthorized access and other security risks.
Amazon S3’s default encryption can be used to automate the encryption of new objects in your bucket, but default encryption does not change the encryption of existing objects in the same bucket. You may have existing objects in your Amazon S3 bucket that must be encrypted, or you may want to change the server-side encryption (SSE) settings you are using. I often get questions from customers on the simplest way to encrypt existing objects in their S3 bucket.
In this post, I cover important things to consider when using the Copy Object API to encrypt existing objects in place. I then provide examples you can use to encrypt existing objects in a bucket to keep your data secure using the AWS Command Line Interface (AWS CLI). I also provide examples you can use to encrypt all S3 objects in a prefix or bucket. Lastly, I discuss common questions around copying and encryption.
PrerequisitesTo run the commands outlined in this post, you need:
First thing’s first, BE CAREFUL! To encrypt an existing object using SSE, you replace the object. To encrypt existing objects in place, you can use the Copy Object or Copy Part API. This copies the objects with the same name and encrypts the object data using server-side encryption. Here are some things to consider before using the Copy Object API:
--metadata
parameter.cp
command does not copy object tags from the source object to the destination object. You can use the aws s3api put-object-tag command to add these to the new object.--storage-class
. This also makes it an easy way to modify the storage class that your objects are using.Outside of the above there are several other things that you should consider before attempting encryption using the AWS CLI:
To get started, you must install and configure the AWS CLI. What follows is a collection of commands you can use to encrypt objects using the AWS CLI:
aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse AES256
aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse aws:kms
--sse-kms-key-id
argument:aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse aws:kms --sse-kms-key-id arn:aws:kms:us-west-2:111122223333:key/3aefc301-b7d2-4601-9298-5a854cf9999d
--dryrun
flag.aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse aws:kms --dryrun
(dryrun) copy: s3://awsexamplebucket/myfile to s3://awsexamplebucket/myfile
aws s3 cp s3://awsexamplebucket/ s3://awsexamplebucket/ --sse aws:kms --recursive
aws s3 cp s3://awsexamplebucket/prefix1/ s3://awsexamplebucket/prefix1/ --sse aws:kms --recursive
aws configure set default.s3.max_concurrent_requests 60
aws configure set default.s3.multipart_threshold 5GB
aws configure set default.s3.multipart_chunksize 5GB
More options and examples for copying and configuration can be found in the AWS CLI documentation.
Common questions around copying and encryptionHere I run down a list of common questions that customers have around copying and encryption.
How long does copying take?I tested from a T2.medium EC2 instance in the same Region as the S3 bucket. I used a total of 10,000 1-GB objects for a total size of 10 TB. I was able to complete encrypting all objects in my test bucket in minutes using the SSE-KMS encryption type.
The amount of time it takes to copy varies, with the variance primarily based on total object counts. For example, a large number of small objects takes longer than a small number of large objects even if the total size is greater.
What about large buckets?For S3 buckets with a large number of objects, in the order of millions or billions of objects, using Amazon S3 inventory or Amazon S3 Batch Operations can be a better option than using the AWS CLI instructions in this post. Check out this blog post to learn more about batch operations.
What about data stored in Amazon S3 Glacier or Amazon S3 Glacier Deep Archive?When objects are moved into Amazon S3 Glacier or Amazon S3 Glacier Deep Archive, they are automatically encrypted at rest. Objects in S3 Glacier or S3 Glacier Deep Archive must first be restored before executing the copy request to encrypt them with SSE-S3.
What about versioned objects?When you overwrite an S3 object, it results in a new object version in the bucket. Running the commands outlined here results in a new encrypted version. However, it does not remove the old unencrypted version of the object. If you want to remove these versions, see the versioning documentation to understand how to use S3 Lifecycle to expire previous versions of objects.
What about tags, ACL, or custom metadata?If an object is greater than your multipart_threshold (5 GB as used in this example), the AWS CLI is unable to copy the existing Tags, ACL, or Custom metadata from the source object. In this case, we use the 5 GB max, but if your object is larger than you must explicitly add them.
aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse aws:kms --metadata tag1=value1
aws s3 cp s3://awsexamplebucket/myfile s3://awsexamplebucket/myfile --sse aws:kms --acl bucket-owner-full-control
More examples and information can be found in the AWS CLI documentation.
What would encryption of existing objects using the CLI cost?As we are not moving any data outside of S3, there is no additional data transfer costs incurred for the encryption by using the CLI instructions in this post. You only incur the costs of the LIST and COPY API Calls, and if using SSE-KMS, the cost of encrypting objects. If you do not delete the previous version of your now encrypted objects, you will be charged for the storage of both versions of the objects.
Cleaning upAfter completing the encryption steps outlined in the post, you want to reset the AWS CLI settings to their defaults or some value that is optimized for your use case.
These commands return your CLI settings to default:
aws configure set default.s3.max_concurrent_requests 10
aws configure set default.s3.multipart_threshold 8MB
aws configure set default.s3.multipart_chunksize 8MB
More CLI configuration information and options can be found here.
ConclusionIn this post, I demonstrated how to use the AWS CLI to encrypt existing data in your Amazon S3 buckets to help ensure that your data is protected. I also covered several things to consider when encrypting your objects, as well as a few suggestions. Running these commands allows you to quickly and easily complete operations like encrypting all existing objects in your S3 bucket to meet compliance, or internal, guidelines. This can save you time setting up your encryption while enabling you to achieve high levels of data security.
Thanks for reading this blog post about encrypting objects in Amazon S3 using the AWS CLI, please leave a comment if you have any feedback or questions!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4