Jenish Jain

Export google cloud resources to terraform format

How to export your google cloud resources created using click OPs to terraform code for IAC

Jenish Jain
written by Jenish

Typically, during the initial stages of development, we often establish our operational infrastructure on Google Cloud through ClickOps. This setup currently serves as our production environment, but our goal is to transition it to Infrastructure as Code (IAC) in the future.

So how can one migrate from ClickOps to IAC for google cloud

Well we already have a command line utility for this, read more about it here

Not a fan of documentation? here are the steps to follow

1. Authenticate with GCP and configure the target project.

> gcloud auth login

> gcloud config set project PROJECT_ID

2. Install GCP's config-connector.

> gcloud components install config-connector

3. Now run the below command to start exporting your infrastructure, replace the output path and your project ID

> gcloud beta resource-config bulk-export \
 --path=OUTPUT_DIRECTORY_PATH \
 --project=PROJECT_ID \
 --resource-format=terraform

The export is arranged in the format:

OUTPUT_DIRECTORY/projects/PROJECT_ID/RESOURCE_TYPE

For most of you, we are done here 🎉 . But for others who want to optimize their export.

Nerd Alert

To get list of resources that can be exported using bulk-export command run list-resource-types

> gcloud beta resource-config list-resource-types

Now using the list you can selectively import only the required ones using --resource-types= flag

Resources