日本語

How to Test S3 Lifecycle Rules with AWS Emulator

​​AWS Emulator​Amazon S3,AWS CLI

With AWS Emulator, once you set an S3 lifecycle rule, you can check which objects are scheduled to expire and when, using the Expiration field in the response. You can build rules that automatically delete old logs or temporary files after a number of days (that is, attach an expiration) locally without using an AWS account, which makes them useful for catching mistakes in your rule definitions. However, even after I advanced the clock in the test environment, the emulator did not delete any objects. The commands and output are included as they are.

Note

The test environment was Ubuntu 24.04 (x86_64), AWS Emulator 1.0.602 (released November 24, 2026), and AWS CLI 2.42.2. I did not use an AWS account or credentials. I did not verify how long it takes for a rule to actually delete objects in real S3, storage class transitions (Transition), pricing, or behavior on buckets with versioning enabled. Where this article describes real S3, it is based on the official documentation. According to the AWS Emulator official documentation, AWS only checks the emulator's behavior against the real service in the US West (Oregon, us-west-2) region (global services use us-east-1). The us-east-1 S3 used in this article is outside that scope.

Start the emulator and create a bucket

AWS Emulator is an emulator that accepts AWS API calls locally. According to the official documentation, no AWS account is needed, and requests are not sent to AWS. How to install it and the environment variables that point the AWS CLI at the emulator are covered in the separate articles "How to install AWS Emulator on Ubuntu and get started" and "How to store and retrieve files in S3 with AWS Emulator." Here, I work against the emulator started with aws-emulator -t off, in a shell where the environment variables from the official documentation are set. -t off turns off the usage data that the emulator sends to AWS by default (telemetry).

If the connection works, the user name in the ARN from aws sts get-caller-identity is emulator (the 12-digit account ID is replaced).

text
$ aws sts get-caller-identity --query Arn --output text
arn:aws:iam::<12桁のアカウントID>:user/emulator

Right after creating a bucket, there is no lifecycle configuration. Reading it with get-bucket-lifecycle-configuration returned NoSuchLifecycleConfiguration.

text
$ aws s3 mb s3://example-bucket
make_bucket: example-bucket
$ aws s3api get-bucket-lifecycle-configuration --bucket example-bucket

aws: [ERROR]: An error occurred (NoSuchLifecycleConfiguration) when calling the GetBucketLifecycleConfiguration operation: The lifecycle configuration does not exist

Add a rule that deletes objects under logs/ after 1 day

Rules are written in a JSON file and passed to put-bucket-lifecycle-configuration. The following rule means that objects whose keys start with logs/ are deleted (Expiration) one day after they are stored. Setting Status to Enabled makes the rule take effect.

text
$ cat lifecycle.json
{
  "Rules": [
    {
      "ID": "expire-logs",
      "Status": "Enabled",
      "Filter": { "Prefix": "logs/" },
      "Expiration": { "Days": 1 }
    }
  ]
}
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration file://lifecycle.json
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K"
}

The TransitionDefaultMinimumObjectSize value in the response shows the default handling of the minimum object size for moving objects between storage classes. According to the PutBucketLifecycleConfiguration API reference, all_storage_classes_128K means that, by default, objects smaller than 128 KB are not moved to any storage class. This rule only deletes objects, so it has no effect here.

When I read it back, the rule I put was returned as is.

text
$ aws s3api get-bucket-lifecycle-configuration --bucket example-bucket
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K",
    "Rules": [
        {
            "Expiration": {
                "Days": 1
            },
            "ID": "expire-logs",
            "Filter": {
                "Prefix": "logs/"
            },
            "Status": "Enabled"
        }
    ]
}

Expiration shows which objects expire and when

I put logs/app.log, which matches the rule, and keep/app.log, which does not. The response to put-object for logs/app.log included an Expiration value.

text
$ aws s3api put-object --bucket example-bucket --key logs/app.log --body app.log
{
    "Expiration": "expiry-date=\"Thu, 26 Nov 2026 00:00:00 GMT\", rule-id=\"expire-logs\"",
    "ETag": "\"9a115ebc25a0a303b3ffee1a7437146d\"",
    "ChecksumCRC64NVME": "aOS3oO7eYU8=",
    "ChecksumType": "FULL_OBJECT",
    "ServerSideEncryption": "AES256"
}
$ aws s3api put-object --bucket example-bucket --key keep/app.log --body app.log --query Expiration
null

expiry-date is the date and time when the object is scheduled to expire, and rule-id is the ID of the rule that set it. You can check the same information for an object that has already been stored with head-object. According to the HeadObject API reference, real S3 also returns expiry-date and rule-id in the x-amz-expiration response header when an expiration is set. The AWS CLI displays this as Expiration.

