Last active
March 31, 2025 06:49
-
-
Save darkn3rd/4c19911c5bafc5692b1e03cc0b109fa9 to your computer and use it in GitHub Desktop.
Export Deployed TeamCity on Google Cloud to Terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################## | |
# Install Terraform with tfenv | |
# Ref: https://github.com/GoogleCloudPlatform/terraformer#installation | |
# For macOS with Homebrew: brew install tfenv | |
############################# | |
git clone https://github.com/tfutils/tfenv.git ~/.tfenv | |
export PATH="$HOME/.tfenv/bin:$PATH" | |
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc | |
# install Terraform | |
tfenv install latest | |
################## | |
# Install Terraformer | |
# Ref: https://github.com/GoogleCloudPlatform/terraformer#installation | |
# For macOS with Homebrew: brew install terraformer | |
############################# | |
PROVIDER="google" | |
KERNAL=$(uname --kernel-name | perl -ne 'print lc') | |
LATEST=$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest \ | |
| grep tag_name \ | |
| cut -d '"' -f 4 | |
) | |
BINARY="terraformer-$PROVIDER-$KERNAL-amd64" | |
curl -sLO https://github.com/GoogleCloudPlatform/terraformer/releases/download/$LATEST/$BINARY | |
chmod +x $BINARY && sudo mv $BINARY /usr/local/bin/terraformer | |
################## | |
# Setup Terraform environment | |
############################# | |
cat <<-EOF > versions.tf | |
terraform { | |
required_providers { | |
google = { | |
source = "hashicorp/google" | |
} | |
} | |
required_version = ">= 0.13" | |
} | |
EOF | |
terraform init | |
################## | |
# Download and install Terraform plugin | |
############################# | |
mkdir -p ~/.terraform.d/plugins/${KERNAL}_amd64 | |
VERSION=$(curl -sL https://releases.hashicorp.com/terraform-provider-google/ \ | |
| awk -F'<|_' '/terraform-provider-google/{ print $3 }' \ | |
| sort --version-sort \ | |
| tail -1 | |
) | |
curl -sOL https://releases.hashicorp.com/terraform-provider-google/${VERSION}/terraform-provider-google_${VERSION}_${KERNAL}_amd64.zip | |
unzip -d ~/.terraform.d/plugins/${KERNAL}_amd64 \ | |
terraform-provider-google_${VERSION}_${KERNAL}_amd64.zip | |
################## | |
# Create Credentials | |
# by using existing TeamCity Service Account | |
############################# | |
PROJECT_ID="my-cicd-project" # change this to desired project name | |
REGION="us-central1" | |
# https://cloud.google.com/docs/authentication/production | |
gcloud iam service-accounts keys create "credentials.json" \ | |
--iam-account tc-server-account@$PROJECT_ID.iam.gserviceaccount.com | |
export GOOGLE_APPLICATION_CREDENTIALS="$PWD/credentials.json" | |
################## | |
# Run Terraformer | |
############################# | |
terraformer import google \ | |
--resources=cloudsql,networks,firewall,iam,gcs,disks,addresses,images,instances \ | |
--regions=$REGION \ | |
--projects=$PROJECT_ID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment