Skip to content

Commit ec601c2

Browse files
DH-5581 modularized terraform resources (Dataherald#490)
* DH-5581 modularized terraform resources
1 parent 680022c commit ec601c2

8 files changed

Lines changed: 236 additions & 125 deletions

File tree

.github/workflows/ai-deploy-branch-env.yml

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ env:
1818
SLACK_ICON: https://files.dataherald.com/random/github-actions.png
1919
SLACK_USERNAME: GitHub Actions
2020
SLACK_CHANNEL: eng-notifications
21+
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
22+
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
2123

2224
jobs:
2325
extract-branch-names:
@@ -78,13 +80,13 @@ jobs:
7880
target_env: branch
7981
image_tag: ${{ needs.extract-branch-names.outputs.branch_name}}-${{ github.sha }}
8082

81-
deploy-backend-dbs-infra:
82-
name: Deploy Backend and DBs infrastructure
83+
deploy-backend:
84+
name: Deploy Backend infrastructure
8385
needs: [extract-branch-names, build-engine, build-server]
8486
runs-on: ubuntu-latest
8587
defaults:
8688
run:
87-
working-directory: ./apps/ai/server/terraform
89+
working-directory: ./apps/ai/server/terraform/branch
8890
outputs:
8991
api_url: ${{ steps.get-api-url.outputs.api_url }}
9092
steps:
@@ -105,9 +107,12 @@ jobs:
105107
terraform_wrapper: false
106108
- name: Apply Terraform
107109
env:
108-
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
109110
TF_VAR_sha: ${{ github.sha }}
110111
TF_VAR_branch_name: ${{ needs.extract-branch-names.outputs.branch_name }}
112+
TF_VAR_vpc_id: ${{ vars.VPC_ID }}
113+
TF_VAR_subnet_1_id: ${{ vars.PRIVATE_SUBNET_1_ID }}
114+
TF_VAR_subnet_2_id: ${{ vars.PRIVATE_SUBNET_2_ID }}
115+
TF_VAR_ecs_security_group_id: ${{ vars.BRANCH_SECURITY_GROUP_ID }}
111116
TF_VAR_pinecone_index_name: ${{ needs.extract-branch-names.outputs.db_name }}
112117
TF_VAR_mongodb_uri: ${{ vars.EPHEMERAL_MONGODB_URI }}
113118
TF_VAR_mongodb_name: ${{ needs.extract-branch-names.outputs.db_name }}
@@ -116,8 +121,8 @@ jobs:
116121

117122
run: |
118123
terraform init -upgrade -backend-config="key=${{ needs.extract-branch-names.outputs.branch_name }}"
119-
terraform plan
120-
terraform apply -auto-approve
124+
terraform plan -target=module.aws_ecs
125+
terraform apply -target=module.aws_ecs -auto-approve
121126
- name: Get API URL
122127
id: get-api-url
123128
run: |
@@ -135,7 +140,36 @@ jobs:
135140
if [ -n "$UNTAGGED_SERVER_IMAGES" ] && [ "$UNTAGGED_SERVER_IMAGES" != "[]" ]; then
136141
aws ecr batch-delete-image --repository-name $ECR_SERVER_REPOSITORY --image-ids "$UNTAGGED_SERVER_IMAGES" --region us-east-1
137142
fi
138-
143+
create-vector-db:
144+
name: Create PineconeDB
145+
needs: [extract-branch-names]
146+
runs-on: ubuntu-latest
147+
defaults:
148+
run:
149+
working-directory: ./apps/ai/server/terraform/branch
150+
steps:
151+
- name: Check out code
152+
uses: actions/checkout@v4
153+
with:
154+
token: ${{ secrets.GH_ACCESS_TOKEN }}
155+
submodules: true
156+
- name: Configure AWS credentials
157+
uses: aws-actions/configure-aws-credentials@v4
158+
with:
159+
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
160+
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
161+
aws-region: ${{ vars.AWS_REGION }}
162+
- name: Setup Terraform
163+
uses: hashicorp/setup-terraform@v3
164+
with:
165+
terraform_wrapper: false
166+
- name: Apply Terraform
167+
env:
168+
TF_VAR_pinecone_index_name: ${{ needs.extract-branch-names.outputs.db_name }}
169+
run: |
170+
terraform init -upgrade -backend-config="key=${{ needs.extract-branch-names.outputs.branch_name }}"
171+
terraform plan -target=module.pinecone_index
172+
terraform apply -target=module.pinecone_index -auto-approve
139173
populate-db:
140174
name: Populate MongoDB
141175
needs: [extract-branch-names, start-self-hosted-runner]
@@ -201,7 +235,7 @@ jobs:
201235
extract-branch-names,
202236
start-self-hosted-runner,
203237
populate-db,
204-
deploy-backend-dbs-infra,
238+
create-vector-db,
205239
]
206240
runs-on: ${{ needs.start-self-hosted-runner.outputs.label }}
207241
defaults:
@@ -215,8 +249,6 @@ jobs:
215249
submodules: true
216250
- name: Populate PineconeDB
217251
env:
218-
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
219-
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
220252
PINECONE_INDEX_NAME: ${{ needs.extract-branch-names.outputs.db_name }}
221253
MONGODB_URI: ${{ vars.EPHEMERAL_MONGODB_URI }}
222254
MONGODB_USERNAME: ${{ secrets.MONGODB_USERNAME }}
@@ -231,7 +263,7 @@ jobs:
231263
232264
deploy-frontend:
233265
name: Deploy Frontend
234-
needs: [extract-branch-names, deploy-backend-dbs-infra]
266+
needs: [extract-branch-names, deploy-backend]
235267
runs-on: ubuntu-latest
236268
outputs:
237269
url: ${{ steps.console_dns.outputs.url }}
@@ -252,10 +284,10 @@ jobs:
252284
- name: Deploy to Vercel and set custom domain alias
253285
run: |
254286
echo Deploying to Vercel with the following environment variables:
255-
echo "NEXT_PUBLIC_API_URL=${{ needs.deploy-backend-dbs-infra.outputs.api_url }}"
287+
echo "NEXT_PUBLIC_API_URL=${{ needs.deploy-backend.outputs.api_url }}"
256288
echo "AUTH0_BASE_URL=${{ steps.console_dns.outputs.url }}"
257289
258-
vercel deploy --yes --build-env NEXT_PUBLIC_API_URL=${{ needs.deploy-backend-dbs-infra.outputs.api_url }} --env AUTH0_BASE_URL=${{ steps.console_dns.outputs.url }} --token=${{ secrets.VERCEL_ACCESS_TOKEN }} >deployment-url.txt 2>error.txt
290+
vercel deploy --yes --build-env NEXT_PUBLIC_API_URL=${{ needs.deploy-backend.outputs.api_url }} --env AUTH0_BASE_URL=${{ steps.console_dns.outputs.url }} --token=${{ secrets.VERCEL_ACCESS_TOKEN }} >deployment-url.txt 2>error.txt
259291
260292
# check the exit code
261293
code=$?
@@ -274,7 +306,7 @@ jobs:
274306
needs:
275307
[
276308
extract-branch-names,
277-
deploy-backend-dbs-infra,
309+
deploy-backend,
278310
populate-db,
279311
populate-vector-db,
280312
deploy-frontend,
@@ -299,12 +331,12 @@ jobs:
299331
needs:
300332
[
301333
extract-branch-names,
302-
deploy-backend-dbs-infra,
334+
deploy-backend,
303335
populate-db,
304336
populate-vector-db,
305337
deploy-frontend,
306338
]
307-
if: needs.deploy-backend-dbs-infra.result == 'failure' || needs.populate-db.result == 'failure' || needs.populate-vector-db.result == 'failure' || needs.deploy-frontend.result == 'failure'
339+
if: needs.deploy-backend.result == 'failure' || needs.populate-db.result == 'failure' || needs.populate-vector-db.result == 'failure' || needs.deploy-frontend.result == 'failure'
308340
runs-on: ubuntu-latest
309341
steps:
310342
- name: Send failed notification
@@ -324,12 +356,12 @@ jobs:
324356
needs:
325357
[
326358
extract-branch-names,
327-
deploy-backend-dbs-infra,
359+
deploy-backend,
328360
populate-db,
329361
populate-vector-db,
330362
deploy-frontend,
331363
]
332-
if: needs.deploy-backend-dbs-infra.result == 'success' && needs.populate-db.result == 'success' && needs.populate-vector-db.result == 'success' && needs.deploy-frontend.result == 'success'
364+
if: needs.deploy-backend.result == 'success' && needs.populate-db.result == 'success' && needs.populate-vector-db.result == 'success' && needs.deploy-frontend.result == 'success'
333365
runs-on: ubuntu-latest
334366
steps:
335367
- name: Send success notification
@@ -341,7 +373,7 @@ jobs:
341373
The branch environment deployment succeeded. Here are the details:
342374
343375
*Branch*: `${{ needs.extract-branch-names.outputs.branch }}`
344-
*API URL*: ${{ needs.deploy-backend-dbs-infra.outputs.api_url }}
376+
*API URL*: ${{ needs.deploy-backend.outputs.api_url }}
345377
*Console URL*: ${{ needs.deploy-frontend.outputs.url }}
346378
SLACK_COLOR: success
347379

.github/workflows/ai-destroy-branch-env.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }}
5353
defaults:
5454
run:
55-
working-directory: ./apps/ai/server
55+
working-directory: ./apps/ai/server/terraform/branch
5656

