-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathuc_cross_account_role.tf
109 lines (103 loc) · 2.85 KB
/
uc_cross_account_role.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
data "aws_iam_policy_document" "passrole_for_uc" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
identifiers = [
"arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"
]
type = "AWS"
}
condition {
test = "StringEquals"
variable = "sts:ExternalId"
values = [databricks_metastore_data_access.this.aws_iam_role.0.external_id]
}
}
statement {
sid = "ExplicitSelfRoleAssumption"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${var.aws_account_id}:root"]
}
condition {
test = "ArnLike"
variable = "aws:PrincipalArn"
values = [local.iam_role_arn]
}
}
}
resource "aws_iam_policy" "unity_metastore" {
name = "${var.prefix}-unity-catalog-metastore-access-iam-policy"
policy = jsonencode({
Version = "2012-10-17"
Id = "${var.prefix}-databricks-unity-metastore"
Statement = [
{
"Action" : [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource" : [
aws_s3_bucket.metastore.arn,
"${aws_s3_bucket.metastore.arn}/*"
],
"Effect" : "Allow"
},
{
"Action" : [
"sts:AssumeRole"
],
"Resource" : [local.iam_role_arn],
"Effect" : "Allow"
}
]
})
tags = merge(var.tags, {
Name = "${local.iam_role_name} IAM policy"
})
}
// Required, in case https://docs.databricks.com/data/databricks-datasets.html are needed
resource "aws_iam_policy" "sample_data" {
name = "${var.prefix}-unity-catalog-sample-data-access"
policy = jsonencode({
Version = "2012-10-17"
Id = "${var.prefix}-databricks-sample-data"
Statement = [
{
"Action" : [
"s3:GetObject",
"s3:GetObjectVersion",
"s3:ListBucket",
"s3:GetBucketLocation"
],
"Resource" : [
"arn:aws:s3:::databricks-datasets-oregon/*",
"arn:aws:s3:::databricks-datasets-oregon"
],
"Effect" : "Allow"
}
]
})
tags = merge(var.tags, {
Name = "${var.prefix}-unity-catalog IAM policy"
})
}
resource "aws_iam_role" "metastore_data_access" {
name = local.iam_role_name
assume_role_policy = data.aws_iam_policy_document.passrole_for_uc.json
managed_policy_arns = [aws_iam_policy.unity_metastore.arn, aws_iam_policy.sample_data.arn]
tags = merge(var.tags, {
Name = "${var.prefix}-unity-catalog IAM role"
})
}
# Sleeping for 20s to wait for the workspace to enable identity federation
resource "time_sleep" "wait_role_creation" {
depends_on = [aws_iam_role.metastore_data_access]
create_duration = "20s"
}