Well-Architected Framework
Automate cloud storage lifecycle policies
Data lifecycle management policies help organizations automatically manage cloud storage costs, meet compliance requirements, and secure sensitive data. Using infrastructure as code tools like Terraform, you can define, version, and apply lifecycle rules across AWS S3, Google Cloud Storage, and Azure Blob Storage.
Why automate data lifecycle policies
Automating cloud storage lifecycle policies addresses the following operational challenges:
Reduce uncontrolled storage costs: Without lifecycle policies, storage costs grow unchecked as teams create data but never clean it up. Objects accumulate across hot storage tiers long after anyone accesses them, driving up costs unnecessarily. Automated rules move data to lower-cost tiers like Glacier or cold storage, and delete data that has passed its retention period.
Ensure regulatory compliance: Organizations face legal and regulatory requirements for data retention that vary by industry and jurisdiction. Manually tracking retention schedules is error-prone and difficult to audit. Automated lifecycle policies apply consistent, codified retention rules to every resource, making compliance audits straightforward and reducing the risk of violations.
Minimize security risk: Sensitive data that persists beyond its retention period increases your attack surface. A breach that exposes data from three years ago can be just as damaging as one that exposes current data. Automated deletion policies ensure that data is removed on schedule, reducing the volume of sensitive information exposed in the event of a breach.
Automate policy management with infrastructure as code
You can use Terraform to define reusable modules that enforce lifecycle policies consistently across your organization. Each module can target specific data types and compliance requirements, applying the appropriate storage tier transitions and retention rules to new and existing resources.
The following Terraform configuration defines a data lifecycle policy to move AWS S3 data to Glacier Instant Retrieval after 365 days:
resource "aws_s3_bucket_lifecycle_configuration" "example" {
bucket = aws_s3_bucket.bucket.id
rule {
id = "Allow small object transitions"
filter {
object_size_greater_than = 1
}
status = "Enabled"
transition {
days = 365
storage_class = "GLACIER_IR"
}
}
}
The data lifecycle policy moves any S3 object larger than 1 byte to Glacier Instant Retrieval (GLACIER_IR) after 365 days. Glacier Instant Retrieval costs significantly less than standard S3 storage while still providing millisecond retrieval times, making it suitable for infrequently accessed data that must remain available. The object_size_greater_than filter excludes zero-byte placeholder objects that do not benefit from tiering.
Terraform can also tag resources with appropriate retention metadata, including creation dates, data classifications, and retention periods.
For example, you can use the tag block with AWS S3 to automatically apply tags to all resources created by Terraform. The S3 lifecycle rule specifies a filter based on a tag key and value. The rule then applies only to a subset of objects with the specific tag.
resource "aws_s3_bucket_lifecycle_configuration" "example" {
bucket = aws_s3_bucket.bucket.id
rule {
id = "rule-1"
filter {
tag {
key = "Name"
value = "Staging"
}
}
transition {
days = 30
storage_class = "GLACIER"
}
status = "Enabled"
}
}
The lifecycle rule uses a tag-based filter to target only objects tagged Name: Staging, moving them to standard Glacier storage after 30 days. Tag-based rules let you apply different retention schedules to different data classifications within the same bucket. For example, shorter retention for staging data and longer schedules for production objects.
Other cloud providers, such as Google Cloud Platform and Microsoft Azure, offer similar lifecycle management features for their storage services. Terraform manages lifecycle policies consistently across all three providers, applying the same retention and tiering rules no matter where your data lives.
HashiCorp resources
- Read the Tag cloud resources guide for applying consistent metadata that lifecycle rules can target.
- Read the Decommission infrastructure resources guide to retire storage resources safely at end of life.
- Learn how to create cloud budgets to set spending thresholds on storage costs alongside lifecycle policies.
- Read the aws_s3_bucket_lifecycle_configuration Terraform registry documentation for complete resource arguments and examples.
- Search the Terraform Registry for the cloud or database provider you use.
- Learn best practices for writing Terraform with the Terraform style guide.
- Start learning Terraform with the Get started tutorials.
External resources
- Cloud storage: AWS, GCP, and Azure
- Learn how to set the lifecycle configuration for a Google Cloud Bucket with Terraform.
- Read the AWS data retention policies enforcement guidance.
Next steps
In this section of Lifecycle management, you learned about implementing data management policies, including why you should use lifecycle policies and how to automate policy management with infrastructure as code.
To continue building your lifecycle management practices, refer to the following resources:
- Learn about Automate infrastructure provisioning
- Read the Tag cloud resources guide
- Read the Decommission infrastructure resources guide