5757
steps:
5858
- name: Check out code
@@ -75,7 +75,6 @@ jobs:
7575
TF_VAR_mongodb_password: ${{ secrets.MONGODB_PASSWORD }}
7676
timeout-minutes: 3
7777
run: |
78-
cd terraform
7978
terraform init -upgrade -backend-config="key=${{ needs.extract-branch-name.outputs.branch_name }}"
8079
terraform destroy -auto-approve
8180
- name: Delete images

apps/ai/server/terraform/main.tf renamed to apps/ai/server/terraform/branch/aws_ecs/main.tf

Lines changed: 26 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,19 @@ terraform {
44
source = "aws"
55
version = "4.1.0"
66
}
7-
8-
pinecone = {
9-
source = "skyscrapr/pinecone"
10-
version = "0.5.1"
11-
}
12-
}
13-
backend "s3" {
14-
bucket = "terraform-states2"
15-
region = "us-east-1"
16-
}
17-
}
18-
19-
20-
provider "aws" {
21-
region = "us-east-1"
22-
}
23-
provider "pinecone" {}
24-
25-
variable "branch_name" { type= string }
26-
27-
variable "sha" { type= string }
28-
29-
variable "pinecone_index_name" {
30-
description = "pinecone_index_name"
31-
type = string
32-
}
33-
34-
variable "mongodb_uri" {
35-
description = "mongodb_uri"
36-
type = string
37-
}
38-
39-
variable "mongodb_name" {
40-
description = "mongodb_name"
41-
type = string
42-
}
43-
44-
variable "mongodb_username" {
45-
description = "mongodb_username"
46-
type = string
47-
}
48-
49-
variable "mongodb_password" {
50-
description = "mongodb_password"
51-
type = string
52-
}
53-
54-
variable "subnet_1_id" {
55-
description = "subnet_1_id"
56-
type = string
57-
default = "subnet-076afb4a159204349"
58-
}
59-
60-
variable "subnet_2_id" {
61-
description = "subnet_2_id"
62-
type = string
63-
default = "subnet-0b6b9dbf631131b09"
64-
}
65-
66-
variable "ecs_security_group_id" {
67-
description = "security_group_id"
68-
type = string
69-
default = "sg-07fac199a96aa3b65"
70-
}
71-
72-
resource "pinecone_index" "my_index" {
73-
name = var.pinecone_index_name
74-
dimension = 1536
75-
metric = "cosine"
76-
spec = {
77-
serverless = {
78-
cloud = "aws"
79-
region = "us-west-2"
80-
}
817
}
828
}
839

