-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.tf
194 lines (155 loc) · 4.45 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
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
188
189
190
191
192
193
data "aws_iam_policy_document" "lambda" {
statement {
effect = "Allow"
actions = [
"ecr:*",
"cloudwatch:*",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = [
"*"
]
}
}
data "aws_iam_policy_document" "sfn_invoke_lamnda" {
statement {
effect = "Allow"
actions = [
"lambda:InvokeFunction"
]
resources = [
module.ecr-scan-notify-lambda.arn,
module.ecr-scan-trigger-lambda.arn
]
}
}
data "aws_iam_policy_document" "cloudwatch_start_sfn" {
statement {
effect = "Allow"
actions = [
"states:StartExecution"
]
resources = [
"*"
]
}
}
module "ecr-scan-trigger-lambda" {
source = "./templates/lambda"
policy = data.aws_iam_policy_document.lambda.json
name_prefix = "ecr-scan-trigger-${var.local_environment}"
source_dir = "${path.module}/lambdas_code/scan_trigger"
handler = "lambda_function.lambda_handler"
runtime = "python3.6"
subnet_ids = var.subnet_ids
security_group_ids = var.security_group_ids
tags = merge(
var.tags,
tomap({ "Name" = var.global_name }),
tomap({ "Project" = var.global_project }),
tomap({ "Environment" = var.local_environment })
)
}
module "ecr-scan-notify-lambda" {
source = "./templates/lambda"
policy = data.aws_iam_policy_document.lambda.json
name_prefix = "ecr-scan-notify-${var.local_environment}"
source_dir = "${path.module}/lambdas_code/scan_notify"
handler = "lambda_function.lambda_handler"
runtime = "python3.6"
subnet_ids = var.subnet_ids
security_group_ids = var.security_group_ids
environment = {
SLACK_CHANNEL = var.slack_channel
SLACK_USERNAME = var.slack_username
SLACK_EMOJI = var.slack_emoji
SLACK_WEBHOOK_URL = var.slack_webhook_url
RISK_LEVELS = var.risk_levels
}
tags = merge(
var.tags,
tomap({ "Name" = var.global_name }),
tomap({ "Project" = var.global_project }),
tomap({ "Environment" = var.local_environment })
)
}
resource "random_string" "postfix_generator" {
length = 6
upper = true
lower = true
number = true
special = false
}
resource "aws_iam_role" "iam_for_sfn" {
name = "ecr-scan-stepfunction-role-${random_string.postfix_generator.result}"
assume_role_policy = data.aws_iam_policy_document.sfn_assume_role_policy_document.json
}
resource "aws_iam_policy" "policy_for_sfn_lambdas" {
name = "policy_for_sfn_lambdas"
policy = data.aws_iam_policy_document.sfn_invoke_lamnda.json
}
resource "aws_iam_policy" "cloudwatch_start_sfn" {
name = "cloudwatch_start_sfn"
policy = data.aws_iam_policy_document.cloudwatch_start_sfn.json
}
resource "aws_iam_role_policy_attachment" "sfn_lambda_perrmissions" {
role = aws_iam_role.iam_for_sfn.name
policy_arn = aws_iam_policy.policy_for_sfn_lambdas.arn
}
resource "aws_iam_role_policy_attachment" "clodwatch_start_sfn" {
role = aws_iam_role.iam_for_sfn.name
policy_arn = aws_iam_policy.cloudwatch_start_sfn.arn
}
// Assume role policy document
data "aws_iam_policy_document" "sfn_assume_role_policy_document" {
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = [
"states.amazonaws.com",
"events.amazonaws.com"
]
}
}
}
resource "aws_sfn_state_machine" "orchestrate_scan_sfn" {
name = "ecr-scan-sfn-${var.local_environment}-${random_string.postfix_generator.result}"
role_arn = aws_iam_role.iam_for_sfn.arn
definition = <<EOF
{
"Comment": "SFN to orchastrate ECR vulnerability scanning.",
"StartAt": "TriggerScan",
"States": {
"TriggerScan": {
"Type": "Task",
"Resource": "${module.ecr-scan-trigger-lambda.arn}",
"Next": "Wait"
},
"Wait": {
"Type": "Wait",
"Seconds": 300,
"Next": "Notify"
},
"Notify": {
"Type": "Task",
"Resource": "${module.ecr-scan-notify-lambda.arn}",
"End": true
}
}
}
EOF
}
resource "aws_cloudwatch_event_rule" "trigger_scan" {
name = "ecr-trigger-scan-rule-${var.local_environment}-${random_string.postfix_generator.result}"
schedule_expression = "cron(${var.scan_trigger_schedule_expression})"
}
resource "aws_cloudwatch_event_target" "trigger_scan_sfn" {
rule = aws_cloudwatch_event_rule.trigger_scan.id
arn = aws_sfn_state_machine.orchestrate_scan_sfn.id
role_arn = aws_iam_role.iam_for_sfn.arn
}