Skip to content

Commit

Permalink
Merge pull request #57 from imp14a/fix/automation-docs
Browse files Browse the repository at this point in the history
terraform deployment docs
  • Loading branch information
leiterenato authored May 3, 2024
2 parents 1e67c1a + 1c2733f commit 3820284
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion backend_apis/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ FROM python:3.11-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
ENV GOOGLE_CLOUD_PROJECT blackbelt-cloudai-gdm
ENV GOOGLE_CLOUD_PROJECT <your_project_id>

WORKDIR /
COPY ./requirements.txt /requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion installation_scripts/backend_deployment.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

set -e

REGION=$1
RUN_SA=$2

cp templates/config.toml ../backend_apis/app
cp templates/Dockerfile ../backend_apis/
cd ../backend_apis
gcloud run deploy genai-for-marketing --source . --allow-unauthenticated --region="${REGION}" --service-account="${RUN_SA}"
55 changes: 49 additions & 6 deletions tf/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Terraform deployment
Terraform deployment simpliefies the deployment of this solution and can be used as blueprint of the solution, it includes all requirements in order to deploy in a single terraform scripts the.

*Note*: Current version of the Terraform Google Cloud provider has not been updated to generate some of the GenAI resources, this soultion uses null_resource to create some resources using Google Cloud SDK.

## Prerequisites:

Previous to execute terraform you need to enable some services using the following steps:

### Enable Firebase
Will be used for the frontend deployment

1. Go to https://console.firebase.google.com/
2. Select add project and load your Google Cloud Platform project
Expand All @@ -11,23 +17,60 @@
5. Continue and complete

### Enable Vertex AI Agent Builder
Required before start using App Builder services
1. Go to https://console.cloud.google.com/gen-app-builder/start
2. Accept TOS


## Terraform deployment
```
export USER_PROJECT_OVERRIDE=true
export GOOGLE_BILLING_PROJECT=<your_project_id>

From [Cloud Shell](https://cloud.google.com/shell/docs/using-cloud-shell) run the following commands:

*Note*: This deployment requires Terraform 1.7 or higher

```sh
# move to the tf folder
cd tf/

export USER_PROJECT_OVERRIDE=true
export GOOGLE_BILLING_PROJECT=<your_project_id>

terraform init
terraform apply -var=project_id=<your_project_id>
```

## After Terraform deployment
1. Go to https://firebase.corp.google.com/project/genai-marketing-tf-test/authentication/providers
### Terraform variables
You can change any of the default variables values in [variables.tf](variables.tf).

This terraform will generate all configurations files required in the frontend and backend_apis you need to change [variables.tf](variables.tf) values in order to change configuration if needed.

### After Terraform deployment
You need to enable at least one authentication provider in Firebase, you can enable it using the following steps:
1. Go to https://firebase.corp.google.com/project/your_project_id/authentication/providers (change the your_project_id value)
2. Select Google and enable it
3. Set the name for the project and support email for project
4. Save
4. Save

## Check your deployment
Once deployment is completed terraform will output relevants resoruces values.

Resulting example outputs:
```sh
backend_deployment = "https://genai-for-marketing-xxxxxxxx.a.run.app"
backend_service_account = "[email protected]"
frontend_deployment = "https://your-project-id.web.app"
```
You can use the app by accessing to the frontend_deployment URL.

### Deployed resources
This deployment creates all the resources described in the main [README.md](../README.md) file, the following is a list of the created resources:
- Required Google Cloud services
- [BiqQuery](https://console.cloud.google.com/bigquery) Dataset and tables (populating tables with sample data)
- Google Drive folder and templates files
- [Service Account](https://console.cloud.google.com/iam-admin) with the required permissions
- [Search engine and Chat engine](https://console.cloud.google.com/gen-app-builder) with datastores
- [Cloud Run](https://console.cloud.google.com/run) for backend APIs
- Firebase for frontend deployment

### Configuration files
This deployment uses the tempaltes in the [templates/](templates/) diractory to replace all necessary configuration values for the application. After the deployment is complete, you can review the resulting values in the config.tomal and enviroments.ts files.
15 changes: 12 additions & 3 deletions tf/app.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

resource "google_artifact_registry_repository" "docker-repo" {
project = var.project_id
location = "us-central1"
Expand Down Expand Up @@ -50,7 +50,7 @@ resource "null_resource" "backend_deployment" {
command = "sh aux_data/backend_deployment.sh ${var.region} ${module.genai_run_service_account.email}"
}

depends_on = [google_artifact_registry_repository.docker-repo, local_file.config_toml]
depends_on = [google_artifact_registry_repository.docker-repo, local_file.config_toml, local_file.dockerfile]
}

data "google_cloud_run_service" "run_service" {
Expand Down Expand Up @@ -105,6 +105,15 @@ resource "local_file" "config_toml" {
filename = "${path.module}/templates/config.toml"
}


resource "local_file" "dockerfile" {
content = templatefile("${path.module}/templates/Dockerfile.tftpl", {
project_id = var.project_id
}
)
filename = "${path.module}/templates/Dockerfile"
}

resource "local_file" "enviroments_ts" {
content = templatefile("${path.module}/templates/environments.ts.tftpl", {
run_service_url = data.google_cloud_run_service.run_service.status[0].url,
Expand Down Expand Up @@ -137,4 +146,4 @@ resource "null_resource" "frontend_deployment" {
}

depends_on = [google_artifact_registry_repository.docker-repo, local_file.enviroments_ts]
}
}
2 changes: 1 addition & 1 deletion tf/gdrive.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ resource "null_resource" "gdrive_folder" {
provisioner "local-exec" {
command = "[ ! -e create_gdrive_folder_results.json ] && echo \"{}\" >> create_gdrive_folder_results.json"
}

provisioner "local-exec" {
command = "source venv/bin/activate; python3 aux_data/Create_GDrive_folder.py --folder_name=\"${var.gdrive_folder_name}\" --service_account_email=\"${module.genai_run_service_account.email}\""
}
Expand Down
2 changes: 1 addition & 1 deletion tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module "project-services" {
project_id = var.project_id
enable_apis = true

activate_apis = local.services
activate_apis = local.services
disable_services_on_destroy = false
}

Expand Down
28 changes: 28 additions & 0 deletions tf/templates/Dockerfile.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


FROM python:3.11-slim

# Allow statements and log messages to immediately appear in the Knative logs
ENV PYTHONUNBUFFERED True
ENV GOOGLE_CLOUD_PROJECT ${project_id}

WORKDIR /
COPY ./requirements.txt /requirements.txt
RUN pip install --no-cache-dir --upgrade -r /requirements.txt

COPY ./app /app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]

0 comments on commit 3820284

Please sign in to comment.