Skip to content

Instantly share code, notes, and snippets.

@martinvirtel
Last active June 13, 2016 07:51
Show Gist options
  • Save martinvirtel/4b3646cc94c79edbfde029cd7b58d226 to your computer and use it in GitHub Desktop.
Save martinvirtel/4b3646cc94c79edbfde029cd7b58d226 to your computer and use it in GitHub Desktop.

Using AWS S3 logging to gather metrics

This repo contains a terraform file and a single-pixel transparent gif. You can use it to get approximate cookie-less usage metrics out of any web application that refernces the publicly exposed pixel.gif.

variable "logging_bucket" {
default = "logging.mvtango"
}
variable "log_bucket" {
default = "logs.mvtango"
}
variable "region" {
default = "eu-west-1"
}
variable "logging_grantee" {
default = "arn:aws:iam::858827067514:root"
}
resource "aws_s3_bucket" "logging_bucket" {
bucket = "${var.logging_bucket}"
acl = "public-read"
policy = <<eof
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::${var.logging_bucket}/*"]
}
]
}
eof
website {
index_document = "index.html"
error_document = "error.html"
}
cors_rule {
allowed_origins = ["*"]
allowed_headers = ["*"]
allowed_methods = ["GET","HEAD"]
expose_headers = ["ETag"]
max_age_seconds = 3600
}
logging {
target_bucket = "${aws_s3_bucket.log_bucket.id}"
target_prefix = "log/${var.logging_bucket}/"
}
provisioner "local-exec" {
command = "aws s3 cp ../pixel.gif s3://${var.logging_bucket}/ "
}
}
resource "aws_s3_bucket" "log_bucket" {
bucket = "${var.log_bucket}"
acl = "log-delivery-write"
lifecycle_rule {
id = "log"
prefix = "log/"
enabled = true
expiration {
days = 90
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment