Guest User

Untitled

a guest
Aug 12th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. # variables.tf
  2. variable "prefix" {
  3. description = "Prefix for all resource names"
  4. type = string
  5. }
  6.  
  7. variable "databricks_account_id" {
  8. description = "Databricks account ID for Unity Catalog"
  9. type = string
  10. }
  11.  
  12. variable "aws_account_id" {
  13. description = "AWS account ID where resources will be created"
  14. type = string
  15. }
  16.  
  17. variable "storage_credential_name" {
  18. description = "Name for the Databricks storage credential"
  19. type = string
  20. default = "storage-credential"
  21. }
  22.  
  23. variable "s3_bucket_name" {
  24. description = "S3 bucket name for external location"
  25. type = string
  26. }
  27.  
  28. variable "tags" {
  29. description = "Tags to apply to all resources"
  30. type = map(string)
  31. default = {}
  32. }
  33.  
  34. variable "iam_propagation_delay" {
  35. description = "Time to wait for IAM changes to propagate"
  36. type = string
  37. default = "30s"
  38. }
  39.  
  40. variable "credential_propagation_delay" {
  41. description = "Time to wait for credential creation to complete"
  42. type = string
  43. default = "20s"
  44. }
  45.  
  46. # Local values for commonly used references
  47. locals {
  48. databricks_master_role_arn = "arn:aws:iam::414351767826:role/unity-catalog-prod-UCMasterRole-14S5ZJVKOTYTL"
  49. unity_catalog_role_name = "${var.prefix}-unity-catalog-access-role"
  50. unity_catalog_policy_name = "${var.prefix}-unity-catalog-access-policy"
  51. external_location_name = "${var.s3_bucket_name}-external-location"
  52. }
  53.  
  54. # main.tf
  55. # IAM role for Unity Catalog data access
  56. resource "aws_iam_role" "unity_catalog_access_role" {
  57. name = local.unity_catalog_role_name
  58.  
  59. assume_role_policy = jsonencode({
  60. Version = "2012-10-17"
  61. Statement = [
  62. # Allow Databricks master role to assume this role from the Databricks Customer AWS Account
  63. {
  64. Sid = "AllowDatabricksMasterRole"
  65. Effect = "Allow"
  66. Principal = {
  67. AWS = local.databricks_master_role_arn
  68. }
  69. Action = "sts:AssumeRole"
  70. Condition = {
  71. StringEquals = {
  72. "sts:ExternalId" = var.databricks_account_id
  73. }
  74. }
  75. },
  76. # Allow self-assumption for role chaining
  77. {
  78. Sid = "AllowSelfAssumption"
  79. Effect = "Allow"
  80. Principal = {
  81. AWS = "arn:aws:iam::${var.aws_account_id}:root"
  82. }
  83. Action = "sts:AssumeRole"
  84. Condition = {
  85. ArnEquals = {
  86. "aws:PrincipalArn" = "arn:aws:iam::${var.aws_account_id}:role/${local.unity_catalog_role_name}"
  87. }
  88. }
  89. }
  90. ]
  91. })
  92.  
  93. tags = merge(var.tags, {
  94. Name = "${var.prefix}-unity-catalog-iam-role"
  95. Environment = var.prefix
  96. Purpose = "Unity Catalog Data Access"
  97. })
  98. }
  99.  
  100. # IAM policy for S3 bucket access
  101. resource "aws_iam_policy" "unity_catalog_s3_access_policy" {
  102. name = local.unity_catalog_policy_name
  103. description = "Policy for Unity Catalog to access S3 bucket ${var.s3_bucket_name}"
  104.  
  105. policy = jsonencode({
  106. Version = "2012-10-17"
  107. Statement = [
  108. {
  109. Sid = "S3BucketAccess"
  110. Effect = "Allow"
  111. Action = [
  112. "s3:GetObject",
  113. "s3:PutObject",
  114. "s3:DeleteObject",
  115. "s3:ListBucket",
  116. "s3:GetBucketLocation",
  117. "s3:GetBucketPolicy",
  118. "s3:ListBucketMultipartUploads",
  119. "s3:GetObjectVersion"
  120. ]
  121. Resource = [
  122. "arn:aws:s3:::${var.s3_bucket_name}",
  123. "arn:aws:s3:::${var.s3_bucket_name}/*"
  124. ]
  125. },
  126. {
  127. Sid = "AllowRoleAssumption"
  128. Effect = "Allow"
  129. Action = "sts:AssumeRole"
  130. Resource = aws_iam_role.unity_catalog_access_role.arn
  131. }
  132. ]
  133. })
  134.  
  135. tags = merge(var.tags, {
  136. Name = "${var.prefix}-unity-catalog-iam-policy"
  137. Environment = var.prefix
  138. Purpose = "Unity Catalog S3 Access"
  139. })
  140. }
  141.  
  142. # Attach the policy to the role
  143. resource "aws_iam_role_policy_attachment" "unity_catalog_policy_attachment" {
  144. role = aws_iam_role.unity_catalog_access_role.name
  145. policy_arn = aws_iam_policy.unity_catalog_s3_access_policy.arn
  146. }
  147.  
  148. # Wait for IAM resources to propagate
  149. resource "time_sleep" "wait_for_iam_propagation" {
  150. create_duration = var.iam_propagation_delay
  151.  
  152. depends_on = [
  153. aws_iam_role_policy_attachment.unity_catalog_policy_attachment
  154. ]
  155.  
  156. triggers = {
  157. role_arn = aws_iam_role.unity_catalog_access_role.arn
  158. policy_arn = aws_iam_policy.unity_catalog_s3_access_policy.arn
  159. }
  160. }
  161.  
  162. # Databricks storage credential
  163. resource "databricks_storage_credential" "unity_catalog_credential" {
  164. name = "${var.prefix}-${var.storage_credential_name}"
  165.  
  166. aws_iam_role {
  167. role_arn = aws_iam_role.unity_catalog_access_role.arn
  168. }
  169.  
  170. depends_on = [
  171. time_sleep.wait_for_iam_propagation
  172. ]
  173. }
  174.  
  175. # Wait for credential creation to complete
  176. resource "time_sleep" "wait_for_credential_propagation" {
  177. create_duration = var.credential_propagation_delay
  178.  
  179. depends_on = [
  180. databricks_storage_credential.unity_catalog_credential
  181. ]
  182.  
  183. triggers = {
  184. credential_id = databricks_storage_credential.unity_catalog_credential.id
  185. }
  186. }
  187.  
  188. # Databricks external location
  189. resource "databricks_external_location" "s3_external_location" {
  190. name = local.external_location_name
  191. url = "s3://${var.s3_bucket_name}"
  192. credential_name = databricks_storage_credential.unity_catalog_credential.name
  193.  
  194. comment = "External location for bucket ${var.s3_bucket_name}"
  195.  
  196. depends_on = [
  197. time_sleep.wait_for_credential_propagation
  198. ]
  199. }
  200.  
  201. # outputs.tf
  202. output "unity_catalog_role_arn" {
  203. description = "ARN of the Unity Catalog IAM role"
  204. value = aws_iam_role.unity_catalog_access_role.arn
  205. }
  206.  
  207. output "unity_catalog_policy_arn" {
  208. description = "ARN of the Unity Catalog IAM policy"
  209. value = aws_iam_policy.unity_catalog_s3_access_policy.arn
  210. }
  211.  
  212. output "storage_credential_id" {
  213. description = "ID of the Databricks storage credential"
  214. value = databricks_storage_credential.unity_catalog_credential.id
  215. }
  216.  
  217. output "external_location_id" {
  218. description = "ID of the Databricks external location"
  219. value = databricks_external_location.s3_external_location.id
  220. }
  221.  
  222. output "external_location_url" {
  223. description = "URL of the external location"
  224. value = databricks_external_location.s3_external_location.url
  225. }
  226.  
Advertisement
Add Comment
Please, Sign In to add comment