84-
locals {
85-
srv_connection_string = replace(var.mongodb_uri, "mongodb+srv://", "mongodb+srv://${var.mongodb_username}:${var.mongodb_password}@")
86-
}
87-
8810
resource "aws_ecs_task_definition" "my_task_definition" {
89-
family = "ai-backend-branch-${var.branch_name}"
90-
task_role_arn = "arn:aws:iam::422486916789:role/ecsk2TaskExecutionRole"
91-
execution_role_arn = "arn:aws:iam::422486916789:role/ecsk2TaskExecutionRole"
92-
network_mode = "awsvpc"
11+
family = "ai-backend-branch-${var.branch_name}"
12+
task_role_arn = "arn:aws:iam::422486916789:role/ecsk2TaskExecutionRole"
13+
execution_role_arn = "arn:aws:iam::422486916789:role/ecsk2TaskExecutionRole"
14+
network_mode = "awsvpc"
9315
requires_compatibilities = ["FARGATE"]
94-
cpu = "2048"
95-
memory = "4096"
96-
# container_definitions = file("task_definition.json")
97-
container_definitions =<<DEFINITION
16+
cpu = "2048"
17+
memory = "4096"
18+
# container_definitions = file("task_definition.json")
19+
container_definitions = <<DEFINITION
9820
[
9921
{
10022
"name": "ai-engine-branch",
@@ -128,11 +50,11 @@ resource "aws_ecs_task_definition" "my_task_definition" {
12850
},
12951
{
13052
"name": "MONGODB_URI",
131-
"value": "${local.srv_connection_string}"
53+
"value": "${var.mongodb_uri}"
13254
},
13355
{
13456
"name": "GOLDEN_SQL_COLLECTION",
135-
"value": "${var.pinecone_index_name}"
57+
"value": "${var.index_name}"
13658
}
13759
],
13860
"command": ["sh", "-c", "uvicorn dataherald.app:app --host 0.0.0.0 --port $CORE_PORT --log-config log_config.yml --log-level debug --reload"],
@@ -180,7 +102,7 @@ resource "aws_ecs_task_definition" "my_task_definition" {
180102
},
181103
{
182104
"name": "MONGODB_URI",
183-
"value": "${local.srv_connection_string}"
105+
"value": "${var.mongodb_uri}"
184106
}
185107
],
186108
"mountPoints": [],
@@ -200,19 +122,19 @@ resource "aws_ecs_task_definition" "my_task_definition" {
200122
}
201123

202124
resource "aws_lb" "my_load_balancer" {
203-
name = "${var.branch_name}"
125+
name = var.branch_name
204126
internal = false
205127
idle_timeout = 300
206128
load_balancer_type = "application"
207-
security_groups = ["sg-07fac199a96aa3b65"] # Replace with your security group ID
208-
subnets = ["subnet-076afb4a159204349", "subnet-0b6b9dbf631131b09"] # Replace with your subnet IDs
129+
security_groups = [var.ecs_security_group_id]
130+
subnets = [var.subnet_1_id, var.subnet_2_id]
209131
}
210132

211133
resource "aws_lb_target_group" "ecs_target_group" {
212-
name = "${var.branch_name}"
134+
name = var.branch_name
213135
port = 80
214136
protocol = "HTTP"
215-
vpc_id = "vpc-09c492a49b76fdf80"
137+
vpc_id = var.vpc_id
216138
target_type = "ip"
217139

218140
health_check {
@@ -240,7 +162,7 @@ resource "aws_lb_listener" "https_listener" {
240162
load_balancer_arn = aws_lb.my_load_balancer.arn
241163
port = 443
242164
protocol = "HTTPS"
243-
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" # Choose an appropriate SSL policy for your application
165+
ssl_policy = "ELBSecurityPolicy-TLS13-1-2-2021-06" # Choose an appropriate SSL policy for your application
244166

245167
default_action {
246168
type = "forward"
@@ -250,11 +172,11 @@ resource "aws_lb_listener" "https_listener" {
250172
}
251173

252174
resource "aws_ecs_service" "my_service" {
253-
name = "ai-backend-branch-${var.branch_name}"
254-
cluster = "arn:aws:ecs:us-east-1:422486916789:cluster/ai"
255-
task_definition = aws_ecs_task_definition.my_task_definition.arn
256-
desired_count = 1
257-
launch_type = "FARGATE"
175+
name = "ai-backend-branch-${var.branch_name}"
176+
cluster = "arn:aws:ecs:us-east-1:422486916789:cluster/ai"
177+
task_definition = aws_ecs_task_definition.my_task_definition.arn
178+
desired_count = 1
179+
launch_type = "FARGATE"
258180
enable_ecs_managed_tags = true
259181
wait_for_steady_state = true
260182

@@ -265,15 +187,15 @@ resource "aws_ecs_service" "my_service" {
265187
}
266188

267189
network_configuration {
268-
subnets = [var.subnet_1_id, var.subnet_2_id]
269-
security_groups = [var.ecs_security_group_id]
190+
subnets = [var.subnet_1_id, var.subnet_2_id]
191+
security_groups = [var.ecs_security_group_id]
270192
assign_public_ip = true
271193
}
272194
}
273195

274196
resource "aws_route53_record" "my_load_balancer_record" {
275-
zone_id = "Z07539241TW7P7NHVR11T" # Replace with your Route 53 hosted zone ID
276-
name = "${var.branch_name}.api.dataherald.ai" # Replace with the desired domain name
197+
zone_id = "Z07539241TW7P7NHVR11T" # Replace with your Route 53 hosted zone ID
198+
name = "${var.branch_name}.api.dataherald.ai" # Replace with the desired domain name
277199
type = "A"
278200
alias {
279201
name = aws_lb.my_load_balancer.dns_name

0 commit comments

Comments
 (0)