Resources
    How to Create a Storage B ...
    13 September 22

    How to Create a Storage Bucket with Terraform

    Posted byINE
    facebooktwitterlinkedin
    news-featured

    In our lab walkthrough series, we go through selected lab exercises on our INE Platform. Subscribe or sign up for a 7-day, risk-free trial with INE and access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!

    Purpose: Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. In this article, you will learn how to Create a Storage Bucket with Terraform on Google Cloud Platform.

    Technical difficulty:

    |   Novice   |   Beginner   |   Competent  |   Proficient   |   Expert

    What are Storage Buckets?

    Storage Buckets are the basic data storage containers. Everything you keep in Cloud Storage must be kept in a bucket. Buckets can be used to organize data and control access to it, but unlike directories and folders, you cannot nest buckets. While the number of buckets in a project or location is not limited, the rate at which they can be created or deleted is.

    When you create a bucket, you assign it a globally unique name as well as a geographic location where the bucket and its contents will be stored. The bucket's name and location cannot be changed after it is created, but you can delete and recreate it to achieve a similar result.

    Lab Scenario

    We have set up the below scenario in our INE labs for our students to practice. The screenshots have been taken from our online lab environment.

    Lab Link: Creating a Storage Bucket with Terraform


    Objective

    In this lab, you will use Terraform to create a storage bucket on GCP and upload an object to it.

    Solution

    Step 1: Choose the Start lab button and then choose Open lab. On the next page, agree to the terms and conditions and then select Continue.

    You will be presented with access credentials, as shown in the following example.

    csbt1.png

    The URL will take you to the Google Cloud Platform sign-in page. Sign in using the Username email you were provided with and choose Next.

    Enter the password you were provided with and choose Sign in. If you're prompted to save the password, choose Not Now.​You will be redirected to the Google Cloud Platform.

    Install Terraform

    Step 2: Download Terraform from the given link.

    Link: https://www.terraform.io/downloads

    Find and download the package appropriate for your operating system, and install it.

    Find access token

    Step 3: To find the access token. First, you need to login to Google Cloud Shell with the same credentials.

    Link: https://shell.cloud.google.com/?pli=1&show=ide%2C

    Now run the below-mentioned command​ to get the access token.

    Command:

    gcloud beta auth application-default print-access-token

    csbt2.png

    Note: The access token may expire after some time. You need to generate a new access token each time you are running the terraform script. Otherwise, you may encounter the error.

    Now copy and store the access token somewhere for future reference.

    Create a new variables.tf file

    Step 4: Create a file with the name "variables.tf" in your terraform folder and paste the below-mentioned code into it.

    Code:

    variable "region" {
        type = string
        default = "us-central1"
    }
    variable "bucket_name" {
        type = string
    
    
    
    }
    variable "project" {
        type = string
    }
    variable "access_token" {
        type = string
    }

    In the variables.tf file, we are not hard-coding the values of bucket_name, project, and access_token. You need to provide their value during the execution of the terraform script.

    Create a main.tf file

    Step 5: Your main.tf file will consist of the following components:

    • Initialization details
    provider "google"{
      project = var.project
      region  = var.region
      access_token = var.access_token
    }
    • Defining a storage bucket

    resource "google_storage_bucket" "test" {
      name = var.bucket_name
      project = var.project
      location = var.region
      storage_class = "REGIONAL"
    }

    • Pushing an object into bucket

    resource "google_storage_bucket_object" "picture" {
      name   = "demo"
      source = "./images/pacademy.png"
      bucket = var.bucket_name
    }

    Now, you will combine all components into the main.tf file. In your main.tf file, copy and paste the following:

    Code:

    provider "google"{
      project = var.project
      region  = var.region
      access_token = var.access_token
    }
    resource "google_storage_bucket" "test" {
      name = var.bucket_name
      project = var.project
      location = var.region
      storage_class = "REGIONAL"
    }
    resource "google_storage_bucket_object" "picture" {
      name   = "demo"
      source = "./images/pacademy.png"
      bucket = var.bucket_name
    }

    Step 6: In your current directory, create a folder with the name "images" and store an image (png format) in it with the name "pacademy.png".

    Run the terraform script

    Step 7: To run the terraform script, copy the below-mentioned command and run it into your local command prompt or PowerShell.

    Command:

    terraform init

    csbt3.png

    Step 8: Run terraform plan command

    Command:

    terraform plan

    csbt4.png

    Note 1: In this step, you need to provide values for the project name, access token, and bucket name.

    Note 2: The bucket name must be unique, otherwise, you will encounter the error.


    Step 9: Run terraform apply command to create the resources on GCP.

    Command:

    terraform apply

    csbt5.png

    After running terraform apply, you will encounter an error as shown in the below image.

    csbt6.png

    To remove the error simply re-run the terraform apply command.

    Command:

    terraform apply

    csbt7.png

    csbt8.png

    We can see the resources have been created.

    Open Google cloud console and verify

    Step 10: Open google cloud console and navigate to the cloud storage.

    csbt9.png

    You can see, a bucket has been created.

    Now, click on that bucket and verify whether the image is been pushed or not.

    csbt10.png

    We can see that the image (demo object) is present in our bucket.

    Conclusion

    Congrats! We successfully created a storage bucket and pushed an object into it.

    To perform these tasks hands-on in our lab, subscribe or sign up for a 7-day, risk-free trial with INE to access this lab and a robust library covering the latest in Cyber Security, Networking, Cloud, and Data Science!

    © 2024 INE. All Rights Reserved. All logos, trademarks and registered trademarks are the property of their respective owners.
    instagram Logofacebook Logotwitter Logolinkedin Logoyoutube Logo