# network-flow-logs / aws / t0 "Quick start"
#
# Every connection in one VPC, accepted or rejected, written to S3 in Parquet
# and partitioned by hour, and kept for a year. Parquet makes months of records
# cheap to store and fast to search with Athena.
#
# Change the VPC below, then:
#   terraform init && terraform apply
#   terraform output athena_table     # run it once in the Athena console
#
# Verify, after an hour of traffic:
#   terraform output example_query    # run it in Athena; rows come back

locals {
  vpc_id = "vpc-xxxxxxxxxxxxxxxxx" # change me
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  default_tags {
    tags = {
      owner               = "security-engineering"
      environment         = "prod"
      data_classification = "confidential"
      managed_by          = "terraform"
    }
  }
}

data "aws_caller_identity" "current" {}
data "aws_region" "current" {}

locals {
  account = data.aws_caller_identity.current.account_id
  region  = data.aws_region.current.region
}

resource "aws_s3_bucket" "flows" {
  bucket_prefix = "vpc-flow-logs-"
  force_destroy = true
}

resource "aws_s3_bucket_public_access_block" "flows" {
  bucket                  = aws_s3_bucket.flows.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}

resource "aws_s3_bucket_server_side_encryption_configuration" "flows" {
  bucket = aws_s3_bucket.flows.id
  rule {
    apply_server_side_encryption_by_default {
      sse_algorithm = "AES256"
    }
  }
}

resource "aws_s3_bucket_lifecycle_configuration" "flows" {
  bucket = aws_s3_bucket.flows.id
  rule {
    id     = "keep-a-year"
    status = "Enabled"
    filter {}
    expiration {
      days = 365
    }
    abort_incomplete_multipart_upload {
      days_after_initiation = 1
    }
  }
}

# The log delivery service writes the files. The conditions keep it to
# deliveries for this account.
data "aws_iam_policy_document" "flows" {
  statement {
    sid     = "LogDeliveryWrite"
    actions = ["s3:PutObject"]
    # Hour partitions write under aws-account-id=<id>, so both layouts are allowed.
    resources = ["${aws_s3_bucket.flows.arn}/AWSLogs/${local.account}/*", "${aws_s3_bucket.flows.arn}/AWSLogs/aws-account-id=${local.account}/*"]
    principals {
      type        = "Service"
      identifiers = ["delivery.logs.amazonaws.com"]
    }
    condition {
      test     = "StringEquals"
      variable = "aws:SourceAccount"
      values   = [local.account]
    }
    condition {
      test     = "StringEquals"
      variable = "s3:x-amz-acl"
      values   = ["bucket-owner-full-control"]
    }
  }
  statement {
    sid       = "LogDeliveryAclCheck"
    actions   = ["s3:GetBucketAcl"]
    resources = [aws_s3_bucket.flows.arn]
    principals {
      type        = "Service"
      identifiers = ["delivery.logs.amazonaws.com"]
    }
    condition {
      test     = "StringEquals"
      variable = "aws:SourceAccount"
      values   = [local.account]
    }
  }
}

resource "aws_s3_bucket_policy" "flows" {
  bucket = aws_s3_bucket.flows.id
  policy = data.aws_iam_policy_document.flows.json
}

resource "aws_flow_log" "vpc" {
  vpc_id                   = local.vpc_id
  traffic_type             = "ALL"
  log_destination_type     = "s3"
  log_destination          = aws_s3_bucket.flows.arn
  max_aggregation_interval = 60
  destination_options {
    file_format                = "parquet"
    hive_compatible_partitions = true
    per_hour_partition         = true
  }
  depends_on = [aws_s3_bucket_policy.flows]
}

locals {
  location = "s3://${aws_s3_bucket.flows.bucket}/AWSLogs/aws-account-id=${local.account}/aws-service=vpcflowlogs/aws-region=${local.region}/"
}

output "athena_table" {
  description = "Creates the table once. Partition projection finds each hour's files, so nothing needs loading later."
  value       = <<-EOT
    CREATE EXTERNAL TABLE vpc_flow_logs (
      version int, account_id string, interface_id string,
      srcaddr string, dstaddr string, srcport int, dstport int, protocol int,
      packets bigint, bytes bigint, start bigint, `end` bigint,
      action string, log_status string
    )
    PARTITIONED BY (year string, month string, day string, hour string)
    STORED AS PARQUET
    LOCATION '${local.location}'
    TBLPROPERTIES (
      'projection.enabled'='true',
      'projection.year.type'='integer', 'projection.year.range'='2025,2040',
      'projection.month.type'='integer', 'projection.month.range'='1,12', 'projection.month.digits'='2',
      'projection.day.type'='integer', 'projection.day.range'='1,31', 'projection.day.digits'='2',
      'projection.hour.type'='integer', 'projection.hour.range'='0,23', 'projection.hour.digits'='2',
      'storage.location.template'='${local.location}year=$${year}/month=$${month}/day=$${day}/hour=$${hour}/'
    );
  EOT
}

output "example_query" {
  description = "Every connection from one address on one day, grouped. Change the address and the date."
  value       = <<-EOT
    SELECT srcaddr, dstaddr, dstport, protocol, action, count(*) AS flows, sum(bytes) AS bytes
    FROM vpc_flow_logs
    WHERE year = '2026' AND month = '09' AND day = '01' AND srcaddr = '10.0.1.23'
    GROUP BY 1, 2, 3, 4, 5
    ORDER BY bytes DESC
    LIMIT 50;
  EOT
}
