Running Terraform on GCP ( Google Cloud Platform) without configuring provider & credentials

Introduction

Whenever we write code for any tool then it becomes very important how can we execute it. In this article we’ll learn how can we execute our terraform code for Google Cloud Platform (GCP) very easily without maintaining the credential json file and provider in our terraform code. We don’t need to set up the Terraform providers.

Let’s Get Started

Assumptions

It is assumed that some utilities are already installed on your system.
1. Google Cloud SDK
2. Terraform
3. configured project in Google Cloud

What we usually do

We write terraform for our GCP environment we configure provider in our code where we

create a service account and give permissions to it
download the generated json file and keep into our terraform code.

and configure those in our terraform code like https://www.terraform.io/docs/providers/google/index.html this.

provider "google" {
credentials = file("path/account_file.json")
project = "project-id"
region = "ap-south1"
}

What we can do

We can just configure the GCP account in our gcloud sdk and then we don’t need to specify any project, credentials file in our terraform code.
Just a few commands to execute and we are good to go.
Let’s go through those commands one by one

  1. Create configuration for the project.

$ gcloud config configurations create PROJECT-NAME

Pic-1

2. Check what properties are set now for the project. So as of now nothing has been set, just the name.

$ gcloud config configurations describe PROJECT-NAME

Pic-2

3. Now initialise to configure the project and account.

$ gcloud init

Pic-3

As we want to re-initialise our current project, we have chosen option-1. We can choose other option too if we want to.

4. Once we enter the choice it asks for account which we want to use, if that is already configured in the terminal then will show in the list otherwise you can go with configuring the same too.

Pic-4

I have chosen the account which was already configured in my terminal.

5. After this, it will prompt to choose the GCP project

Pic-5

We can go with the options available for our account.

6. Once we set our project, it will prompt to set availability zone & region. This is optional, but if we know in which region we are going to work then it’s better to configure this too.

Pic-6

Once above option is selected it will configure the properties for the configuration test-project.

7. Last but not the least, We have to authorise for our account which can be done with below command.

$ gcloud auth application-default login

This will open the browser to verify the account, just allow this so that SDK is authorised to run commands for our project.

That’s all, now we are good to run terraform command for our terraform code without configuring the credentials file and project etc in providers.

This is my first post, please let me know if there is anything I can improve upon. If you like this, please give clap/s for me, it really motivates me to write more useful things.
If you have anything in your mind and want me to write about, then also you can suggest me. My LinkedIn profile in the bio, you can connect with me from there.
Thank you very much for reading this.

Happy Learning !!! 🙂 📚

--

--

Sachin Sharma | Senior DevOps Engineer & Lead

AWS | GCP | Kubernetes | IaC | Terraform | CI/CD | Docker | Security | Python | Automation | DevOps www.linkedin.com/in/rksachin5