# network-flow-logs / oci / t0 "Quick start"
#
# Flow logs for one subnet: every connection accepted or rejected there, kept
# 90 days in the Logging service, where Logging Search can query them.
#
# Change the two OCIDs below. The provider reads everything else, region
# included, from the DEFAULT profile in ~/.oci/config. Then:
#   terraform init && terraform apply
#
# Verify: records arrive a few minutes after traffic. In the console, open
# Logging, then Search, and run:
#   search "<compartment>/vcn-flow-logs" | where data.action = 'REJECT' | sort by datetime desc

locals {
  compartment_ocid = "ocid1.compartment.oc1..change-me" # change me
  subnet_ocid      = "ocid1.subnet.oc1..change-me"      # change me

  tags = {
    owner               = "security-engineering"
    environment         = "prod"
    data_classification = "internal"
    managed_by          = "terraform"
  }
}

terraform {
  required_version = ">= 1.6"
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 9.0"
    }
  }
}

provider "oci" {}

resource "oci_logging_log_group" "flows" {
  compartment_id = local.compartment_ocid
  display_name   = "vcn-flow-logs"
  description    = "Flow logs for the subnets named in the network flow logs template."
  freeform_tags  = local.tags
}

# Logging keeps records for 30 to 180 days, in steps of 30.
resource "oci_logging_log" "subnet" {
  display_name       = "subnet-flows"
  log_group_id       = oci_logging_log_group.flows.id
  log_type           = "SERVICE"
  is_enabled         = true
  retention_duration = 90
  freeform_tags      = local.tags

  configuration {
    compartment_id = local.compartment_ocid
    source {
      category    = "all"
      resource    = local.subnet_ocid
      service     = "flowlogs"
      source_type = "OCISERVICE"
    }
  }
}

output "log_id" {
  description = "The flow log."
  value       = oci_logging_log.subnet.id
}
