forked from plus3it/terraform-aws-tardigrade-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
101 lines (80 loc) · 2.68 KB
/
main.tf
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
locals {
create_iam_role = var.iam_role_arn == null
iam_role_arn = local.create_iam_role ? join("", aws_iam_role.this.*.arn) : var.iam_role_arn
}
### RESOURCES ###
# Race condition on resource cycles requires some randomness in the name to avoid "name already exists" errors
resource "random_uuid" "assessment_template" {
keepers = {
rules_package_arns = join(",", data.aws_inspector_rules_packages.this.arns)
duration = var.duration
target_arn = aws_inspector_assessment_target.this.arn
}
}
# Adding random string to assessment target name
resource "random_string" "random_suffix" {
length = 6
special = false
}
# Create Inspector Assessment Target
resource "aws_inspector_assessment_target" "this" {
name = "${var.name}-${random_string.random_suffix.result}"
}
# Create Inspector Assessment Template
resource "aws_inspector_assessment_template" "this" {
name = "${var.name} ${random_uuid.assessment_template.result}"
target_arn = random_uuid.assessment_template.keepers.target_arn
duration = random_uuid.assessment_template.keepers.duration
rules_package_arns = split(",", random_uuid.assessment_template.keepers.rules_package_arns)
}
# Create Cloudwatch Event Rule
resource "aws_cloudwatch_event_rule" "this" {
name = var.name
description = "Run inspector scan on a schedule"
schedule_expression = var.schedule
event_pattern = var.event_pattern
tags = var.tags
}
# Create IAM Role
resource "aws_iam_role" "this" {
count = local.create_iam_role ? 1 : 0
name = var.name
assume_role_policy = data.aws_iam_policy_document.assume_role.json
tags = var.tags
}
# Create IAM Policy
resource "aws_iam_policy" "this" {
count = local.create_iam_role ? 1 : 0
name = var.name
policy = data.aws_iam_policy_document.start_inspector.json
}
# Attach Policy to IAM Role
resource "aws_iam_role_policy_attachment" "this" {
count = local.create_iam_role ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.this[0].arn
}
# Create Cloudwatch Event Target
resource "aws_cloudwatch_event_target" "this" {
rule = aws_cloudwatch_event_rule.this.name
arn = aws_inspector_assessment_template.this.arn
role_arn = local.iam_role_arn
}
### DATA SOURCES ###
data "aws_inspector_rules_packages" "this" {
}
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["events.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "start_inspector" {
statement {
actions = ["inspector:StartAssessmentRun"]
resources = ["*"]
}
}