-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-iam.tf
59 lines (56 loc) · 1.72 KB
/
aws-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
data "aws_iam_policy_document" "lambda_assume_role" {
count = length(var.endpoints) > 0 ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "iam_for_lambda" {
count = length(var.endpoints) > 0 ? 1 : 0
name = "cloudtrail-cn-role-${data.aws_region.current.name}"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role[0].json
tags = var.tags
}
resource "aws_iam_policy" "lambda_cw" {
count = length(var.endpoints) > 0 ? 1 : 0
name = "cloudtrail-cn-policy-${data.aws_region.current.name}"
path = "/"
description = "IAM policy for logging from a lambda"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action : [
"cloudwatch:GetMetricData",
"cloudwatch:ListMetrics",
"cloudwatch:DescribeAlarms",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeMetricFilters",
"logs:FilterLogEvents"
],
Resource : [aws_lambda_function.lambda[0].arn, "arn:aws:logs:*:*:*", "arn:aws:cloudwatch:*:*:*"]
Effect : "Allow"
},
{
Action : ["SNS:Publish"],
Resource : "arn:aws:sns:*:*:*",
Effect : "Allow"
},
{
Action : ["kms:Decrypt", "kms:GenerateDataKey*"],
Resource : "*",
Effect : "Allow"
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_cw" {
count = length(var.endpoints) > 0 ? 1 : 0
role = aws_iam_role.iam_for_lambda[0].name
policy_arn = aws_iam_policy.lambda_cw[0].arn
}