Skip to content

Commit a71f85f

Browse files
authored
[Fix] Invert creation of storage credential and metastore (#143)
* Invert creation of storage credential and metastore * Update example * small tweak * add sleep
1 parent 3134192 commit a71f85f

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Module aws-databricks-unity-catalog
2+
3+
## Description
4+
5+
This module creates a Databricks Unity Catalog Metastore and Storage Credential for said metastore. It automatically creates an IAM role suitable for Unity Catalog and attaches it to the newly created metastore.
6+
7+
## Required Providers
8+
9+
- `databricks` - only account-level providers are supported.
10+
- `aws`

modules/aws-databricks-unity-catalog/main.tf

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
locals {
2+
iam_role_name = "${var.prefix}-unity-catalog-metastore-access"
3+
iam_role_arn = "arn:aws:iam::${var.aws_account_id}:role/${local.iam_role_name}"
4+
}
5+
16
resource "databricks_metastore" "this" {
27
name = local.metastore_name
38
region = var.region
@@ -8,23 +13,11 @@ resource "databricks_metastore" "this" {
813

914
resource "databricks_metastore_data_access" "this" {
1015
metastore_id = databricks_metastore.this.id
11-
name = aws_iam_role.metastore_data_access.name
16+
name = local.iam_role_name
1217
aws_iam_role {
13-
role_arn = aws_iam_role.metastore_data_access.arn
18+
role_arn = local.iam_role_arn
1419
}
1520
is_default = true
16-
depends_on = [
17-
resource.time_sleep.wait_role_creation
18-
]
19-
}
20-
21-
# Sleeping for 20s to wait for the workspace to enable identity federation
22-
resource "time_sleep" "wait_role_creation" {
23-
depends_on = [
24-
resource.aws_iam_role.metastore_data_access,
25-
resource.databricks_metastore.this
26-
]
27-
create_duration = "20s"
2821
}
2922

3023
resource "databricks_metastore_assignment" "default_metastore" {

modules/aws-databricks-unity-catalog/uc_cross_account_role.tf

+12-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ data "aws_iam_policy_document" "passrole_for_uc" {
1111
condition {
1212
test = "StringEquals"
1313
variable = "sts:ExternalId"
14-
values = [var.databricks_account_id]
14+
values = [databricks_metastore_data_access.this.aws_iam_role.0.external_id]
1515
}
1616
}
17+
1718
statement {
1819
sid = "ExplicitSelfRoleAssumption"
1920
effect = "Allow"
@@ -25,7 +26,7 @@ data "aws_iam_policy_document" "passrole_for_uc" {
2526
condition {
2627
test = "ArnLike"
2728
variable = "aws:PrincipalArn"
28-
values = ["arn:aws:iam::${var.aws_account_id}:role/${var.prefix}-uc-access"]
29+
values = [local.iam_role_arn]
2930
}
3031
}
3132
}
@@ -54,15 +55,13 @@ resource "aws_iam_policy" "unity_metastore" {
5455
"Action" : [
5556
"sts:AssumeRole"
5657
],
57-
"Resource" : [
58-
"arn:aws:iam::${var.aws_account_id}:role/${var.prefix}-uc-access"
59-
],
58+
"Resource" : [local.iam_role_arn],
6059
"Effect" : "Allow"
6160
}
6261
]
6362
})
6463
tags = merge(var.tags, {
65-
Name = "${var.prefix}-unity-catalog IAM policy"
64+
Name = "${local.iam_role_name} IAM policy"
6665
})
6766
}
6867

@@ -95,10 +94,16 @@ resource "aws_iam_policy" "sample_data" {
9594
}
9695

9796
resource "aws_iam_role" "metastore_data_access" {
98-
name = "${var.prefix}-uc-access"
97+
name = local.iam_role_name
9998
assume_role_policy = data.aws_iam_policy_document.passrole_for_uc.json
10099
managed_policy_arns = [aws_iam_policy.unity_metastore.arn, aws_iam_policy.sample_data.arn]
101100
tags = merge(var.tags, {
102101
Name = "${var.prefix}-unity-catalog IAM role"
103102
})
104103
}
104+
105+
# Sleeping for 20s to wait for the workspace to enable identity federation
106+
resource "time_sleep" "wait_role_creation" {
107+
depends_on = [aws_iam_role.metastore_data_access]
108+
create_duration = "20s"
109+
}

modules/aws-databricks-unity-catalog/variables.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ variable "tags" {
66

77
variable "prefix" {
88
type = string
9-
description = "(Optional) Prefix to name the resources created by this module"
9+
description = "(Required) Prefix to name the resources created by this module"
1010
}
1111

1212
variable "region" {

0 commit comments

Comments
 (0)