-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathiam.tf
161 lines (158 loc) · 5.28 KB
/
iam.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
data "aws_caller_identity" "current" {}
data "aws_iam_policy" "AdministratorAccess" {
arn = "arn:aws:iam::aws:policy/AdministratorAccess"
}
data "aws_iam_policy_document" "lambda_execution_policy_document" {
statement {
actions = ["logs:CreateLogGroup"]
effect = "Allow"
resources = ["arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:*"]
}
statement {
actions = ["logs:CreateLogStream", "logs:PutLogEvents"]
effect = "Allow"
resources = ["arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/aws/lambda/${var.lambda_function_name}:*"]
}
}
data "aws_iam_policy_document" "lambda_ssm_policy_document" {
statement {
actions = [
"ssm:GetParameterHistory",
"ssm:GetParametersByPath",
"ssm:GetParameters",
"ssm:GetParameter"
]
effect = "Allow"
resources = [
"arn:aws:ssm:${var.region}:${data.aws_caller_identity.current.account_id}:parameter/*"
]
}
}
data "aws_iam_policy_document" "lambda_s3_policy_document" {
statement {
actions = [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:DeleteObject",
"s3:DeleteObjectVersion"
]
effect = "Allow"
resources = ["${data.aws_s3_bucket.bucket.arn}/*"]
}
}
data "aws_iam_policy_document" "lambda_securityhub_policy_document" {
statement {
actions = [
"securityhub:GetFindings"
]
effect = "Allow"
resources = ["arn:aws:securityhub:${var.region}:${data.aws_caller_identity.current.account_id}:hub/default"]
}
statement {
actions = [
"securityhub:BatchImportFindings"
]
effect = "Allow"
resources = ["arn:aws:securityhub:${var.region}:517716713836:product/crowdstrike/*"]
}
}
# data "aws_iam_policy_document" "ec2_s3_policy_document" {
# statement {
# actions = [
# "s3:*"
# ]
# effect = "Allow"
# resources = [data.aws_s3_bucket.bucket.arn, "${data.aws_s3_bucket.bucket.arn}/*"]
# }
# }
resource "aws_iam_policy" "lambda_ssm_policy" {
name = "${var.lambda_function_name}_ssm_policy"
policy = data.aws_iam_policy_document.lambda_ssm_policy_document.json
}
resource "aws_iam_policy" "lambda_execution_policy" {
name = "${var.lambda_function_name}_execution_policy"
policy = data.aws_iam_policy_document.lambda_execution_policy_document.json
}
resource "aws_iam_policy" "lambda_s3_policy" {
name = "${var.lambda_function_name}_s3_policy"
policy = data.aws_iam_policy_document.lambda_s3_policy_document.json
}
resource "aws_iam_policy" "lambda_securityhub_policy" {
name = "${var.lambda_function_name}_securityhub_policy"
policy = data.aws_iam_policy_document.lambda_securityhub_policy_document.json
}
# resource "aws_iam_policy" "ec2_s3_policy" {
# name = "${var.instance_name}_s3_policy"
# policy = data.aws_iam_policy_document.ec2_s3_policy_document.json
# }
# resource "aws_iam_policy" "ec2_securityhub_policy" {
# name = "${var.instance_name}_securityhub_policy"
# policy = data.aws_iam_policy_document.lambda_securityhub_policy_document.json
# }
resource "aws_iam_role" "iam_for_lambda" {
name = "${var.lambda_execution_role_name}"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow"
}
]
}
EOF
}
# resource "aws_iam_instance_profile" "ec2-ssm-role-profile" {
# name = "${var.iam_prefix}-role-profile"
# role = aws_iam_role.ec2-ssm-role-for-mgt.name
# }
# resource "aws_iam_role" "ec2-ssm-role-for-mgt" {
# name = "${var.iam_prefix}-role"
# assume_role_policy = <<EOF
# {
# "Version": "2012-10-17",
# "Statement": [
# {
# "Effect": "Allow",
# "Principal": {
# "Service": "ec2.amazonaws.com"
# },
# "Action": "sts:AssumeRole"
# }
# ]
# }
# EOF
# }
# resource "aws_iam_role_policy_attachment" "ec2-ssm-role-policy" {
# role = aws_iam_role.ec2-ssm-role-for-mgt.name
# policy_arn = aws_iam_policy.ec2_s3_policy.arn
# }
# resource "aws_iam_policy_attachment" "ec2_securityhub_policy_attach" {
# name = "ec2-securityhub-policy-attachment"
# roles = [aws_iam_role.ec2-ssm-role-for-mgt.name]
# policy_arn = aws_iam_policy.ec2_securityhub_policy.arn
# }
resource "aws_iam_policy_attachment" "lambda_ssm_policy_attach" {
name = "s3x-ssm-policy-attachment"
roles = [aws_iam_role.iam_for_lambda.name]
policy_arn = aws_iam_policy.lambda_ssm_policy.arn
}
resource "aws_iam_policy_attachment" "lambda_exec_policy_attach" {
name = "s3x-lambda-exec-policy-attachment"
roles = [aws_iam_role.iam_for_lambda.name]
policy_arn = aws_iam_policy.lambda_execution_policy.arn
}
resource "aws_iam_policy_attachment" "lambda_s3_policy_attach" {
name = "s3x-lambda-s3-policy-attachment"
roles = [aws_iam_role.iam_for_lambda.name]
policy_arn = aws_iam_policy.lambda_s3_policy.arn
}
resource "aws_iam_policy_attachment" "lambda_securityhub_policy_attach" {
name = "s3x-lambda-securityhub-policy-attachment"
roles = [aws_iam_role.iam_for_lambda.name]
policy_arn = aws_iam_policy.lambda_securityhub_policy.arn
}