-
Notifications
You must be signed in to change notification settings - Fork 62
/
iam.tf
74 lines (64 loc) · 2.62 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
data "aws_partition" "current" {}
data "aws_iam_policy_document" "ab_role_assume_role_policy" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["backup.amazonaws.com"]
}
}
}
resource "aws_iam_role" "ab_role" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
name = var.iam_role_name == "" ? "aws-backup-plan-${var.plan_name}-role" : var.iam_role_name
assume_role_policy = data.aws_iam_policy_document.ab_role_assume_role_policy[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "ab_policy_attach" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
role = aws_iam_role.ab_role[0].name
}
resource "aws_iam_role_policy_attachment" "ab_backup_s3_policy_attach" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Backup"
role = aws_iam_role.ab_role[0].name
}
# Tag policy
data "aws_iam_policy_document" "ab_tag_policy_document" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
statement {
effect = "Allow"
resources = ["*"]
actions = [
"backup:ListTags",
"backup:TagResource",
"backup:UntagResource",
"tag:GetResources"
]
}
}
resource "aws_iam_policy" "ab_tag_policy" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
description = "AWS Backup Tag policy"
policy = data.aws_iam_policy_document.ab_tag_policy_document[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "ab_tag_policy_attach" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
policy_arn = aws_iam_policy.ab_tag_policy[0].arn
role = aws_iam_role.ab_role[0].name
}
# Restores policy
resource "aws_iam_role_policy_attachment" "ab_restores_policy_attach" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForRestores"
role = aws_iam_role.ab_role[0].name
}
resource "aws_iam_role_policy_attachment" "ab_restores_s3_policy_attach" {
count = var.enabled && var.iam_role_arn == null ? 1 : 0
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSBackupServiceRolePolicyForS3Restore"
role = aws_iam_role.ab_role[0].name
}