-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.tf
More file actions
70 lines (60 loc) · 1.85 KB
/
main.tf
File metadata and controls
70 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
data "aws_partition" "current" {
}
locals {
bucket = var.create_bucket ? aws_s3_bucket.this[0].id : var.vpcflowlog_bucket
kms_key_id = var.create_kms_key ? aws_kms_key.this[0].arn : var.vpcflowlog_kms_key
partition = data.aws_partition.current.partition
}
resource "aws_cloudwatch_log_group" "this" {
count = var.log_to_cloudwatch ? 1 : 0
name_prefix = "vpcflowlog"
kms_key_id = local.kms_key_id
retention_in_days = 30
tags = var.tags
}
data "aws_iam_policy_document" "assume" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["vpc-flow-logs.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name_prefix = "vpcflowlog-role"
assume_role_policy = data.aws_iam_policy_document.assume.json
}
data "aws_iam_policy_document" "this" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogDelivery",
"logs:DeleteLogDelivery"
]
resources = ["*"]
}
}
resource "aws_iam_policy" "this" {
name_prefix = "vpcflowlog-"
policy = data.aws_iam_policy_document.this.json
}
resource "aws_iam_role_policy_attachment" "vpcflowlog-attach-localconfig-policy" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.this.arn
}
resource "aws_flow_log" "cloudwatch" {
count = var.log_to_cloudwatch ? length(var.vpc_ids) : 0
iam_role_arn = aws_iam_role.this.arn
log_destination = aws_cloudwatch_log_group.this[0].arn
traffic_type = "ALL"
vpc_id = var.vpc_ids[count.index]
}
resource "aws_flow_log" "s3" {
count = var.log_to_s3 ? length(var.vpc_ids) : 0
log_destination = "arn:${local.partition}:s3:::${local.bucket}"
log_destination_type = "s3"
traffic_type = "ALL"
vpc_id = var.vpc_ids[count.index]
}