We will be using Terraform to initialize the GCP environment. This article will take you step by step from creating a Service Account to GCS and saving Terraform State for version control.
Preparation
Installing Terraform
Please refer to the Official Terraform Documentation to install Terraform. MacOS can be installed using Homebrew.
|
|
Installing Google Cloud SDK
Please refer to the Google Cloud SDK official documentation to install Google Cloud SDK. MacOS can be installed using Homebrew.
|
|
Once the installation is complete, use the following command to log in to your Google Cloud account.
|
|
Create a Service Account on GCP
Make sure that the Google Cloud SDK is installed and set up, and that you are logged into your Google Cloud account. You can run the following commands to check if the gcloud tools are correctly installed.
Next, use the following command to create a service account.
Replace [SERVICE_ACCOUNT_NAME]
with the name of the service account you want to create and [DISPLAY_NAME]
with the appropriate display name.
After creating a service account, you can add the required roles/permissions to that account. For example, if you want to assign the service account as the project owner, run the following command.
Replace [PROJECT_ID]
with your project ID and [SERVICE_ACCOUNT_EMAIL]
with the email address of the service account you just created.
After completing the above steps, you have successfully created a service account using the gcloud command and assigned it the appropriate roles/permissions. Next, you can use the following command to create a service account key.
Replace [FILE_NAME]
with the name of the key file you want to create and [SERVICE_ACCOUNT_EMAIL]
with the email address of the service account you just created. Please keep this key file in a safe place because you will need to use it later.
Create Terraform Profile
Please refer to the Official Terraform Documentation to create a Terraform profile. You can use the following commands to initialize Terraform.
|
|
Next, please set project
, credentials
, region
, zone
in the main.tf
file as your GCP project information.
|
|
The credentials parameter is the key file of the service account you just created. Here you can use terraform.tfvars
to set it.
In addition to setting credentials
, you can also use the GOOGLE_APPLICATION_CREDENTIALS
environment variable to set the path to the service account key file.
|
|
Create GCP resources
Create the main.tf file.
|
|
Create VPC resources.
After completing the above steps, you can use the following commands to preview the resources that Terraform will create.
|
|
If there are no problems with the preview results, you can use the following commands to create resources.
|
|
Save Terraform State to GCS
After running the above steps, you will find a terraform.tfstate
file at the bottom of the directory. This file is used to record the status of resources managed by Terraform and the correlation between resources. If you want to save this file to GCS, you can use Terraform to create GCS-related resources.
|
|
Output the GCS bucket name.
The following results can be seen.
Open the main.tf file and add the following settings to the terraform main settings.
Do the terraform init
again and you will see the following result.
|
|
Enter yes
and you will see the results below.
|
|
This will allow you to save and version control the Terraform state through GCS. Finally, the following files can be deleted.
Delete GCP Resources
If you want to delete a resource you just created, you can use the following command.
|
|
For full example code, please refer to terraform-gcp-demo.
Ref
-
https://blog.wu-boy.com/2023/05/initialize-gcp-environment-using-terraform/