A Beginner's Guide to Terraform

How I switched from AWS to GCP in a matter of minutes

Okay, backstory! It all started when I was working on my current side project DocGuard, which involves cloud storage was my obvious go-to AWS S3 at the time, especially because I already had an AWS account and wanted to try out another AWS service other than RDS (which btw had incurred costs I didn't know about). Desperate to try S3, I tried it and I loved it as it was working so well with my application until disaster struck.

AWS had suspended my account over the unpaid charges from the RDS instance that was still running(which was so funny because I swore I turned those off), and just like that my beloved cloud storage was gone. But then I remembered that Google Cloud had a similar service with their storage buckets.

So I thought to myself this was a good time to learn Terraform as I had heard so many good things about it. So if you've never heard about Terraform, it is an infrastructure-as-code tool that enables you to spin up infrastructure and set up resources in the cloud (clear of AWS CloudFormation btw, an argument for another day).

Using Terraform I was able to quickly set up my environment on Google Cloud Platform and provision a new storage bucket in my project right from my code editor. The wonderful thing about Terraform is that it is cloud-agnostic as it is not for a specific cloud vendor and can be used with multiple cloud vendors or providers like AWS, Google Cloud Platform, or Microsoft Azure just to name a few.

To get started with Terraform:

  1. Install the Terraform binary for your device, depending on your OS. You can get the latest version of Terraform from https://developer.hashicorp.com/terraform/install

  2. Add Terraform to PATH.

    Instructions for Windows: https://stackoverflow.com/a/55949463

    Instructions for MacOS and Linux: https://stackoverflow.com/a/50445479

  3. Make sure you have the AWS CLI or Google Cloud CLI installed and you have authenticated into the respective CLI

  4. Create the Terraform configuration at the root of your project in a file called main.tf

     terraform {
       required_providers {
         aws = {
           source  = "hashicorp/aws"
           version = "~> 4.16"
         }
    
         google = {
           source = "hashicorp/google"
           version = "4.51.0"
         }
       }
     }
    
     provider "google" {
       project = "sylvan-repeater-407810"
       region  = "us-east4"
       zone    = "us-east4-c"
       credentials = file("./docguard-service-key.json")
     }
    
     resource "google_storage_bucket" "docguard-bucket" {
       name = "docguard-bucket-v1"
       location = "us-east4"
     }
    

    The above block of code is called the Terraform configuration.

    The terraform {} section will contain all the settings including the required providers you need in your project.

    The provider {} section shows the details for the provider(s) you want to use, for example in this configuration, I used the google provider and it contained the project id, region, availability zone, and my credentials file.

    The resource {} section contains the resource or service you're trying to set up on a particular provider. For example, in this configuration, I was using the "google_storage_bucket" service on the "google" provider, and I called the storage bucket "docguard-bucket-v1".

    You may be wondering what the difference is between required_providers and provider . required_providers will and must contain all the Cloud Providers you will be using in your project so that they can be installed by Terraform, while provider shows the configuration for each of the providers you specified in required_providers.

    Another important thing to note in this is that, provider shows what cloud vendor you're using in your configuration. You can have more than one provider block. While resource shows what resources you want to set up for each provider.

  5. Now that your Terraform configuration is done, we have to start up Terraform using terraform init . This lets Terraform download the providers and everything needed to build your infrastructure.

  6. Next, you can validate if the changes in your configuration (main.tf) are valid using the terraform validate . If the changes in your configuration are valid, the CLI will return a success message.

  7. Finally, you can apply your changes and push your provisioned infrastructure to your cloud provider using the terraform apply command.

And that's it. Basic cloud computing all from your code editor with minimal effort. If you liked this article, give it a like or comment, and feel free to connect with me on LinkedIn or Twitter.

Thanks for reading, and I'll see you in the next one!