2023-07-12 20:37:30 +08:00
|
|
|
resource "aws_flow_log" "vpc-flowlog" {
|
2023-07-13 07:48:49 +08:00
|
|
|
count = var.enable-flow-log ? 1 : 0
|
|
|
|
iam_role_arn = aws_iam_role.vpcflowlog-role.arn
|
|
|
|
log_destination = aws_cloudwatch_log_group.vpcflowlog-loggroup[0].arn
|
|
|
|
traffic_type = "ALL"
|
|
|
|
vpc_id = aws_vpc.vpc.id
|
2023-07-12 20:37:30 +08:00
|
|
|
tags = {
|
|
|
|
Name = "${var.resource-prefix}-vpcflowlog"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_cloudwatch_log_group" "vpcflowlog-loggroup" {
|
|
|
|
count = var.enable-flow-log ? 1 : 0
|
|
|
|
|
|
|
|
name_prefix = "vpcflowlog/${aws_vpc.vpc.id}/"
|
2023-07-13 07:48:49 +08:00
|
|
|
kms_key_id = var.vpcflowlog-cwl-loggroup-key-arn
|
2023-07-12 20:37:30 +08:00
|
|
|
|
|
|
|
retention_in_days = var.vpcflowlog-retain-days
|
|
|
|
}
|
|
|
|
|
2023-07-13 07:48:49 +08:00
|
|
|
resource "random_id" "rid" {
|
|
|
|
byte_length = 2
|
|
|
|
}
|
|
|
|
|
2023-07-12 20:37:30 +08:00
|
|
|
resource "aws_iam_role" "vpcflowlog-role" {
|
2023-07-13 07:48:49 +08:00
|
|
|
name = "VpcFlowlogRole-${random_id.rid.dec}"
|
|
|
|
path = "/service/"
|
2023-07-12 20:37:30 +08:00
|
|
|
assume_role_policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Sid": "",
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": {
|
|
|
|
"Service": "vpc-flow-logs.amazonaws.com"
|
|
|
|
},
|
|
|
|
"Action": "sts:AssumeRole"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_iam_role_policy" "vpcflowlog-role-policy" {
|
2023-07-13 07:48:49 +08:00
|
|
|
name = "VpcFlowlogRole-${random_id.rid.dec}"
|
2023-07-12 20:37:30 +08:00
|
|
|
role = aws_iam_role.vpcflowlog-role.id
|
|
|
|
|
|
|
|
policy = <<EOF
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Action": [
|
|
|
|
"logs:CreateLogGroup",
|
|
|
|
"logs:CreateLogStream",
|
|
|
|
"logs:PutLogEvents",
|
|
|
|
"logs:DescribeLogGroups",
|
|
|
|
"logs:DescribeLogStreams"
|
|
|
|
],
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Resource": "*"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|