-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
187 lines (145 loc) · 4.34 KB
/
Copy pathmain.tf
File metadata and controls
187 lines (145 loc) · 4.34 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
data "aws_caller_identity" "current" {}
locals {
role_arn = resource.aws_iam_role.role.arn
env_vars = {
SUBNET_ID = module.network.subnet_id # Subnet Id for compliant server
IAM_PROFILE_ARN = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:instance-profile/AmazonSSMRoleForInstancesQuickSetup" # IAM profile ARN for compliant server
QUEUE_URL = aws_sqs_queue.queue.id
SG_ID = module.network.sg_id # Security group ID for compliant server
ROLE_NAME = var.iam_role
}
}
# IAM role
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "role" {
name = var.iam_role
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
managed_policy_arns = [aws_iam_policy.iam_policy.arn]
}
resource "aws_iam_policy" "iam_policy" {
name = "${var.project}_policy"
policy = file("data/iamPolicy.json")
}
# create necessary network resources: VPC, route table, 1 Internet gateway, 1 public subnet, 1 security group
data "aws_availability_zones" "available" {
state = "available"
}
module "network" {
source = "./modules/networking"
project = var.project
vpc_cidr = "10.0.0.0/24"
subnet_cidr = "10.0.0.0/28"
az = data.aws_availability_zones.available.names[0]
}
# creates lambda functions
module "initiate" {
source = "./modules/lambda_function"
rule_name = "initiate"
env_vars = local.env_vars
role_arn = local.role_arn
}
module "list_instances" {
source = "./modules/lambda_function"
env_vars = local.env_vars
rule_name = "list_instances"
rule = true
role_arn = local.role_arn
}
# create sqs queue for validateInstanceCompliance
resource "aws_sqs_queue" "queue" {
name = "patch_inspect_instances"
delay_seconds = 5
message_retention_seconds = 86400
receive_wait_time_seconds = 20
visibility_timeout_seconds = 920
tags = {
Name = "patch_inspect_instances"
Service = "sqs"
}
}
resource "aws_lambda_event_source_mapping" "sqs_lambda_mapping" {
event_source_arn = aws_sqs_queue.queue.arn
function_name = module.validate_instance_compliance.function_arn
maximum_batching_window_in_seconds = 30
batch_size = 50
scaling_config {
maximum_concurrency = 4
}
}
module "validate_instance_compliance" {
source = "./modules/lambda_function"
env_vars = local.env_vars
rule_name = "validate_instance_compliance"
role_arn = local.role_arn
}
# creates log group to publish findings
resource "aws_cloudwatch_event_rule" "findings_rule" {
name = "${var.project}_findings"
description = "Rule to trigger Patch Inspect findings"
event_pattern = file("data/event_pattern/findings.json")
tags = {
Name = "${var.project}_findings"
Service = "eventbridge"
}
}
resource "aws_cloudwatch_event_target" "rule_target" {
target_id = "log"
arn = aws_cloudwatch_log_group.findings.arn
rule = aws_cloudwatch_event_rule.findings_rule.name
}
resource "aws_cloudwatch_log_group" "findings" {
name = "/aws/events/${var.project}_findings"
tags = {
Name = "/aws/events/${var.project}_findings"
Service = "cloudwatch"
}
}
data "aws_iam_policy_document" "log_policy" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogStream"
]
resources = [
"${aws_cloudwatch_log_group.findings.arn}:*"
]
principals {
type = "Service"
identifiers = [
"events.amazonaws.com"
]
}
}
statement {
effect = "Allow"
actions = [
"logs:PutLogEvents"
]
resources = [
"${aws_cloudwatch_log_group.findings.arn}:*:*"
]
principals {
type = "Service"
identifiers = [
"events.amazonaws.com"
]
}
condition {
test = "ArnEquals"
values = [aws_cloudwatch_event_rule.findings_rule.arn]
variable = "aws:SourceArn"
}
}
}
resource "aws_cloudwatch_log_resource_policy" "findings_policy" {
policy_document = data.aws_iam_policy_document.log_policy.json
policy_name = "${var.project}-findings-publishing-policy"
}