# network-flow-logs / gcp / t0 "Quick start"
#
# A log bucket that keeps VPC flow logs for a year, queryable with SQL through
# Log Analytics, and a sink that routes them there.
#
# Flow logs are a setting on each subnet. Change the two values below, then:
#   gcloud auth application-default login
#   terraform init && terraform apply
#   terraform output switch_on        # run it for each subnet you want recorded
#
# Verify, after some traffic:
#   terraform output example_query    # run it in Log Analytics; rows come back

locals {
  project_id = "your-project-id" # change me
  region     = "us-central1"     # change me: where the subnets are
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 8.0"
    }
  }
}

provider "google" {
  project = local.project_id
}

resource "google_logging_project_bucket_config" "flows" {
  project          = local.project_id
  location         = "global"
  bucket_id        = "vpc-flow-logs"
  retention_days   = 365
  enable_analytics = true
  description      = "VPC flow logs, kept a year and queryable with SQL."
}

resource "google_logging_project_sink" "flows" {
  name        = "vpc-flow-logs"
  destination = "logging.googleapis.com/${google_logging_project_bucket_config.flows.id}"
  filter      = "logName:\"compute.googleapis.com%2Fvpc_flows\""
}

output "switch_on" {
  description = "Turns on flow logs for one subnet. Half of flows sampled, grouped every five seconds."
  value       = "gcloud compute networks subnets update <subnet> --region ${local.region} --enable-flow-logs --logging-aggregation-interval=interval-5-sec --logging-flow-sampling=0.5 --logging-metadata=include-all"
}

output "example_query" {
  description = "Every connection from one address in the last 30 days. Change the address."
  value       = <<-EOT
    SELECT
      timestamp,
      JSON_VALUE(json_payload.connection.src_ip) AS src,
      JSON_VALUE(json_payload.connection.dest_ip) AS dst,
      JSON_VALUE(json_payload.connection.dest_port) AS port,
      JSON_VALUE(json_payload.bytes_sent) AS bytes
    FROM `${local.project_id}.global.vpc-flow-logs._AllLogs`
    WHERE JSON_VALUE(json_payload.connection.src_ip) = '10.128.0.5'
      AND timestamp > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
    ORDER BY timestamp DESC
    LIMIT 100
  EOT
}
