Advanced storage management features for Amazon S3 buckets including lifecycle policies, storage class transitions, intelligent tiering, and object expiration rules. These features help optimize storage costs and manage object lifecycles automatically.
Configure automatic transitions and deletions of objects based on age, prefixes, tags, and other criteria.
/**
* Configuration for lifecycle rules that manage object transitions and deletions
*/
interface LifecycleRule {
/** Unique identifier for the rule */
id?: string;
/** Whether this rule is enabled */
enabled?: boolean;
/** Key prefix for objects affected by this rule */
prefix?: string;
/** Time after which objects are deleted */
expiration?: Duration;
/** Time after which noncurrent versions are deleted */
noncurrentVersionExpiration?: Duration;
/** Lifecycle transitions for current object versions */
transitions?: Transition[];
/** Lifecycle transitions for noncurrent object versions */
noncurrentVersionTransitions?: NoncurrentVersionTransition[];
/** Time after which incomplete multipart uploads are aborted */
abortIncompleteMultipartUploadAfter?: Duration;
/** Tag filters to apply this rule to specific objects */
tagFilters?: {[tag: string]: any};
/** Apply rule to objects with these tags */
tags?: {[key: string]: string};
/** Filter objects by size (minimum bytes) */
objectSizeGreaterThan?: number;
/** Filter objects by size (maximum bytes) */
objectSizeLessThan?: number;
}Usage Examples:
import * as s3 from '@aws-cdk/aws-s3';
import * as cdk from '@aws-cdk/core';
const bucket = new s3.Bucket(this, 'MyBucket', {
lifecycleRules: [
{
id: 'DeleteOldVersions',
enabled: true,
noncurrentVersionExpiration: cdk.Duration.days(30),
},
{
id: 'TransitionToIA',
enabled: true,
transitions: [{
storageClass: s3.StorageClass.STANDARD_INFREQUENT_ACCESS,
transitionAfter: cdk.Duration.days(30),
}],
},
{
id: 'ArchiveOldObjects',
enabled: true,
prefix: 'archive/',
transitions: [
{
storageClass: s3.StorageClass.STANDARD_INFREQUENT_ACCESS,
transitionAfter: cdk.Duration.days(30),
},
{
storageClass: s3.StorageClass.GLACIER,
transitionAfter: cdk.Duration.days(90),
},
{
storageClass: s3.StorageClass.DEEP_ARCHIVE,
transitionAfter: cdk.Duration.days(365),
},
],
expiration: cdk.Duration.days(2555), // 7 years
},
{
id: 'CleanupMultipartUploads',
enabled: true,
abortIncompleteMultipartUploadAfter: cdk.Duration.days(7),
},
{
id: 'TagBasedDeletion',
enabled: true,
tagFilters: {
'Environment': 'Development',
'Project': 'Testing',
},
expiration: cdk.Duration.days(7),
},
],
});Define how objects transition between different storage classes over time.
/**
* Configuration for transitioning objects to different storage classes
*/
interface Transition {
/** The storage class to transition to */
storageClass: StorageClass;
/** Time after object creation when this transition takes effect */
transitionAfter: Duration;
/** Specific date when this transition takes effect (alternative to transitionAfter) */
transitionDate?: Date;
}
/**
* Configuration for transitioning noncurrent object versions
*/
interface NoncurrentVersionTransition {
/** The storage class to transition to */
storageClass: StorageClass;
/** Time after the object becomes noncurrent when this transition takes effect */
transitionAfter: Duration;
/** Number of newer versions to retain before applying this transition */
noncurrentVersionsToRetain?: number;
}Predefined storage classes for cost optimization based on access patterns.
/**
* S3 storage classes for lifecycle transitions and cost optimization
*/
class StorageClass {
/** Standard storage class for frequently accessed data */
static readonly STANDARD: StorageClass;
/** Reduced redundancy storage (deprecated) */
static readonly REDUCED_REDUNDANCY: StorageClass;
/** Standard-Infrequent Access for less frequently accessed data */
static readonly STANDARD_INFREQUENT_ACCESS: StorageClass;
/** One Zone-Infrequent Access for recreatable, less frequently accessed data */
static readonly ONEZONE_INFREQUENT_ACCESS: StorageClass;
/** Intelligent tiering that automatically moves data between access tiers */
static readonly INTELLIGENT_TIERING: StorageClass;
/** Glacier storage for long-term archival */
static readonly GLACIER: StorageClass;
/** Deep Archive for long-term archival with retrieval times of 12+ hours */
static readonly DEEP_ARCHIVE: StorageClass;
/** Glacier Instant Retrieval for long-term archival with millisecond retrieval */
static readonly GLACIER_INSTANT_RETRIEVAL: StorageClass;
/** Glacier Flexible Retrieval for archival with minutes to hours retrieval */
static readonly GLACIER_FLEXIBLE_RETRIEVAL: StorageClass;
/** Create a custom storage class */
constructor(value: string);
/** The storage class value as a string */
readonly value: string;
/** String representation of the storage class */
toString(): string;
}Usage Examples:
// Using predefined storage classes
const transitions: s3.Transition[] = [
{
storageClass: s3.StorageClass.STANDARD_INFREQUENT_ACCESS,
transitionAfter: cdk.Duration.days(30),
},
{
storageClass: s3.StorageClass.INTELLIGENT_TIERING,
transitionAfter: cdk.Duration.days(60),
},
{
storageClass: s3.StorageClass.GLACIER_FLEXIBLE_RETRIEVAL,
transitionAfter: cdk.Duration.days(365),
},
];
// Noncurrent version transitions
const noncurrentTransitions: s3.NoncurrentVersionTransition[] = [
{
storageClass: s3.StorageClass.STANDARD_INFREQUENT_ACCESS,
transitionAfter: cdk.Duration.days(30),
noncurrentVersionsToRetain: 3,
},
{
storageClass: s3.StorageClass.GLACIER,
transitionAfter: cdk.Duration.days(90),
},
];Cross-Origin Resource Sharing configuration for web applications.
/**
* CORS rule configuration for cross-origin requests
*/
interface CorsRule {
/** Allowed HTTP methods */
allowedMethods: HttpMethods[];
/** Allowed origins (domains) */
allowedOrigins: string[];
/** Allowed headers in requests */
allowedHeaders?: string[];
/** Headers exposed to the browser */
exposedHeaders?: string[];
/** Cache time for preflight requests */
maxAge?: number;
/** Unique identifier for this rule */
id?: string;
}
/**
* HTTP methods allowed for CORS
*/
enum HttpMethods {
GET = 'GET',
PUT = 'PUT',
HEAD = 'HEAD',
POST = 'POST',
DELETE = 'DELETE'
}Usage Examples:
const bucket = new s3.Bucket(this, 'CorsEnabledBucket', {
cors: [
{
allowedMethods: [s3.HttpMethods.GET, s3.HttpMethods.POST],
allowedOrigins: ['https://example.com', 'https://app.example.com'],
allowedHeaders: ['Content-Type', 'Authorization'],
exposedHeaders: ['ETag'],
maxAge: 3600,
id: 'BasicCorsRule',
},
{
allowedMethods: [s3.HttpMethods.PUT, s3.HttpMethods.DELETE],
allowedOrigins: ['https://admin.example.com'],
allowedHeaders: ['*'],
id: 'AdminCorsRule',
},
],
});Configure inventory reports for bucket contents analysis.
/**
* Configuration for S3 inventory reports
*/
interface Inventory {
/** Destination bucket for inventory reports */
destination: InventoryDestination;
/** Whether this inventory configuration is enabled */
enabled?: boolean;
/** Unique identifier for this inventory configuration */
inventoryId: string;
/** Frequency of inventory reports */
frequency: InventoryFrequency;
/** Whether to include object versions in the inventory */
includeObjectVersions?: InventoryObjectVersions;
/** Additional fields to include in inventory */
optionalFields?: InventoryFormat[];
/** Key prefix for objects to include in inventory */
prefix?: string;
}
/**
* Destination configuration for inventory reports
*/
interface InventoryDestination {
/** Destination bucket for inventory files */
bucket: IBucket;
/** Key prefix for inventory files */
prefix?: string;
/** File format for inventory reports */
format?: InventoryFormat;
/** Encryption for inventory files */
bucketOwner?: string;
}
/**
* Frequency options for inventory reports
*/
enum InventoryFrequency {
DAILY = 'Daily',
WEEKLY = 'Weekly'
}
/**
* File format options for inventory reports
*/
enum InventoryFormat {
CSV = 'CSV',
ORC = 'ORC',
PARQUET = 'Parquet'
}
/**
* Options for including object versions in inventory
*/
enum InventoryObjectVersions {
ALL = 'All',
CURRENT = 'Current'
}Configure CloudWatch metrics for bucket monitoring.
/**
* Metrics configuration for S3 bucket monitoring
*/
interface BucketMetrics {
/** Unique identifier for this metrics configuration */
id: string;
/** Key prefix for objects to include in metrics */
prefix?: string;
/** Tag filters for objects to include in metrics */
tagFilters?: {[tag: string]: any};
}Usage Examples:
const bucket = new s3.Bucket(this, 'MonitoredBucket', {
metrics: [
{
id: 'EntireBucket',
},
{
id: 'ImportantFiles',
prefix: 'important/',
},
{
id: 'ProductionData',
tagFilters: {
'Environment': 'Production',
'Backup': 'Required',
},
},
],
});Configure static website hosting for S3 buckets.
/**
* Website redirect configuration
*/
interface RedirectTarget {
/** Target hostname for redirects */
hostName: string;
/** Protocol for redirects */
protocol?: RedirectProtocol;
}
/**
* Protocols available for website redirects
*/
enum RedirectProtocol {
HTTP = 'http',
HTTPS = 'https'
}
/**
* Routing rule configuration for website redirects
*/
interface RoutingRule {
/** Condition that triggers this rule */
condition?: RoutingRuleCondition;
/** Redirect instructions */
redirect: RedirectRule;
}
/**
* Condition for routing rules
*/
interface RoutingRuleCondition {
/** HTTP error code that triggers this rule */
httpErrorCodeReturnedEquals?: string;
/** Key prefix that triggers this rule */
keyPrefixEquals?: string;
}
/**
* Redirect instructions for routing rules
*/
interface RedirectRule {
/** Target hostname */
hostName?: string;
/** HTTP redirect code */
httpRedirectCode?: string;
/** Protocol for redirect */
protocol?: RedirectProtocol;
/** Replacement key */
replaceKeyWith?: string;
/** Key prefix replacement */
replaceKeyPrefixWith?: string;
}