text
$ aws s3api head-object --bucket example-bucket --key logs/app.log --query "{LastModified: LastModified, Expiration: Expiration}"
{
    "LastModified": "2026-11-24T12:50:50+00:00",
    "Expiration": "expiry-date=\"Thu, 26 Nov 2026 00:00:00 GMT\", rule-id=\"expire-logs\""
}
$ aws s3api head-object --bucket example-bucket --key keep/app.log --query "{LastModified: LastModified, Expiration: Expiration}"
{
    "LastModified": "2026-11-24T12:50:51+00:00",
    "Expiration": null
}

I stored the object at 12:50 on November 24, and the rule says "1 day," but the expiration date was not November 25 at 12:50. It was November 26 at 00:00 (UTC). According to the explanation of lifecycle configuration elements, S3 adds the number of days in the rule to the time the object was created, then rounds up to midnight (UTC) of the following day. The emulator's dates also matched this calculation.

Diagram of how the expiry-date is determined for a Days: 1 rule. An object stored at 2026-11-24 12:50:50 (UTC) gets 1 day added, giving 2026-11-25 12:50:50. Rounding that up to midnight (UTC) of the following day gives 2026-11-26 00:00:00, which becomes the expiry-date. Labels in the image are in Japanese.
How expiry-date is determined from the storage time (Days: 1 case)

When testing "how many days until an object is deleted," you need to compare against this rounded-up date, not the date and time obtained by simply adding the number of days.

Replace with rules that filter by tag or size

For rule filters (Filter), you can use tags and object size in addition to a key prefix (Prefix). I replaced the rules with the following three:

  • tmp-tag: Delete objects under data/ that have the tag retention=short seven days after they are stored
  • big-files: Delete objects larger than 1,000 bytes after 30 days
  • abort-mpu: Abort incomplete multipart uploads three days after they are started

The contents of lifecycle2.json are as follows.

json
{
  "Rules": [
    {
      "ID": "tmp-tag",
      "Status": "Enabled",
      "Filter": {
        "And": {
          "Prefix": "data/",
          "Tags": [ { "Key": "retention", "Value": "short" } ]
        }
      },
      "Expiration": { "Days": 7 }
    },
    {
      "ID": "big-files",
      "Status": "Enabled",
      "Filter": { "ObjectSizeGreaterThan": 1000 },
      "Expiration": { "Days": 30 }
    },
    {
      "ID": "abort-mpu",
      "Status": "Enabled",
      "Filter": {},
      "AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 3 }
    }
  ]
}
text
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration file://lifecycle2.json
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K"
}
$ aws s3api get-bucket-lifecycle-configuration --bucket example-bucket --query "Rules[].ID"
[
    "tmp-tag",
    "big-files",
    "abort-mpu"
]

When you combine two or more conditions, such as a key prefix and a tag, wrap them in And. According to the explanation of lifecycle configuration elements, only objects that match all the conditions inside And are targeted.

put-bucket-lifecycle-configuration does not add a rule; it replaces the entire configuration. The earlier expire-logs rule was removed, and the Expiration on logs/app.log was also cleared. The API reference also says that because it overwrites the existing configuration, any rules you want to keep must be included in the new configuration.

text
$ aws s3api head-object --bucket example-bucket --key logs/app.log --query Expiration
null

I put files under data/ and checked Expiration. The tagged file got an Expiration from tmp-tag, and the 2,000-byte file got one from big-files. The small untagged file got nothing.

text
$ aws s3api put-object --bucket example-bucket --key data/tagged.txt --body app.log --tagging retention=short --query Expiration
"expiry-date=\"Wed, 02 Dec 2026 00:00:00 GMT\", rule-id=\"tmp-tag\""
$ aws s3api put-object --bucket example-bucket --key data/plain.txt --body app.log --query Expiration
null
$ aws s3api put-object --bucket example-bucket --key data/big.bin --body big.bin --query Expiration
"expiry-date=\"Fri, 25 Dec 2026 00:00:00 GMT\", rule-id=\"big-files\""

When I added a tag afterward to data/plain.txt, which was stored without a tag, it got an Expiration from tmp-tag.

text
$ aws s3api put-object-tagging --bucket example-bucket --key data/plain.txt --tagging "TagSet=[{Key=retention,Value=short}]"
$ aws s3api head-object --bucket example-bucket --key data/plain.txt --query Expiration
"expiry-date=\"Wed, 02 Dec 2026 00:00:00 GMT\", rule-id=\"tmp-tag\""

