-
Notifications
You must be signed in to change notification settings - Fork 13
/
cognito_iam.tf
159 lines (142 loc) · 3.84 KB
/
cognito_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
# es access cognito
data "aws_iam_policy_document" "es_assume_policy" {
count = var.cognito_enabled ? 1 : 0
version = "2012-10-17"
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = ["es.amazonaws.com"]
}
actions = ["sts:AssumeRole"]
}
}
data "aws_iam_policy" "cognito_es_policy" {
count = var.cognito_enabled ? 1 : 0
name = "AmazonOpenSearchServiceCognitoAccess"
}
resource "aws_iam_role" "cognito_es_role" {
count = var.cognito_enabled ? 1 : 0
name = var.custom_es_cognito_role_name == null ? "${var.name}_AmazonOpenSearchServiceCognitoAccess" : var.custom_es_cognito_role_name
assume_role_policy = data.aws_iam_policy_document.es_assume_policy[0].json
}
resource "aws_iam_role_policy_attachment" "cognito_es_attach" {
count = var.cognito_enabled ? 1 : 0
role = aws_iam_role.cognito_es_role[0].name
policy_arn = data.aws_iam_policy.cognito_es_policy[0].arn
}
# authenticated role
resource "aws_iam_role" "authenticated" {
count = var.cognito_enabled ? 1 : 0
name = format("cognito_authenticated-%s", var.name)
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${aws_cognito_identity_pool.identity_pool[0].id}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated"
}
}
}
]
}
EOF
}
resource "aws_iam_role_policy" "authenticated" {
count = var.cognito_enabled ? 1 : 0
name = format("authenticated_policy-%s", var.name)
role = aws_iam_role.authenticated[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"cognito-identity:*"
],
"Resource": [
"*"
]
}
]
}
EOF
}
# unauthenticated role
resource "aws_iam_role" "unauthenticated" {
count = var.cognito_enabled ? 1 : 0
name = format("cognito_unauthenticated-%s", var.name)
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "${aws_cognito_identity_pool.identity_pool[0].id}"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated"
}
}
}
]
}
EOF
}
resource "aws_iam_role_policy" "unauthenticated" {
count = var.cognito_enabled ? 1 : 0
name = format("unauthenticated_policy-%s", var.name)
role = aws_iam_role.unauthenticated[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*"
],
"Resource": [
"*"
]
}
]
}
EOF
}
resource "aws_cognito_identity_pool_roles_attachment" "roles_attachment" {
count = var.cognito_enabled ? 1 : 0
identity_pool_id = aws_cognito_identity_pool.identity_pool[0].id
roles = {
"authenticated" = aws_iam_role.authenticated[0].arn,
"unauthenticated" = aws_iam_role.unauthenticated[0].arn,
}
dynamic "role_mapping" {
for_each = var.role_mapping
content {
ambiguous_role_resolution = try(role_mapping.value["ambiguous_role_resolution"], null)
identity_provider = try(role_mapping.value["identity_provider"], null)
type = try(role_mapping.value["type"], null)
}
}
}