Skip to content

Commit bf3cb6a

Browse files
author
Alexandre Mont'Alvao
committed
refactor: enable flow logs for tgw
1 parent d8b6a24 commit bf3cb6a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

_variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,15 @@ variable "ram_organization_association" {
171171
default = true
172172
description = "Controls if a resource share of the transit gateway to AWS Organizations should be created."
173173
}
174+
175+
variable "flow_logs" {
176+
type = bool
177+
default = true
178+
description = "Enable or disable Transit Gateway Flow Logs"
179+
}
180+
181+
variable "flow_logs_retention" {
182+
type = number
183+
default = 365
184+
description = "Retention in days for Transit Gateway Flow Logs CloudWatch Log Group"
185+
}

flow-logs.tf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
resource "aws_flow_log" "tgw" {
2+
count = var.flow_logs ? 1 : 0
3+
iam_role_arn = aws_iam_role.flow_logs[0].arn
4+
log_destination = aws_cloudwatch_log_group.flow_logs[0].arn
5+
traffic_type = "ALL"
6+
transit_gateway_id = aws_ec2_transit_gateway.default[0].id
7+
max_aggregation_interval = 60
8+
9+
tags = {
10+
"Name" = "${var.name}-tgw-flow-logs"
11+
}
12+
}
13+
14+
resource "aws_cloudwatch_log_group" "flow_logs" {
15+
count = var.flow_logs ? 1 : 0
16+
name = "/aws/vpc/${var.name}-tgw/flow-logs"
17+
retention_in_days = var.flow_logs_retention
18+
}
19+
20+
resource "aws_iam_role" "flow_logs" {
21+
count = var.flow_logs ? 1 : 0
22+
name = "${var.name}-tgw-flow-logs-role"
23+
24+
assume_role_policy = <<EOF
25+
{
26+
"Version": "2012-10-17",
27+
"Statement": [
28+
{
29+
"Sid": "",
30+
"Effect": "Allow",
31+
"Principal": {
32+
"Service": "vpc-flow-logs.amazonaws.com"
33+
},
34+
"Action": "sts:AssumeRole"
35+
}
36+
]
37+
}
38+
EOF
39+
}
40+
41+
resource "aws_iam_role_policy" "flow_log" {
42+
count = var.flow_logs ? 1 : 0
43+
name = "${var.name}-tgw-flow-logs-policy"
44+
role = aws_iam_role.flow_logs[0].id
45+
46+
policy = <<EOF
47+
{
48+
"Version": "2012-10-17",
49+
"Statement": [
50+
{
51+
"Action": [
52+
"logs:CreateLogGroup",
53+
"logs:CreateLogStream",
54+
"logs:PutLogEvents",
55+
"logs:DescribeLogGroups",
56+
"logs:DescribeLogStreams"
57+
],
58+
"Effect": "Allow",
59+
"Resource": "*"
60+
}
61+
]
62+
}
63+
EOF
64+
}

0 commit comments

Comments
 (0)