Terraform + GCP || Part 1
Let’s start the process by understanding some basics of Terraform.
Terraform configuration is as follows:
1. Providers: List the cloud service providers along with any necessary setup.
2. Resources: List the elements of the infrastructure that need to be built or maintained.
Installing Terraform
1. Download Terraform:
— Visit the Terraform download page.
— Download the appropriate package for your operating system.
2. Install Terraform:
– Windows:
– Downloaded package time.
– Move the “terraform.exe” file to the repertoire contained in the system’s “path”. — Mac OS and Linux:
-Understand the downloaded package.
– Move the binary “Terraform” to a different directory contained in “/usr/local/bin” or “path”. 3. Verify Installation:
— Open a terminal or command prompt. — Start “Terraform -version” to ensure that Terraform is installed correctly.
Now, let’s create a Cloud Storage Bucket with Terraform.
- Make a folder for this example
mkdir cloud-bucket
cd cloud-bucket
- Create a terraform file “ main.tf”:
provider "google" {
project = ""
region = "us-east2"
}# we have define the GCP provider here in the terraform file
- Defining the cloud storage bucket in the tf config file
resource "google_storage_bucket" "cloud_bucket" {
name = "my-terraform-bucket"
location = "US"
force_destroy = truelifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
}
}
}
- Here we have created the cloud storage bucket that has a lifecycle rule to delete the objects that are more than 10 days.
Initializing and Applying the Configuration
1. Initialize the Terraform Project:
— Run the following command in your project directory to initialize the project and download the necessary provider plugins:
terraform init
2. Check your configuration plan:
– Run the following command to see what actions the Terraform performs to create a defined resource:
terraform plan
3. Apply the configuration:
– Create the resource by running the following command:
terraform apply
- Enter `yes` when prompted to confirm the operation. 4. Check the resource:
– After completing the use operation, go to Cloud Console Google and access storage> Browser to make sure that the bucket has been created.
Conclusion:
- Here, we have created the cloud storage bucket using terraform, taking the provider as GCP and also adding the lifecycle rule to the bucket.
Resources:
Thank you for reading this. In case you have any queries, feel free to reach out to me on LinkedIn.
Source Credit: https://medium.com/google-cloud/automating-google-cloud-storage-bucket-creation-with-terraform-e170ce122131?source=rss—-e52cf94d98af—4