When I started a multipart upload, the response included the date the upload is scheduled to be aborted (AbortDate) and the ID of the rule that set it (AbortRuleId). These correspond to the x-amz-abort-date and x-amz-abort-rule-id headers described in the CreateMultipartUpload API reference.

text
$ aws s3api create-multipart-upload --bucket example-bucket --key data/upload.bin --query "{AbortDate: AbortDate, AbortRuleId: AbortRuleId}"
{
    "AbortDate": "2026-11-28T00:00:00+00:00",
    "AbortRuleId": "abort-mpu"
}

Setting Status to Disabled stops Expiration from being attached

When I replaced the rule with one that has Status set to Disabled, the Expiration on data/tagged.txt became null. According to the explanation of lifecycle configuration elements, S3 does nothing for the actions written in a disabled rule. This is useful when you want to temporarily stop a rule without deleting it.

text
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"tmp-tag","Status":"Disabled","Filter":{"Prefix":"data/"},"Expiration":{"Days":7}}]}'
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K"
}
$ aws s3api head-object --bucket example-bucket --key data/tagged.txt --query Expiration
null

Mistaken rules are rejected when they are put

Four of the rule mistakes were rejected with InvalidArgument: a day count of 0, a Date that is not at midnight, two rules with the same ID, and an expiration that comes before the transition (Transitions) day count.

text
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"r","Status":"Enabled","Filter":{"Prefix":"logs/"},"Expiration":{"Days":0}}]}'

aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketLifecycleConfiguration operation: 'Days' for Expiration action must be a positive integer
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"r","Status":"Enabled","Filter":{"Prefix":"logs/"},"Expiration":{"Date":"2026-12-01T09:00:00Z"}}]}'

aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketLifecycleConfiguration operation: 'Date' must be at midnight GMT
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"r","Status":"Enabled","Filter":{"Prefix":"a/"},"Expiration":{"Days":3}},{"ID":"r","Status":"Enabled","Filter":{"Prefix":"b/"},"Expiration":{"Days":3}}]}'

aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketLifecycleConfiguration operation: Rule ID must be unique. Found same ID for more than one rule
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"r","Status":"Enabled","Filter":{"Prefix":""},"Transitions":[{"Days":30,"StorageClass":"GLACIER"}],"Expiration":{"Days":20}}]}'

aws: [ERROR]: An error occurred (InvalidArgument) when calling the PutBucketLifecycleConfiguration operation: 'Days' in the Expiration action for filter '(prefix=)' must be greater than 'Days' in the Transition action

The explanation of lifecycle configuration elements also states that when you specify a date, the time is always midnight (UTC), and that rule IDs must be unique within the bucket's configuration.

In the emulator, objects were not deleted even after expiration

I tested whether objects actually get deleted in two ways.

The first was a rule with a date in the past. According to the explanation of lifecycle configuration elements, with real S3, if you specify a past date, matching objects are immediately eligible for deletion. In the emulator, a past date was attached as Expiration, but logs/app.log was still there after waiting 60 seconds.

text
$ aws s3api put-bucket-lifecycle-configuration --bucket example-bucket --lifecycle-configuration '{"Rules":[{"ID":"past-date","Status":"Enabled","Filter":{"Prefix":"logs/"},"Expiration":{"Date":"2026-01-01T00:00:00Z"}}]}'
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K"
}
$ aws s3api head-object --bucket example-bucket --key logs/app.log --query Expiration
"expiry-date=\"Thu, 01 Jan 2026 00:00:00 GMT\", rule-id=\"past-date\""
$ aws s3 ls s3://example-bucket/logs/
2026-11-24 12:50:50          4 app.log

The second was to advance the emulator's clock. I used libfaketime from the Ubuntu faketime package and advanced the clock of an emulator started on a different port (9298) by 3 days partway through. When an AWS CLI that did not have its clock advanced sent requests to the emulator with the advanced clock, the signature time was off and the requests were rejected. For that reason, I applied the same settings only to the AWS CLI after advancing the clock.

The startup and switching steps, run from the verification script, are shown below (there is no output, and the working directory path is replaced with /path/to). If you leave the contents of faketime.txt as +0d, the clock is not shifted.

text
echo '+0d' > /path/to/faketime.txt
FT="env FAKETIME_TIMESTAMP_FILE=/path/to/faketime.txt FAKETIME_NO_CACHE=1 LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1"
$FT aws-emulator -t off -p 9298 > emulator2.log 2>&1 &
export AWS_ENDPOINT_URL=https://127.0.0.1:9298 AWS_SHARED_CREDENTIALS_FILE=~/.aws/emulator/9298/emulator_credentials AWS_CONFIG_FILE=~/.aws/emulator/9298/config AWS_CA_BUNDLE=~/.aws/emulator/9298/ca.pem

