From 446fb63cf83ec8782f483f4c27804787364833a1 Mon Sep 17 00:00:00 2001 From: Vuong Date: Fri, 2 Aug 2024 10:54:50 +0700 Subject: [PATCH] Allow to enable S3 lifecycle for snapshot bucket. --- indexer/s3_bucket.tf | 17 ++++++++++++++++- indexer/variables.tf | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/indexer/s3_bucket.tf b/indexer/s3_bucket.tf index e0b9636b..37d77262 100644 --- a/indexer/s3_bucket.tf +++ b/indexer/s3_bucket.tf @@ -22,19 +22,34 @@ resource "aws_s3_bucket" "indexer_full_node_snapshots" { } } +# Enable S3 snapshot lifecycle to clean up old snapshots +resource "aws_s3_bucket_lifecycle_configuration" "indexer_full_node_snapshots" { + count = var.enable_s3_snapshot_lifecycle ? 1 : 0 + bucket = aws_s3_bucket.indexer_full_node_snapshots.id + + rule { + id = "expire-old-snapshots" + status = "Enabled" + + expiration { + days = var.snapshot_bucket_expiration_days + } + } +} + # Enable S3 bucket metrics to be sent to Datadog for monitoring resource "aws_s3_bucket_metric" "indexer_full_node_snapshots" { bucket = aws_s3_bucket.indexer_full_node_snapshots.id name = "EntireBucket" } + # Attach policy to s3 bucket to allow load balancer to write logs to the S3 bucket # NOTE: This resource cannot be tagged. resource "aws_s3_bucket_policy" "lb_s3_bucket_policy" { bucket = aws_s3_bucket.load_balancer.id policy = data.aws_iam_policy_document.lb_s3_bucket_policy.json } - # Policy to allow load balancer to write logs into the s3 bucket data "aws_iam_policy_document" "lb_s3_bucket_policy" { statement { diff --git a/indexer/variables.tf b/indexer/variables.tf index 37f1ed6d..66ac633f 100644 --- a/indexer/variables.tf +++ b/indexer/variables.tf @@ -472,3 +472,15 @@ variable "image_count" { description = "Number of images to store for ECR, defaults to 100." default = 100 } + +variable "enable_s3_snapshot_lifecycle" { + type = bool + description = "Enables S3 lifecycle on snapshot bucket. Default is true" + default = true +} + +variable "snapshot_bucket_expiration_days" { + type = number + description = "Number of days to store fullnode snapshot on S3, defaults to 7." + default = 7 +}