0
0
AWScloud~5 mins

S3 storage class optimization in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Storing data in the cloud can cost money depending on how often you use it. S3 storage classes help you save money by choosing the right place to keep your files based on how often you need them.
When you have files that you rarely access but want to keep for a long time, like old backups.
When you want to save money by moving files that are not used often to cheaper storage automatically.
When you want to keep frequently used files quickly accessible but move older files to slower storage.
When you want to manage storage costs without manually moving files around.
When you want to protect important files by keeping copies in different storage classes.
Config File - lifecycle-policy.json
lifecycle-policy.json
{
  "Rules": [
    {
      "ID": "MoveToInfrequentAccess",
      "Status": "Enabled",
      "Filter": {
        "Prefix": ""
      },
      "Transitions": [
        {
          "Days": 30,
          "StorageClass": "STANDARD_IA"
        }
      ],
      "Expiration": {
        "Days": 365
      }
    }
  ]
}

This JSON file defines a lifecycle policy for an S3 bucket.

  • ID: Name of the rule.
  • Status: Enables the rule.
  • Filter: Applies to all files (empty prefix).
  • Transitions: Moves files to Standard-Infrequent Access after 30 days.
  • Expiration: Deletes files after 365 days.
Commands
This command applies the lifecycle policy to the S3 bucket named 'example-bucket'. It tells AWS to move files to cheaper storage after 30 days and delete them after 365 days.
Terminal
aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration file://lifecycle-policy.json
Expected OutputExpected
No output (command runs silently)
--bucket - Specifies the name of the S3 bucket.
--lifecycle-configuration - Points to the lifecycle policy file to apply.
This command checks the lifecycle policy currently set on the bucket to confirm it was applied correctly.
Terminal
aws s3api get-bucket-lifecycle-configuration --bucket example-bucket
Expected OutputExpected
{ "Rules": [ { "ID": "MoveToInfrequentAccess", "Status": "Enabled", "Filter": { "Prefix": "" }, "Transitions": [ { "Days": 30, "StorageClass": "STANDARD_IA" } ], "Expiration": { "Days": 365 } } ] }
--bucket - Specifies the bucket to check.
Key Concept

If you remember nothing else from this pattern, remember: use lifecycle policies to automatically move files to cheaper storage classes based on how often you access them to save money.

Common Mistakes
Not enabling the lifecycle rule after creating it.
The rule won't work if its status is not set to 'Enabled', so files won't move to cheaper storage.
Always set the 'Status' field to 'Enabled' in the lifecycle policy.
Using an incorrect bucket name in the command.
The policy will not apply if the bucket name is wrong, causing confusion and no cost savings.
Double-check the bucket name before running the command.
Setting transition days too low without understanding access patterns.
Files might move to slower storage too soon, causing delays when accessed.
Choose transition days based on how often you actually use the files.
Summary
Create a lifecycle policy JSON file to define when files move to cheaper storage and when they expire.
Apply the lifecycle policy to your S3 bucket using the AWS CLI command.
Verify the lifecycle policy is set correctly by retrieving it from the bucket.