forked from milliHQ/terraform-aws-next-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
87 lines (65 loc) · 1.98 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
######################
# IAM Role (λ Next.js)
######################
data "aws_iam_policy_document" "assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "lambda" {
for_each = local.lambdas
name = random_id.function_name[each.key].hex
description = "Managed by Terraform Next.js"
permissions_boundary = var.lambda_role_permissions_boundary
assume_role_policy = data.aws_iam_policy_document.assume_role.json
tags = var.tags
}
#############################
# Cloudwatch Logs (λ Next.js)
#############################
resource "aws_cloudwatch_log_group" "this" {
for_each = local.lambdas
name = "/aws/lambda/${random_id.function_name[each.key].hex}"
retention_in_days = 14
tags = var.tags
}
data "aws_iam_policy_document" "lambda_logging" {
statement {
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = [
"arn:aws:logs:*:*:*"
]
}
}
resource "aws_iam_policy" "lambda_logging" {
description = "Managed by Terraform Next.js"
policy = data.aws_iam_policy_document.lambda_logging.json
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
for_each = local.lambdas
role = aws_iam_role.lambda[each.key].name
policy_arn = aws_iam_policy.lambda_logging.arn
}
####################################
# Additional policy JSON (λ Next.js)
####################################
resource "aws_iam_policy" "additional_json" {
count = var.lambda_policy_json != null ? 1 : 0
description = "Managed by Terraform Next.js"
policy = var.lambda_policy_json
}
resource "aws_iam_role_policy_attachment" "additional_json" {
for_each = var.lambda_policy_json != null ? local.lambdas : {}
role = aws_iam_role.lambda[each.key].name
policy_arn = aws_iam_policy.additional_json[0].arn
}