[[2021-06-26_Sat]] #terraform #gcp
`gcloud beta resource-config bulk-export --resource-format=terraform` コマンドにより、GCPの設定をterraformのコードへexportできる。
[gcloud beta resource-config bulk-export | Cloud SDK のドキュメント](https://cloud.google.com/sdk/gcloud/reference/beta/resource-config/bulk-export?hl=ja)
```shell-session
$
$ gcloud beta resource-config bulk-export --on-error=continue --resource-format=terraform --project $PROJECT_ID --path ./deploy/terraform/
Pausing command execution:
This command requires the `config-connector` binary to be installed to
export GCP resource configurations. Would you like to install
the`config-connector` binary to continue command execution? (Y/n)? y
ERROR: (gcloud.beta.resource-config.bulk-export)
You cannot perform this action because the Cloud SDK component manager
is disabled for this installation. You can run the following command
to achieve the same result for this installation:
sudo apt-get install google-cloud-sdk-config-connector
```
`sudo apt-get install google-cloud-sdk-config-connector`でインストールした。
```shell-session
$ gcloud beta resource-config bulk-export --on-error=continue --resource-format=terraform --project $PROJECT_ID --path ./deploy/terraform/
Exporting resource configurations to [./deploy/terraform/]...done.
ERROR: (gcloud.beta.resource-config.bulk-export) Error executing export:: [error in 'config-connector' version '1.53.0': error creating temporary bucket and prefix: error getting project id: error retrieving gcp sdk credentials: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
]
```
上記のサービスアカウントのCredentialsの設定をした。
```shell-session
$ gcloud beta resource-config bulk-export --on-error=continue --resource-format=terraform --project $PROJECT_ID --path ./deploy/terraform/
Exporting resource configurations to [./deploy/terraform/]...done.
Exported 156 resource configuration(s) to [./deploy/terraform/].
```