Skip to content

Commit 65ed0fb

Browse files
feat: Add bucket metrics support (#190)
Co-authored-by: magreenbaum <magreenbaum> Co-authored-by: Anton Babenko <[email protected]>
1 parent fe51edf commit 65ed0fb

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ No modules.
141141
| [aws_s3_bucket_intelligent_tiering_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_intelligent_tiering_configuration) | resource |
142142
| [aws_s3_bucket_lifecycle_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
143143
| [aws_s3_bucket_logging.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_logging) | resource |
144+
| [aws_s3_bucket_metric.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_metric) | resource |
144145
| [aws_s3_bucket_object_lock_configuration.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_object_lock_configuration) | resource |
145146
| [aws_s3_bucket_ownership_controls.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_ownership_controls) | resource |
146147
| [aws_s3_bucket_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
@@ -184,6 +185,7 @@ No modules.
184185
| <a name="input_intelligent_tiering"></a> [intelligent\_tiering](#input\_intelligent\_tiering) | Map containing intelligent tiering configuration. | `any` | `{}` | no |
185186
| <a name="input_lifecycle_rule"></a> [lifecycle\_rule](#input\_lifecycle\_rule) | List of maps containing configuration of object lifecycle management. | `any` | `[]` | no |
186187
| <a name="input_logging"></a> [logging](#input\_logging) | Map containing access bucket logging configuration. | `map(string)` | `{}` | no |
188+
| <a name="input_metric_configuration"></a> [metric\_configuration](#input\_metric\_configuration) | Map containing bucket metric configuration. | `any` | `[]` | no |
187189
| <a name="input_object_lock_configuration"></a> [object\_lock\_configuration](#input\_object\_lock\_configuration) | Map containing S3 object locking configuration. | `any` | `{}` | no |
188190
| <a name="input_object_lock_enabled"></a> [object\_lock\_enabled](#input\_object\_lock\_enabled) | Whether S3 bucket should have an Object Lock configuration enabled. | `bool` | `false` | no |
189191
| <a name="input_object_ownership"></a> [object\_ownership](#input\_object\_ownership) | Object ownership. Valid values: BucketOwnerEnforced, BucketOwnerPreferred or ObjectWriter. 'BucketOwnerEnforced': ACLs are disabled, and the bucket owner automatically owns and has full control over every object in the bucket. 'BucketOwnerPreferred': Objects uploaded to the bucket change ownership to the bucket owner if the objects are uploaded with the bucket-owner-full-control canned ACL. 'ObjectWriter': The uploading account will own the object if the object is uploaded with the bucket-owner-full-control canned ACL. | `string` | `"ObjectWriter"` | no |

examples/complete/main.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,28 @@ module "s3_bucket" {
324324
}
325325
}
326326
}
327+
328+
metric_configuration = [
329+
{
330+
name = "documents"
331+
filter = {
332+
prefix = "documents/"
333+
tags = {
334+
priority = "high"
335+
}
336+
}
337+
},
338+
{
339+
name = "other"
340+
filter = {
341+
tags = {
342+
production = "true"
343+
}
344+
}
345+
},
346+
{
347+
name = "all"
348+
}
349+
]
350+
327351
}

main.tf

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ locals {
66
attach_policy = var.attach_require_latest_tls_policy || var.attach_elb_log_delivery_policy || var.attach_lb_log_delivery_policy || var.attach_deny_insecure_transport_policy || var.attach_policy
77

88
# Variables with type `any` should be jsonencode()'d when value is coming from Terragrunt
9-
grants = try(jsondecode(var.grant), var.grant)
10-
cors_rules = try(jsondecode(var.cors_rule), var.cors_rule)
11-
lifecycle_rules = try(jsondecode(var.lifecycle_rule), var.lifecycle_rule)
12-
intelligent_tiering = try(jsondecode(var.intelligent_tiering), var.intelligent_tiering)
9+
grants = try(jsondecode(var.grant), var.grant)
10+
cors_rules = try(jsondecode(var.cors_rule), var.cors_rule)
11+
lifecycle_rules = try(jsondecode(var.lifecycle_rule), var.lifecycle_rule)
12+
intelligent_tiering = try(jsondecode(var.intelligent_tiering), var.intelligent_tiering)
13+
metric_configuration = try(jsondecode(var.metric_configuration), var.metric_configuration)
1314
}
1415

1516
resource "aws_s3_bucket" "this" {
@@ -719,3 +720,18 @@ resource "aws_s3_bucket_intelligent_tiering_configuration" "this" {
719720
}
720721

721722
}
723+
724+
resource "aws_s3_bucket_metric" "this" {
725+
for_each = { for k, v in local.metric_configuration : k => v if local.create_bucket }
726+
727+
name = each.value.name
728+
bucket = aws_s3_bucket.this[0].id
729+
730+
dynamic "filter" {
731+
for_each = length(try(flatten([each.value.filter]), [])) == 0 ? [] : [true]
732+
content {
733+
prefix = try(each.value.filter.prefix, null)
734+
tags = try(each.value.filter.tags, null)
735+
}
736+
}
737+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ variable "object_lock_configuration" {
160160
default = {}
161161
}
162162

163+
variable "metric_configuration" {
164+
description = "Map containing bucket metric configuration."
165+
type = any
166+
default = []
167+
}
168+
163169
variable "object_lock_enabled" {
164170
description = "Whether S3 bucket should have an Object Lock configuration enabled."
165171
type = bool

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module "wrapper" {
3030
server_side_encryption_configuration = try(each.value.server_side_encryption_configuration, var.defaults.server_side_encryption_configuration, {})
3131
intelligent_tiering = try(each.value.intelligent_tiering, var.defaults.intelligent_tiering, {})
3232
object_lock_configuration = try(each.value.object_lock_configuration, var.defaults.object_lock_configuration, {})
33+
metric_configuration = try(each.value.metric_configuration, var.defaults.metric_configuration, [])
3334
object_lock_enabled = try(each.value.object_lock_enabled, var.defaults.object_lock_enabled, false)
3435
block_public_acls = try(each.value.block_public_acls, var.defaults.block_public_acls, false)
3536
block_public_policy = try(each.value.block_public_policy, var.defaults.block_public_policy, false)

0 commit comments

Comments
 (0)