AWS CLI Bucket Lifecycle Configuration

I was banging my head against the wall (not quite literally, but it was close) trying to come up with a S3 lifecycle configuration from scratch. I needed a JSON file that I can run in a bash script, so I can apply the lifecycle rules to a few dozen S3 buckets. Obviously, trying to create one from scratch wasn’t the wisest choice in my part. As it turns out, you can setup a temporary bucket and use the console to recreate the lifecycle, then export the lifecycle JSON file. Duh! Genius! Here’s the command to export the lifecycle configuration.

aws s3api get-bucket-lifecycle-configuration \
--bucket bucket-name

The output would be in a JSON format similar to this:

{
    "Rules": [
        {
            "Status": "Enabled",
            "NoncurrentVersionExpiration": {
                "NoncurrentDays": 90
            },
            "NoncurrentVersionTransitions": [
                {
                    "NoncurrentDays": 7,
                    "StorageClass": "INTELLIGENT_TIERING"
                }
            ],
            "Filter": {
                "Prefix": ""
            },
            "Expiration": {
                "ExpiredObjectDeleteMarker": true
            },
            "AbortIncompleteMultipartUpload": {
                "DaysAfterInitiation": 7
            },
            "ID": "EFS S3 Backup Lifecycle Rules"
        }
    ]
}

AWS S3 Intelligent-Tiering

AWS recently released a new S3 feature called Intelligent-Tiering. It’s designed to optimize storage costs automatically when data access patterns change, without performance impact or operational overhead. S3 Intelligent-Tiering delivers automatic cost savings by moving data between two access tiers, frequent access and infrequent access, when access patterns change. To activate, all you need to do create or edit your current S3 lifecycle.

Transition to Intelligent-Tiering

AWS CLI: Describe Instances

If you have a ton of EC2 instances, one way to get results quickly is to run to use AWS CLI such as “describe-instances.” Here’s an example on how to get a list of instances with a tag name equal to some value. In addition, you limit the display only results you want displayed.

$ aws ec2 describe-instances \
--query 'Reservations[].Instances[].Tags[?Key==`Name`].Value' \
--filter Name=tag:Project,Values=web \
--region us-east-1 \
--profile default \
--output text

This returns values of the “Name” tag name in instances where they occur.

AWS CLI S3 Lifecycle

Here’s how to add a lifecycle management to S3 buckets via the CLI. In this example, expiration is set to 7 days. S3 will auto delete expired objects.

$aws s3api put-bucket-lifecycle-configuration \
--bucket bucketname --lifecycle-configuration file://lifecycle.json

The lifecycle is defined in this JSON file.

{
  "Rules": [ 
    {
       "ID": "Expires in 7 DAYS", 
       "Prefix": "", 
       "Status": "Enabled", 
       "Expiration": { 
         "Days": 7
         }
    } 
  ] 
}