Skip to content

Commit 41fc6d8

Browse files
Simplify module interface for flow logs
1 parent 8111b81 commit 41fc6d8

File tree

8 files changed

+16
-28
lines changed

8 files changed

+16
-28
lines changed

examples/vpc-flow-create-s3-bucket/main.tf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ module "vpc" {
2424
single_nat_gateway = true
2525

2626
enable_flow_log = true
27-
create_flow_log_cloudwatch_log_group = false
28-
create_flow_log_cloudwatch_iam_role = false
29-
create_flow_log_s3_bucket = true
3027
push_flow_log_to_s3 = true
3128
flow_log_force_destroy_s3_bucket = true
3229

examples/vpc-flow-provided-cloudwatch-log-group/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
# VPC Flow Logs provided CloudWatch log group
22

33
Configuration in this directory creates set of VPC resources with VPC FLow Logs enabled and configured to push the logs to CloudWatch using a provided (ie. pre-existing) log group.
4+
This configuration requires that the CloudWatch log group is already created before enabling the VPC FLow Logs because otherwise terraform will complain about a count attribute being computed.
5+
This is because the conditional logic that determines if a new CloudWatch log group should be created is dependent on the ARN string being empty, which cannot be determined before the actual resource is created.
6+
To that end, we can leverage terraform targeted plans to first create the log group, and then the VPC with Flow Logs enabled.
7+
A realist scenario for this is to have the log group created in a separate configuration (different layer or even account) and read in via terraform remote state.
8+
9+
One way to avoid this issue would be the introduction of new boolean arguments that control the creation logic, but that would add at least two more boolean arguments that users need to set correctly.
410

511
## Usage
612

713
To run this example you need to execute:
814

915
```bash
1016
$ terraform init
17+
$ terraform apply -target aws_cloudwatch_log_group.vpc_flow_log
1118
$ terraform plan
1219
$ terraform apply
1320
```

examples/vpc-flow-provided-cloudwatch-log-group/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module "vpc" {
2424
single_nat_gateway = true
2525

2626
enable_flow_log = true
27-
create_flow_log_cloudwatch_log_group = false
2827
flow_log_destination_arn = aws_cloudwatch_log_group.vpc_flow_log.arn
2928

3029
tags = {

examples/vpc-flow-provided-cloudwatch-role/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
# VPC Flow Logs provided IAM role for CloudWatch
22

33
Configuration in this directory creates set of VPC resources with VPC FLow Logs enabled and configured to push the logs to CloudWatch using a provided (ie. pre-existing) IAM role.
4+
This configuration requires that the IAM role is already created before enabling the VPC FLow Logs because otherwise terraform will complain about a count attribute being computed.
5+
This is because the conditional logic that determines if a new IAM role should be created is dependent on the ARN string being empty, which cannot be determined before the actual role is created.
6+
To that end, we can leverage terraform targeted plans to first create the role, and then the VPC with Flow Logs enabled.
7+
A realist scenario for this is to have the IAM role created in a separate configuration (different layer or even account) and read in via terraform remote state.
48

9+
One way to avoid this issue would be the introduction of new boolean arguments that control the creation logic, but that would add at least two more boolean arguments that users need to set correctly.
510

611
## Usage
712

813
To run this example you need to execute:
914

1015
```bash
1116
$ terraform init
17+
$ terraform apply -target aws_iam_role.vpc_flow_log_cloudwatch
1218
$ terraform plan
1319
$ terraform apply
1420
```

examples/vpc-flow-provided-cloudwatch-role/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module "vpc" {
2424
single_nat_gateway = true
2525

2626
enable_flow_log = true
27-
create_flow_log_cloudwatch_iam_role = false
2827
flow_log_cloudwatch_iam_role_arn = aws_iam_role.vpc_flow_log_cloudwatch.arn
2928

3029
tags = {

examples/vpc-flow-provided-s3-bucket/main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ module "vpc" {
2424
single_nat_gateway = true
2525

2626
enable_flow_log = true
27-
create_flow_log_cloudwatch_log_group = false
28-
create_flow_log_cloudwatch_iam_role = false
2927
push_flow_log_to_s3 = true
3028
flow_log_destination_arn = aws_s3_bucket.vpf_flow_log.arn
3129

flow-logs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ locals {
22
# Only create flow log if user selected to create a vpc as well
33
enable_flow_log = var.create_vpc && var.enable_flow_log
44

5-
create_flow_log_cloudwatch_iam_role = local.enable_flow_log && var.create_flow_log_cloudwatch_iam_role
6-
create_flow_log_cloudwatch_log_group = local.enable_flow_log && var.create_flow_log_cloudwatch_log_group
7-
create_flow_log_s3_bucket = local.enable_flow_log && var.create_flow_log_s3_bucket
5+
create_flow_log_s3_bucket = local.enable_flow_log && var.push_flow_log_to_s3 && var.flow_log_destination_arn == ""
6+
create_flow_log_cloudwatch_iam_role = local.enable_flow_log && !var.push_flow_log_to_s3 && var.flow_log_cloudwatch_iam_role_arn == ""
7+
create_flow_log_cloudwatch_log_group = local.enable_flow_log && !var.push_flow_log_to_s3 && var.flow_log_destination_arn == ""
88

99
flow_log_cloudwatch_destination = local.create_flow_log_cloudwatch_log_group ? join("", aws_cloudwatch_log_group.flow_log.*.arn) : var.flow_log_destination_arn
1010
flow_log_s3_destination = local.create_flow_log_s3_bucket ? join("", aws_s3_bucket.flow_log.*.arn) : var.flow_log_destination_arn

variables.tf

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,30 +1414,12 @@ variable "flow_log_destination_arn" {
14141414
default = ""
14151415
}
14161416

1417-
variable "create_flow_log_cloudwatch_log_group" {
1418-
description = "Whether or not to create acloudWatch log group"
1419-
type = bool
1420-
default = true
1421-
}
1422-
1423-
variable "create_flow_log_cloudwatch_iam_role" {
1424-
description = "Whether or not to create an IAM role to push VPC Flow Logs to CloudWatch"
1425-
type = bool
1426-
default = true
1427-
}
1428-
14291417
variable "flow_log_cloudwatch_iam_role_arn" {
14301418
description = "The ARN for the IAM role that's used to post flow logs to a CloudWatch Logs log group. When create_flow_log_cloudwatch_iam_role is set to false, this argument needs to be provided.When create_flow_log_cloudwatch_iam_role is set to true, this argument is ignored."
14311419
type = string
14321420
default = ""
14331421
}
14341422

1435-
variable "create_flow_log_s3_bucket" {
1436-
description = "Whether or not to create an S3 bucket to push the VPC Flow Logs to"
1437-
type = bool
1438-
default = false
1439-
}
1440-
14411423
variable "flow_log_force_destroy_s3_bucket" {
14421424
description = "Whether or not to force destroy the Flow Logs S3 bucket created by this module"
14431425
type = bool

0 commit comments

Comments
 (0)