Skip to content

Commit

Permalink
Added script to create custom data commons docker artifact repo in gc…
Browse files Browse the repository at this point in the history
…p. Added terraform variables for adjusting CDC web service instance count. (datacommonsorg#4828)

* Added script to create custom data commons docker artifact repo in
gcp.
* Updated CDC terraform README.md
* Added terraform variables for adjusting CDC web service instance
count.
  • Loading branch information
dwnoble authored Jan 9, 2025
1 parent 81b9e96 commit bd458fa
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
44 changes: 42 additions & 2 deletions deploy/terraform-custom-datacommons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,48 @@ Generate credentials for Google Cloud:
gcloud auth application-default login --project $PROJECT_ID
gcloud auth login
```
### 4. (Optional) Provision a docker artifact registry and push custom Data Commons images

### 4. Initialize Terraform
An artifact registry can optionally be used to store custom Docker images for the Data Commons web service image.

Create a new artifact repository named `$PROJECT_ID-artifacts` in the `us-central1` region:

```bash
REGION=us-central1 # Or any other GCP region
./create_artifact_repository.sh $PROJECT_ID $REGION
```

Follow [these instructions to build custom Data Commons docker images](https://docs.datacommons.org/custom_dc/build_image.html).

For example, to build a custom Data Commons web service image:

```bash
docker build --platform linux/amd64 -f build/cdc_services/Dockerfile \
--tag custom-dc-services \
.
```

And follow [these instructions to push a new image to the artifact registry](https://cloud.google.com/artifact-registry/docs/docker/pushing-and-pulling#pushing_images_to_a_repository).

For example:

```bash
PROJECT_ID=your-gcp-project
docker tag custom-dc-services:latest \
us-central1-docker.pkg.dev/$PROJECT_ID/$PROJECT_ID-artifacts/custom-dc-services:latest
docker push \
us-central1-docker.pkg.dev/$PROJECT_ID/$PROJECT_ID-artifacts/custom-dc-services:latest
```

To use this custom image in your Terraform deployment, set the `dc_web_service_image` variable to `us-central1-docker.pkg.dev/your-gcp-project/your-gcp-project-artifacts/custom-dc-services:latest`.

Example:

```
dc_web_service_image = "us-central1-docker.pkg.dev/my-gcp-project/datcom-website-dev-artifacts/custom-dc-services:latest"
```

### 5. Initialize Terraform

```bash
cd modules
Expand Down Expand Up @@ -111,7 +151,7 @@ terraform init
terraform plan
```

### 5. Provision and run Data Commons in GCP
### 6. Provision and run Data Commons in GCP

Deploy custom Data Commons instance (takes about 15 minutes):

Expand Down
62 changes: 62 additions & 0 deletions deploy/terraform-custom-datacommons/create_artifact_repository.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash
# Copyright 2025 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
#
# http://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.

# One-time setup script for creating a GCR artifact repository

# Takes two arguments: the GCP project ID and optionally the region.
set -e

# Define color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Check if the GCP project ID is provided
if [ -z "$1" ]; then
echo -e "${RED}Error: GCP project ID is required.${NC}"
echo -e "${YELLOW}Usage: $0 <gcp-project-id>${NC}"
exit 1
fi

# First argument is the GCP project
PROJECT_ID=$1

# Check if region is provided, otherwise default to us-central1
if [ -z "$2" ]; then
REGION="us-central1"
else
REGION="$2"
fi

# Validate region format
if ! [[ $REGION =~ ^[a-z]+-[a-z0-9]+$ ]]; then
echo -e "${RED}Error: Invalid region format.${NC}"
echo -e "${YELLOW}Region should be in format like 'us-central1'${NC}"
exit 1
fi


# Enable the Artifact Registry API
gcloud services enable artifactregistry.googleapis.com --project $PROJECT_ID

# Create the artifact repository
gcloud artifacts repositories create $PROJECT_ID-artifacts \
--repository-format=docker \
--location=$REGION \
--description="Artifact repository for $PROJECT_ID"


echo -e "${GREEN}Artifact repository created successfully.${NC}"
4 changes: 2 additions & 2 deletions deploy/terraform-custom-datacommons/modules/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ resource "google_cloud_run_v2_service" "dc_web_service" {
egress = "PRIVATE_RANGES_ONLY"
}
scaling {
min_instance_count = 1
max_instance_count = 1
min_instance_count = var.dc_web_service_min_instance_count
max_instance_count = var.dc_web_service_max_instance_count
}
service_account = google_service_account.datacommons_service_account.email
}
Expand Down
12 changes: 12 additions & 0 deletions deploy/terraform-custom-datacommons/modules/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ variable "dc_web_service_image" {
default = "gcr.io/datcom-ci/datacommons-services:stable"
}

variable "dc_web_service_min_instance_count" {
description = "Minimum number of instances for the Data Commons service"
type = number
default = 1
}

variable "dc_web_service_max_instance_count" {
description = "Maximum number of instances for the Data Commons service"
type = number
default = 1
}

variable "dc_web_service_cpu" {
description = "CPU limit for the Data Commons service container"
type = string
Expand Down

0 comments on commit bd458fa

Please sign in to comment.