-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/data.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/defaults.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/prefixes.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/variables.common.subnet_ids.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/variables.common.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/variables.common.vpc.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/variables.common.vpc_id.tf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../common/version.tf |