Is it possible to enable Request Metrics on an S3 bucket via Terraform, either using the aws_s3_bucket
resource or other?
Asked
Active
Viewed 1,543 times
5

rossco
- 523
- 4
- 20
2 Answers
5
You can use aws_s3_bucket_metric resource in Terraform. Passing the name attribute as EntireBucket enables request metrics for the bucket.
resource "aws_s3_bucket" "example" {
bucket = "example"
}
resource "aws_s3_bucket_metric" "example-entire-bucket" {
bucket = "${aws_s3_bucket.example.bucket}"
name = "EntireBucket"
}

Vikyol
- 5,051
- 23
- 24
-
1This doesn't enable metrics on the S3 bucket. – ydaetskcoR Aug 13 '19 at 08:15
-
As @ydaetskcoR mentioned, I'm asking how to enable metrics on the bucket, not how monitor them or create alarms. – rossco Aug 13 '19 at 11:05
-
I have just updated my answer with the correct solution. – Vikyol Aug 13 '19 at 14:44
-
1I could not enable request metric, it just enables storage metrics – cloudbud Oct 17 '19 at 08:33
-
This enables request and data transfer metrics for me, but it's totally not clear that it would given the options. – mmrobins Aug 13 '20 at 20:18
-
"Passing the name attribute as EntireBucket enables request metrics for the bucket." In my experience, naming the resource "EntireBucket" was not necessary to enable request metrics for the bucket. I used a different name and it worked. – Gordon McCreight Oct 14 '22 at 16:04
1
There is a Terraform resource for that: aws_s3_bucket_metric
You can emit "Request Metrics" for an entire bucket or create a filter to monitor a specific folder or file that has a specific tag.
Here is an example from Terraform docs:
resource "aws_s3_bucket" "example" {
bucket = "example"
}
resource "aws_s3_bucket_metric" "example-entire-bucket" {
bucket = aws_s3_bucket.example.id
name = "EntireBucket"
}
EntireBucket is the name of a filter you will see in the AWS console - name it however you want (don't use spaces between words).
here is an example to monitor only the documents/ folder:
resource "aws_s3_bucket" "example" {
bucket = "example"
}
resource "aws_s3_bucket_metric" "example-filtered" {
bucket = aws_s3_bucket.example.id
name = "ImportantBlueDocuments"
filter {
prefix = "documents/"
}
}
Note the prefix = "documents/" - this is where you set a folder you want to monitor.
Read more on Terraform docs

Egidijus Lenk
- 11
- 1