Skip to content

Commit e1731b1

Browse files
author
Fokko Bucys
authored
feat(aws-s3): support number of newer versions to retain in lifecycle policy (#18225)
The AWS Management console and CloudFormation support to set a number of noncurrent versions to be retained for S3 lifecycle transitions as described in #17996. This pull request introduces the property for S3 lifecycle configurations as an optional property to CDK. The maximum supported number of noncurrent versions that could be retained is 100 which is also documented for the new property. However, no additional check is inserted if the number set by the developer is actually between 0 and 100. If this check is desired, a discussion would be good on how to achieve throwing an error as I don't see a smooth solution. Resolves #17996. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6da471b commit e1731b1

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,7 @@ export class Bucket extends BucketBase {
18861886
noncurrentVersionTransitions: mapOrUndefined(rule.noncurrentVersionTransitions, t => ({
18871887
storageClass: t.storageClass.value,
18881888
transitionInDays: t.transitionAfter.toDays(),
1889+
newerNoncurrentVersions: t.noncurrentVersionsToRetain,
18891890
})),
18901891
prefix: rule.prefix,
18911892
status: enabled ? 'Enabled' : 'Disabled',

packages/@aws-cdk/aws-s3/lib/rule.ts

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ export interface NoncurrentVersionTransition {
151151
* @default No transition count.
152152
*/
153153
readonly transitionAfter: Duration;
154+
155+
/**
156+
* Indicates the number of noncurrent version objects to be retained. Can be up to 100 noncurrent versions retained.
157+
*
158+
* @default No noncurrent version retained.
159+
*/
160+
readonly noncurrentVersionsToRetain?: number;
154161
}
155162

156163
/**

packages/@aws-cdk/aws-s3/test/rules.test.ts

+72
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,76 @@ describe('rules', () => {
139139
},
140140
});
141141
});
142+
143+
test('Noncurrent transistion rule with versions to retain', () => {
144+
// GIVEN
145+
const stack = new Stack();
146+
147+
// WHEN: Noncurrent version to retain available
148+
new Bucket(stack, 'Bucket1', {
149+
versioned: true,
150+
lifecycleRules: [{
151+
noncurrentVersionExpiration: Duration.days(10),
152+
noncurrentVersionTransitions: [
153+
{
154+
storageClass: StorageClass.GLACIER_INSTANT_RETRIEVAL,
155+
transitionAfter: Duration.days(10),
156+
noncurrentVersionsToRetain: 1,
157+
},
158+
],
159+
}],
160+
});
161+
162+
// THEN
163+
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
164+
LifecycleConfiguration: {
165+
Rules: [{
166+
NoncurrentVersionExpirationInDays: 10,
167+
NoncurrentVersionTransitions: [
168+
{
169+
NewerNoncurrentVersions: 1,
170+
StorageClass: 'GLACIER_IR',
171+
TransitionInDays: 10,
172+
},
173+
],
174+
Status: 'Enabled',
175+
}],
176+
},
177+
});
178+
});
179+
180+
test('Noncurrent transistion rule without versions to retain', () => {
181+
// GIVEN
182+
const stack = new Stack();
183+
184+
// WHEN: Noncurrent version to retain not set
185+
new Bucket(stack, 'Bucket1', {
186+
versioned: true,
187+
lifecycleRules: [{
188+
noncurrentVersionExpiration: Duration.days(10),
189+
noncurrentVersionTransitions: [
190+
{
191+
storageClass: StorageClass.GLACIER_INSTANT_RETRIEVAL,
192+
transitionAfter: Duration.days(10),
193+
},
194+
],
195+
}],
196+
});
197+
198+
// THEN
199+
Template.fromStack(stack).hasResourceProperties('AWS::S3::Bucket', {
200+
LifecycleConfiguration: {
201+
Rules: [{
202+
NoncurrentVersionExpirationInDays: 10,
203+
NoncurrentVersionTransitions: [
204+
{
205+
StorageClass: 'GLACIER_IR',
206+
TransitionInDays: 10,
207+
},
208+
],
209+
Status: 'Enabled',
210+
}],
211+
},
212+
});
213+
});
142214
});

0 commit comments

Comments
 (0)