Skip to content

Commit e5354f2

Browse files
authored
Merge pull request #57 from datafold/gerard-eng-941-test-that-workload-identity-can-backup-clickhouse-data
feat: Use IRSA for backups
2 parents d386766 + 9b38821 commit e5354f2

File tree

7 files changed

+90
-64
lines changed

7 files changed

+90
-64
lines changed

main.tf

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,19 @@ locals {
156156
)
157157
}
158158

159+
module "clickhouse_backup" {
160+
source = "./modules/clickhouse_backup"
161+
162+
deployment_name = var.deployment_name
163+
clickhouse_s3_bucket = var.clickhouse_s3_bucket
164+
s3_clickhouse_backup_tags = var.s3_clickhouse_backup_tags
165+
s3_backup_bucket_name_override = var.s3_backup_bucket_name_override
166+
}
167+
168+
locals {
169+
clickhouse_backup_bucket_arn = module.clickhouse_backup.clickhouse_s3_bucket_arn
170+
}
171+
159172
module "eks" {
160173
source = "./modules/eks"
161174

@@ -181,11 +194,12 @@ module "eks" {
181194
k8s_public_access_cidrs = var.k8s_public_access_cidrs
182195

183196
k8s_access_bedrock = var.k8s_access_bedrock
197+
clickhouse_backup_bucket_arn = local.clickhouse_backup_bucket_arn
184198
}
185199

186200
locals {
187-
cluster_name = module.eks.cluster_name
188-
control_plane_sg_id = module.eks.control_plane_security_group_id
201+
cluster_name = module.eks.cluster_name
202+
control_plane_sg_id = module.eks.control_plane_security_group_id
189203
}
190204

191205
module "database" {
@@ -230,15 +244,6 @@ module "database" {
230244
rds_monitoring_interval = var.rds_monitoring_interval
231245
}
232246

233-
module "clickhouse_backup" {
234-
source = "./modules/clickhouse_backup"
235-
236-
deployment_name = var.deployment_name
237-
clickhouse_s3_bucket = var.clickhouse_s3_bucket
238-
s3_clickhouse_backup_tags = var.s3_clickhouse_backup_tags
239-
s3_backup_bucket_name_override = var.s3_backup_bucket_name_override
240-
}
241-
242247
module "private_access" {
243248
count = var.deploy_private_access ? 1 : 0
244249
source = "./modules/private_access"
@@ -263,7 +268,7 @@ resource "aws_ebs_volume" "clickhouse_data" {
263268

264269
tags = merge({
265270
Name = "${var.deployment_name}-clickhouse-data"
266-
}, var.ebs_extra_tags)
271+
}, var.ebs_extra_tags)
267272
}
268273

269274
resource "aws_ebs_volume" "clickhouse_logs" {

modules/clickhouse_backup/iam.tf

Lines changed: 0 additions & 34 deletions
This file was deleted.

modules/clickhouse_backup/outputs.tf

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ output "clickhouse_s3_bucket" {
22
value = resource.aws_s3_bucket.clickhouse_backup.id
33
}
44

5-
output "clickhouse_s3_region" {
6-
value = resource.aws_s3_bucket.clickhouse_backup.region
7-
}
8-
9-
output "clickhouse_access_key" {
10-
value = resource.aws_iam_access_key.clickhouse_backup.id
5+
output "clickhouse_s3_bucket_arn" {
6+
value = resource.aws_s3_bucket.clickhouse_backup.arn
117
}
128

13-
output "clickhouse_secret_key" {
14-
value = resource.aws_iam_access_key.clickhouse_backup.secret
9+
output "clickhouse_s3_region" {
10+
value = resource.aws_s3_bucket.clickhouse_backup.region
1511
}

modules/eks/outputs.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,10 @@ output "storage_worker_role_arn" {
136136
output "storage_worker_service_account_name" {
137137
value = var.storage_worker_service_account_name
138138
description = "The name of the service account for storage_worker"
139-
}
139+
}
140+
141+
# Clickhouse backup
142+
output "clickhouse_backup_role_name" {
143+
value = module.clickhouse_backup_role.iam_role_arn
144+
description = "The name of the role for clickhouse backups"
145+
}

modules/eks/roles.tf

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,37 @@ resource "aws_iam_policy" "bedrock_access_policy" {
2828
tags = var.sg_tags
2929
}
3030

31-
#
31+
resource "aws_iam_policy" "clickhouse_backup_policy" {
32+
name = "${var.deployment_name}-clickhouse-backup-policy"
33+
description = "Policy that allows clickhouse to make backups"
34+
35+
policy = jsonencode({
36+
Version = "2012-10-17",
37+
Statement = [
38+
{
39+
Effect = "Allow",
40+
Action = [
41+
"s3:ListBucket",
42+
],
43+
Resource = [var.clickhouse_backup_bucket_arn]
44+
},
45+
{
46+
Effect = "Allow",
47+
Action = [
48+
"s3:PutObject",
49+
"s3:GetObject",
50+
"s3:ListBucket",
51+
"s3:DeleteObject"
52+
],
53+
Resource = [
54+
"${var.clickhouse_backup_bucket_arn}/*"
55+
]
56+
}
57+
]
58+
})
59+
}
60+
61+
#
3262
# Roles
3363
#
3464

@@ -200,6 +230,18 @@ module "storage_worker_role" {
200230
}
201231
}
202232

233+
module "clickhouse_backup_role" {
234+
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
235+
role_name = "${var.deployment_name}-${var.clickhouse_backup_service_account_name}"
236+
237+
oidc_providers = {
238+
ex = {
239+
provider_arn = module.eks.oidc_provider_arn
240+
namespace_service_accounts = ["${var.deployment_name}:${var.clickhouse_backup_service_account_name}"]
241+
}
242+
}
243+
}
244+
203245
# Policy Attachments
204246
resource "aws_iam_role_policy_attachment" "bedrock_dfshell_attachment" {
205247
count = var.k8s_access_bedrock ? 1 : 0
@@ -225,3 +267,8 @@ resource "aws_iam_role_policy_attachment" "bedrock_worker_interactive_attachment
225267
policy_arn = aws_iam_policy.bedrock_access_policy[0].arn
226268
}
227269

270+
resource "aws_iam_role_policy_attachment" "clickhouse_backup_attachment" {
271+
role = module.clickhouse_backup_role.iam_role_name
272+
policy_arn = aws_iam_policy.clickhouse_backup_policy.arn
273+
}
274+

modules/eks/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ variable "sg_tags" {
118118
default = {}
119119
}
120120

121+
variable "clickhouse_backup_service_account_name" {
122+
type = string
123+
default = "datafold-clickhouse"
124+
description = "Name of the service account for clickhouse backup"
125+
}
126+
127+
variable "clickhouse_backup_bucket_arn" {
128+
type = string
129+
description = "ARN of the backup bucket"
130+
}
131+
121132
variable "dfshell_service_account_name" {
122133
type = string
123134
default = "datafold-dfshell"

outputs.tf

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,9 @@ output "clickhouse_s3_region" {
133133
description = "The region where the S3 bucket is created"
134134
}
135135

136-
output "clickhouse_access_key" {
137-
value = module.clickhouse_backup.clickhouse_access_key
138-
description = "The access key of the IAM user doing the clickhouse backups."
139-
}
140-
141-
output "clickhouse_secret_key" {
142-
value = module.clickhouse_backup.clickhouse_secret_key
143-
description = "The secret key of the IAM user doing the clickhouse backups."
136+
output "clickhouse_backup_role_name" {
137+
value = module.eks.clickhouse_backup_role_name
138+
description = "The name of the role for clickhouse backups"
144139
}
145140

146141
output "private_access_vpces_name" {

0 commit comments

Comments
 (0)