|
| 1 | +# **Steps to Create and Configure a Versioned S3 Bucket** |
| 2 | + |
| 3 | +This guide provides step-by-step instructions to create and configure an Amazon S3 bucket with versioning and lifecycle management to optimize storage and reduce costs. Versioning in S3 allows you to maintain multiple versions of an object. This feature is particularly useful in scenarios of: |
| 4 | + |
| 5 | +* **Accidental Overwrites**: If a file is accidentally overwritten, you can restore a previous version without data loss. |
| 6 | +* **Accidental Deletions**: When an object is deleted, S3 creates a delete marker instead of immediately removing the object, enabling easy recovery of previous versions. |
| 7 | +* **Garbage Collection and Storage Optimization**: Over time, old versions, non-current versions, and delete markers accumulate, taking up storage. Combining versioning with **lifecycle policies** automates the cleanup of outdated versions and unnecessary delete markers. This ensures your bucket stays organized and prevents unneeded storage costs from piling up. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## **Set a Variable for the Bucket Name** |
| 12 | +You can use the name of an existing bucket or specify a new bucket name if you are creating one: |
| 13 | +```bash |
| 14 | +BUCKET_NAME=temp-bucket-for-versioning |
| 15 | +``` |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## **Create a New S3 Bucket** |
| 20 | +To create a new bucket, use the following command. Ensure the correct region is specified. Skip this step if you are working with an existing bucket. |
| 21 | +```bash |
| 22 | +aws s3api create-bucket --bucket $BUCKET_NAME --region your-region |
| 23 | +``` |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## **Enable Versioning on the Bucket** |
| 28 | +This command enables versioning to track changes in your objects. |
| 29 | + |
| 30 | +```bash |
| 31 | +aws s3api put-bucket-versioning \ |
| 32 | +--bucket $BUCKET_NAME \ |
| 33 | +--versioning-configuration Status=Enabled |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## **Add Lifecycle Configuration** |
| 39 | +The following lifecycle policy will: |
| 40 | +1. Delete **non-current versions** of objects after 14 days. |
| 41 | +2. Remove **expired delete markers** when no versions remain. |
| 42 | + |
| 43 | +Create a `lifecycle.json` file with the following content: |
| 44 | +```bash |
| 45 | +cat <<EOF > lifecycle.json |
| 46 | +{ |
| 47 | + "Rules": [ |
| 48 | + { |
| 49 | + "ID": "DeleteOldVersions", |
| 50 | + "Filter": {}, |
| 51 | + "Status": "Enabled", |
| 52 | + "NoncurrentVersionExpiration": { |
| 53 | + "NoncurrentDays": 14 |
| 54 | + }, |
| 55 | + "Expiration": { |
| 56 | + "ExpiredObjectDeleteMarker": true |
| 57 | + } |
| 58 | + } |
| 59 | + ] |
| 60 | +} |
| 61 | +EOF |
| 62 | +``` |
| 63 | + |
| 64 | +Apply the lifecycle configuration: |
| 65 | +```bash |
| 66 | +aws s3api put-bucket-lifecycle-configuration \ |
| 67 | +--bucket $BUCKET_NAME \ |
| 68 | +--lifecycle-configuration file://lifecycle.json |
| 69 | +``` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## **Verify Configuration** |
| 74 | +Check if the lifecycle configuration has been applied correctly: |
| 75 | +```bash |
| 76 | +aws s3api get-bucket-lifecycle-configuration --bucket $BUCKET_NAME |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## **Notes** |
| 82 | +- **NoncurrentVersionExpiration**: Removes old object versions 14 days after they become non-current. |
| 83 | +- **ExpiredObjectDeleteMarker**: Deletes delete markers when no object versions are left, keeping the bucket tidy. |
| 84 | + |
| 85 | +This configuration helps manage storage efficiently by automatically removing obsolete data. |
0 commit comments