I created a bucket on this emulator, put the same lifecycle.json on it, and stored two objects. Up to this point I did not advance the clock, so the AWS CLI ran normally.

text
$ aws s3 mb s3://clock-bucket
make_bucket: clock-bucket
$ aws s3api put-bucket-lifecycle-configuration --bucket clock-bucket --lifecycle-configuration file://lifecycle.json
{
    "TransitionDefaultMinimumObjectSize": "all_storage_classes_128K"
}
$ aws s3 cp app.log s3://clock-bucket/logs/app.log --no-progress
upload: ./app.log to s3://clock-bucket/logs/app.log
$ aws s3 cp app.log s3://clock-bucket/keep/app.log --no-progress
upload: ./app.log to s3://clock-bucket/keep/app.log
$ aws s3api head-object --bucket clock-bucket --key logs/app.log --query "{LastModified: LastModified, Expiration: Expiration}"
{
    "LastModified": "2026-11-24T12:52:08+00:00",
    "Expiration": "expiry-date=\"Thu, 26 Nov 2026 00:00:00 GMT\", rule-id=\"expire-logs\""
}

Next, I changed the contents of faketime.txt to +3d to advance the clock by 3 days (to November 27). The expiration date of November 26 at 00:00 had passed, but after waiting 180 seconds, logs/app.log was still there, and I could also retrieve its contents. The env FAKETIME_... at the start of each command is what sets the clock offset (the working directory path is replaced with /path/to).

text
$ env FAKETIME_TIMESTAMP_FILE=/path/to/faketime.txt FAKETIME_NO_CACHE=1 LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 date -u
Fri Nov 27 12:55:11 UTC 2026
$ env FAKETIME_TIMESTAMP_FILE=/path/to/faketime.txt FAKETIME_NO_CACHE=1 LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 aws s3 ls s3://clock-bucket/ --recursive
2026-11-24 12:52:09          4 keep/app.log
2026-11-24 12:52:08          4 logs/app.log
$ env FAKETIME_TIMESTAMP_FILE=/path/to/faketime.txt FAKETIME_NO_CACHE=1 LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 aws s3api get-object --bucket clock-bucket --key logs/app.log out.txt --query Expiration && cat out.txt
"expiry-date=\"Thu, 26 Nov 2026 00:00:00 GMT\", rule-id=\"expire-logs\""
log

The fact that the emulator's clock really had advanced is shown at the end by the RequestTimeTooSkewed error from an AWS CLI request sent without the clock offset.

text
$ aws s3 ls s3://clock-bucket/ --recursive

aws: [ERROR]: An error occurred (RequestTimeTooSkewed) when calling the ListObjectsV2 operation: The difference between the request time and the current time is too large.
Point

In this verification environment, the emulator only confirmed whether it accepts a rule and which objects get an Expiration with which date. If you want to test what happens after an expired object has been deleted (it no longer appears in listings and cannot be retrieved), one option is not to rely on the emulator but to send delete-object within the test itself to delete the object.

Even on real S3, objects are not always deleted exactly at their expiration time. According to the official documentation on expiration, for buckets without versioning enabled, S3 queues expired objects for deletion and deletes them asynchronously. There can be a delay between the expiration date and the actual deletion day, and the documentation states that no storage charges apply to objects past their expiration date.

Delete the rule

To delete the entire lifecycle configuration, use delete-bucket-lifecycle. Afterward, the bucket returned to NoSuchLifecycleConfiguration, and the Expiration on logs/app.log became null.

text
$ aws s3api delete-bucket-lifecycle --bucket example-bucket
$ aws s3api get-bucket-lifecycle-configuration --bucket example-bucket

aws: [ERROR]: An error occurred (NoSuchLifecycleConfiguration) when calling the GetBucketLifecycleConfiguration operation: The lifecycle configuration does not exist
$ aws s3api head-object --bucket example-bucket --key logs/app.log --query Expiration
null
Warning

According to the explanation of lifecycle configuration elements, on real S3 buckets without versioning enabled, objects that reach their expiration through a lifecycle rule are permanently deleted and cannot be recovered. Also, according to the official documentation on expiration, a rule applies not only to objects added after it was put in place but also to objects that already exist. Before putting a rule on a real bucket, check Expiration in the emulator to confirm that no objects you want to keep have it attached. That way you can catch mistakes early.

​Posted by 86