Skip to content

Instantly share code, notes, and snippets.

@ziritrion
Last active July 13, 2024 10:48
Show Gist options
  • Save ziritrion/3214aa570e15ae09bf72c4587cb9d686 to your computer and use it in GitHub Desktop.
Save ziritrion/3214aa570e15ae09bf72c4587cb9d686 to your computer and use it in GitHub Desktop.
Quick guide to easily create a VM instance in GCP and set up SSH access with Gcloud SDK

Install and setup Gcloud SDK

  1. Download Gcloud SDK from this link and install it according to the instructions for your OS.
  2. Initialize the SDK following these instructions.
    1. Run gcloud init from a terminal and follow the instructions.
    2. Make sure that your project is selected with the command gcloud config list

Creating a VM instance

  1. From your project's dashboard, go to Cloud Compute > VM instance
  2. Create a new instance:
    • Manual setup:
      • Any name of your choosing
      • Pick your favourite region. You can check out the regions in this link.
      • Pick a E2 series instance. A e2-standard-4 instance is recommended (4 vCPUs, 16GB RAM)
      • Change the boot disk to Ubuntu. The Ubuntu 20.04 LTS version is recommended. Also pick at least 30GB of storage.
      • Leave all other settings on their default value and click on Create.
    • Gcloud SDK setup:
      gcloud compute instances create dezoomcamp --zone=europe-west1-b --image-family=ubuntu-2004-lts --image-project=ubuntu-os-cloud --machine-type=e2-standard-4 --boot-disk-size=30GB
  3. When you create an instance, it will be started automatically. You can skip to step 3 of the next section.

Set up SSH access

  1. Start your instance from the VM instances dashboard.
  2. In your local terminal, make sure that gcloud SDK is configured for your project. Use gcloud config list to list your current config's details.
    1. If you have multiple google accounts but the current config does not match the account you want:
      1. Use gcloud config configurations list to see all of the available configs and their associated accounts.
      2. Change to the config you want with gcloud config configurations activate my-project
    2. If the config matches your account but points to a different project:
      1. Use gcloud projects list to list the projects available to your account (it can take a while to load).
      2. use gcloud config set project my-project to change your current config to your project.
  3. Set up the SSH connection to your VM instances with gcloud compute config-ssh
    • Inside ~/ssh/ a new config file should appear with the necessary info to connect.
    • If you did not have a SSH key, a pair of public and private SSH keys will be generated for you.
    • The output of this command will give you the host name of your instance in this format: instance.zone.project ; write it down.
  4. You should now be able to open a terminal and SSH to your VM instance like this:
    • ssh instance.zone.project
  5. In VSCode, with the Remote SSH extension, if you run the command palette and look for Remote-SSH: Connect to Host (or alternatively you click on the Remote SSH icon on the bottom left corner and click on Connect to Host), your instance should now be listed. Select it to connect to it and work remotely.

(Optional) Starting your instance with gcloud sdk after you shut it down.

  1. List your available instances.
    gcloud compute instances list
  2. Start your instance.
    gcloud compute instances start <instance_name>
  3. Set up ssh so that you don't have to manually change the IP in your config files.
    gcloud compute config-ssh

Install stuff

  1. Run this first in your SSH session: sudo apt update && sudo apt -y upgrade
    • It's a good idea to run this command often, once per day or every few days, to keep your VM up to date.

Anaconda:

  1. In your local browser, go to the Anaconda download page, scroll to the bottom, right click on the 64 bit x86 installer link under Linux and copy the URL.
  2. In your SSH session, type wget <anaconda_url> to download the installer.
  3. Find the filename of the installer with ls
  4. Run the installer with bash <filename> (you can start typing the name and then press the Tab key to autocomplete)
  5. Follow the on-screen instructions. Anwer yes to all yes/no questions and leave all other default values.
  6. Log out of your current SSH session with exit and log back in. You should now see a (base) at the beginning of your command prompt.
  7. You may now remove the Anaconda installer with rm <filename>

Docker:

  1. Run sudo apt install docker.io to install it.
  2. Change your settings so that you can run Docker without sudo:
    1. Run sudo groupadd docker
    2. Run sudo gpasswd -a $USER docker
    3. Log out of your SSH session and log back in.
    4. Run sudo service docker restart
    5. Test that Docker can run successfully with docker run hello-world

Docker compose:

  1. Go to https://github.com/docker/compose/releases and copy the URL for the docker-compose-linux-x86_64 binary for its latest version.
  2. Create a folder for binary files for your Linux user:
    1. Create a subfolder bin in your home account with mkdir ~/bin
    2. Go to the folder with cd ~/bin
  3. Download the binary file with wget <compose_url> -O docker-compose
    • If you forget to add the -O option, you can rename the file with mv <long_filename> docker-compose
    • Make sure that the docker-compose file is in the folder with ls
  4. Make the binary executable with chmod +x docker-compose
    • Check the file with ls again; it should now be colored green. You should now be able to run it with ./docker-compose version
  5. Go back to the home folder with cd ~
  6. Run nano .bashrc to modify your path environment variable:
    1. Scroll to the end of the file
    2. Add this line at the end:
      export PATH="${HOME}/bin:${PATH}"
    3. Press CTRL + o in your keyboard and press Enter afterwards to save the file.
    4. Press CTRL + x in your keyboard to exit the Nano editor.
  7. Reload the path environment variable with source .bashrc
  8. You should now be able to run Docker compose from anywhere; test it with docker-compose version

Terraform:

  1. Run curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
  2. Run sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
  3. Run sudo apt-get update && sudo apt-get install terraform

Upload/download files to/from your instance

  1. Download a file.

    # From your local machine
    scp <instance_name>:path/to/remote/file path/to/local/file
  2. Upload a file.

    # From your local machine
    scp path/to/local/file <instance_name>:path/to/remote/file
  3. You can also drag & drop stuff in VSCode with the remote extension.

  4. If you use a client like Cyberduck, you can connect with SFTP to your instance using the instance.zone.project name as server, and adding the generated private ssh key.

@VladSkripniuk
Copy link

Thank you so much! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment