Skip to content

Commit 1e130ab

Browse files
authored
Merge pull request #1049 from gursewak1997/versioning-tutorial
Add steps to create and configure a versioned S3 bucket
2 parents a3da160 + da15d76 commit 1e130ab

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

HACKING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ If you want to store builds persistently, now is a good time to allocate
242242
S3 storage. See the [upstream coreos-assembler docs](https://github.com/coreos/coreos-assembler/blob/main/README-design.md)
243243
around build architecture.
244244

245+
If you need to set up versioning and lifecycle management for the bucket, refer to [s3 bucket versioning doc](https://github.com/coreos/fedora-coreos-pipeline/blob/main/docs/s3-bucket-versioning.md) for detailed steps. For FCOS production builds using the `fcos-builds` bucket we apply a lifecycle policy of 14 days to manage old versions efficiently.
246+
245247
Today, the FCOS pipeline is oriented towards having its own
246248
bucket; this will likely be fixed in the future. But using your
247249
credentials, you should now do e.g.:

docs/s3-bucket-versioning.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)