Skip to content

Commit

Permalink
add flowlogs
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed May 14, 2021
1 parent 1066efd commit 9c188e6
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions flowlogs/data.tf
1 change: 1 addition & 0 deletions flowlogs/defaults.tf
132 changes: 132 additions & 0 deletions flowlogs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* # About aws-vpc-setup :: flowlogs
*
* This submodule creates VPC flow logs. It creates one for private and one for public subnets. Run this once
* per VPC. You will need to run the flow logs role once before (aws-vpc-setup/flowlogs-role)
*
* # Usage
*
* ```hcl
* module "flowlog" {
* source = "git@github.e.it.census.gov:terraform-modules/aws-vpc-setup.git//flowlogs"
* vpc_id = var.vpc_id
* vpc_full_name = var.vpc_full_name
* account_alias = "ma6-gov"
* public_subnets_ids = module.subnets.public_subnets_ids
* private_subnets_ids = module.subnets.private_subnets_ids
* flowlog_bucket_arn = {flowlog-s3-bucket-arn}
* flowlog_role_arn = {flowlog-role-arn}
*
* # optional
* public_subnet_ids = [ for s in module.subnets.public_subnets_ids : s.id ]
* private_subnet_ids = [ for s in module.subnets.private_subnets_ids : s.id ]
* vpc_name = var.vpc_name
* vpc_short_name = var.vpc_short_name
*
* tags = {}
* }
*/

locals {
account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id
account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew"

base_tags = {
"boc:tf_module_version" = local._module_version
"boc:created_by" = "terraform"
}

public_ids = length(var.public_subnet_ids) > 0 ? var.public_subnet_ids : [for subnet in var.public_subnets_ids : subnet.id]
private_ids = length(var.private_subnet_ids) > 0 ? var.private_subnet_ids : [for subnet in var.private_subnets_ids : subnet.id]

splunk_account_alias = replace(var.account_alias, "do2", "do1")
flowlog_stream_name = replace(aws_cloudwatch_log_group.flowlog.name, local._prefixes["log-group"], local._prefixes["log-stream"])
}

#---
# flow logs:
# use s3 for flow logs, create two, one for the whole vpc, and one for just public
#---
resource "aws_flow_log" "flowlog_public" {
for_each = toset(local.public_ids)
log_destination = format("%v/%v-%v/", var.flowlog_bucket_arn, var.vpc_full_name, "public")
log_destination_type = "s3"
iam_role_arn = var.flowlog_role_arn
traffic_type = "ALL"
subnet_id = each.key
}

# whole VPC
resource "aws_flow_log" "flowlog" {
log_destination = format("%v/%v/", var.flowlog_bucket_arn, var.vpc_full_name)
log_destination_type = "s3"
iam_role_arn = var.flowlog_role_arn
traffic_type = "ALL"
vpc_id = var.vpc_id
}

#---
# flowlog, cloudwatch
#---
resource "aws_cloudwatch_log_group" "flowlog" {
name = format("%v-%v", local._prefixes["log-group"], var.vpc_full_name)
}

resource "aws_flow_log" "flowlog_cloudwatch" {
log_destination = aws_cloudwatch_log_group.flowlog.arn
iam_role_arn = var.flowlog_role_arn
traffic_type = "ALL"
vpc_id = var.vpc_id
}

resource "aws_kinesis_stream" "flowlog" {
name = local.flowlog_stream_name
shard_count = 1
retention_period = 48
shard_level_metrics = ["IncomingBytes", "OutgoingBytes", "IncomingRecords", "OutgoingRecords"]
tags = merge(
local.base_tags,
local.tags,
tomap({ "Name" = local.flowlog_stream_name }),
)
}

# have to add the flowlog arn here to the policy used by flowlogs in common/{east,west}/flowlog.tf
resource "aws_cloudwatch_log_subscription_filter" "flowlog" {
name = local.flowlog_stream_name
role_arn = var.flowlog_role_arn
log_group_name = aws_cloudwatch_log_group.flowlog.name
destination_arn = aws_kinesis_stream.flowlog.arn
filter_pattern = "[action=*]"
distribution = "ByLogStream"
}

output "kinesis_flowlog_arn" {
description = "VPC Flowlog Kinesis stream ARN"
value = aws_kinesis_stream.flowlog.arn
}

#---
# generate splunk inputs file
#---
data "template_file" "splunk_flowlog_tasks_flowlog" {
template = file("${path.module}/templates/aws_kinesis_tasks.conf.tpl")
vars = {
account_id = var.account_id
account_alias = local.splunk_account_alias
region = local.region
flowlog_name = aws_cloudwatch_log_group.flowlog.name
flowlog_stream_name = local.flowlog_stream_name
}
}

resource "null_resource" "splunk_flowlog_tasks_flowlog" {
provisioner "local-exec" {
working_dir = path.root
command = "test -d setup || mkdir setup"
}
provisioner "local-exec" {
working_dir = "${path.root}/setup"
command = "echo '${data.template_file.splunk_flowlog_tasks_flowlog.rendered}' > aws_kinesis_tasks.${local.flowlog_stream_name}.conf"
}
}
1 change: 1 addition & 0 deletions flowlogs/prefixes.tf
9 changes: 9 additions & 0 deletions flowlogs/templates/aws_kinesis_tasks.conf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[${account_alias}-flowlogs-${region}_${flowlog_name}]
account = ${account_alias}
format = CloudWatchLogs
index = aws_vpc_flow_logs
region = ${region}
sourcetype = aws:cloudwatchlogs:vpcflow
init_stream_position = LATEST
stream_names = ${flowlog_stream_name}

1 change: 1 addition & 0 deletions flowlogs/variables.common.subnet_ids.tf
1 change: 1 addition & 0 deletions flowlogs/variables.common.tf
1 change: 1 addition & 0 deletions flowlogs/variables.common.vpc.tf
1 change: 1 addition & 0 deletions flowlogs/variables.common.vpc_id.tf
1 change: 1 addition & 0 deletions flowlogs/version.tf

0 comments on commit 9c188e6

Please sign in to comment.