0
0
AWScloud~5 mins

S3 lifecycle rules in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
S3 lifecycle rules help you automatically manage your files in cloud storage. They save money by moving or deleting files you don't need often.
When you want to delete old backup files automatically after 30 days.
When you want to move photos to cheaper storage after 60 days.
When you want to keep only recent logs in fast storage and archive older ones.
When you want to save money by cleaning up unused files regularly.
When you want to automate file management without manual work.
Config File - lifecycle.json
lifecycle.json
{
  "Rules": [
    {
      "ID": "MoveToGlacierAfter30Days",
      "Status": "Enabled",
      "Filter": {},
      "Transitions": [
        {
          "Days": 30,
          "StorageClass": "GLACIER"
        }
      ],
      "NoncurrentVersionTransitions": [],
      "Expiration": {},
      "NoncurrentVersionExpiration": {}
    },
    {
      "ID": "DeleteAfter365Days",
      "Status": "Enabled",
      "Filter": {},
      "Expiration": {
        "Days": 365
      },
      "Transitions": [],
      "NoncurrentVersionTransitions": [],
      "NoncurrentVersionExpiration": {}
    }
  ]
}

This JSON file defines two lifecycle rules for an S3 bucket.

The first rule moves files to Glacier storage after 30 days to save cost.

The second rule deletes files after 365 days to clean up old data.

Both rules are enabled and apply to all files in the bucket.

Commands
This command applies the lifecycle rules from the JSON file to the S3 bucket named 'example-bucket'. It sets up automatic file management.
Terminal
aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration file://lifecycle.json
Expected OutputExpected
No output (command runs silently)
--bucket - Specifies the name of the S3 bucket to apply the rules to
--lifecycle-configuration - Points to the JSON file with lifecycle rules
This command retrieves and shows the current lifecycle rules set on the 'example-bucket' to verify the configuration.
Terminal
aws s3api get-bucket-lifecycle-configuration --bucket example-bucket
Expected OutputExpected
{ "Rules": [ { "ID": "MoveToGlacierAfter30Days", "Status": "Enabled", "Filter": {}, "Transitions": [ { "Days": 30, "StorageClass": "GLACIER" } ], "NoncurrentVersionTransitions": [], "Expiration": {}, "NoncurrentVersionExpiration": {} }, { "ID": "DeleteAfter365Days", "Status": "Enabled", "Filter": {}, "Expiration": { "Days": 365 }, "Transitions": [], "NoncurrentVersionTransitions": [], "NoncurrentVersionExpiration": {} } ] }
--bucket - Specifies the bucket to check lifecycle rules
Key Concept

If you remember nothing else from this pattern, remember: lifecycle rules automate moving or deleting files to save cost and keep storage tidy.

Common Mistakes
Not enabling the lifecycle rule by setting Status to 'Enabled'.
The rule will not work if it is disabled, so files won't move or delete automatically.
Always set "Status": "Enabled" in each lifecycle rule.
Using incorrect JSON syntax or missing required fields in the lifecycle configuration file.
AWS will reject the configuration and not apply the rules.
Validate JSON syntax and include all required keys like ID, Status, and either Transitions or Expiration.
Applying lifecycle rules to the wrong bucket name.
Rules won't affect the intended bucket, causing confusion and no cost savings.
Double-check the bucket name in the command matches the target bucket.
Summary
Create a JSON file defining lifecycle rules to move or delete files automatically.
Use 'aws s3api put-bucket-lifecycle-configuration' to apply the rules to your bucket.
Verify the rules with 'aws s3api get-bucket-lifecycle-configuration' to ensure they are set correctly.