Setup Google Cloud Locally
This developer documentation will guide you through the process of setting up the Google Cloud CLI (Command Line Interface) and logging in to your Google Cloud account. The CLI allows you to interact with various Google Cloud services and manage your resources from the command line.
Prerequisites
Before you begin, make sure you have the following prerequisites:
Step 1: Install the Google Cloud SDK
Windows
The Google Cloud CLI works on Windows 8.1 and later and Windows Server 2012 and later.
- Download the Google Cloud CLI installer.
Alternatively, open a PowerShell terminal and run the following PowerShell commands: 1.
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")
& $env:Temp\GoogleCloudSDKInstaller.exe
- Follow the installation instructions specific to your operating system to download and install the Google Cloud SDK.
- After installation is complete, the installer gives you the option to create Start Menu and Desktop shortcuts, start the Google Cloud CLI shell, and configure the gcloud CLI. Make sure that you leave the options to start the shell and configure your installation selected. The installer starts a terminal window and runs the
gcloud initcommand. - If prompted, authenticate with your Google Cloud account by following the on-screen instructions. This will open a web browser window where you can sign in to your Google Cloud account.
- After successful authentication, the SDK will prompt you to select a project. Choose the project you want to work with by entering its corresponding number.
- Select the default configuration for command-line interactions by entering the desired number when prompted.
- Once the initialization process is complete, you are ready to use the Google Cloud CLI
MacOS:
Follow the steps: https://cloud.google.com/sdk/docs/install#mac
PS: If your install.sh is not executable then follow this steps
- To install the tools via shell, first you’ll need to give executable permissions to “install.sh“ file in unzipped folder.
- To do that, open shell and go inside the directory where the required files for gCloud is stored, and type this command
chmod +x install.sh
- After making file executable we’ll run the shell script using this command in the same directory
./install.sh
Step 2: Logging In to the Google Cloud CLI
There are two ways to login:
1. Via personal gmail account
- Open a** command prompt** or **terminal **window.
- Run the following command to log in to your Google Cloud account
gcloud auth login
// or
gcloud auth application-default login
- A web browser window will open, allowing you to sign in to your Google Cloud account. Follow the on-screen instructions to complete the authentication process.
- After successful authentication, the CLI will display a confirmation message.
2. Via Service account
- Service account is basically gcloud’s account created by admin for various purpose like deployment, container management, storage management.
- Service account has list of permissions which gives you authorization to perform certain actions limited by permissions.
- To login via service account, first we’ll need to get key. Ask to your admin if you don’t have one. It’s JSON key file which will be passed while authentication to login via service account.
- To get those key, follow this steps List and get service account keys | IAM Documentation | Google Cloud
WARNING: Keep the key safe. In-case of detection of unauthorized use, delete the keys from service accounts page immediately and report the incident to your manager.
- After downloading keys follow this steps:
- Go to the download folder where the key is stored.
- Write this command to login via shell. NOTE: key file value is the relative path of the key with file name
gcloud auth activate-service-account --key-file=[PATH_TO_FILE e.g.: ./Downloads/key.json]
Now you’re logged in.
Step 3: Verify the Login Status and Switch accounts [Optional]
To verify that you are logged in to the Google Cloud CLI, you can run the following command:
gcloud auth list
Now you have the list of authenticated accounts.
To login into one, type this command
gcloud config set account `[ACCOUNT_NAME]`
Conclusion
Congratulations! You have successfully set up the Google Cloud CLI and logged in to your Google Cloud account. You can now use the CLI to interact with various Google Cloud services and manage your resources from the command line. Refer to the official Google Cloud SDK documentation for more information on available commands and usage: https://cloud.google.com/sdk/docs .