How-To Deploy Servers to Google Cloud Platform (GCP) with OneFuse and Terraform

Overview

In this article we will go over how to deploy Servers to GCP with OneFuse and Terraform. We will only be using Naming and Static Property Sets (SPS) Groups in this article, but more Modules can be added to your TF files.

Considerations

We will assume you already have an account in GCP that has access to build servers. We will also assume that you have OneFuse and Terraform configured

OneFuse Configuration/Validation

Validate Naming Sequence and Naming Policy

  1. Log into OneFuse and go to the Naming section under Modules

    1. In our example Naming Policy below, you can see some templated {{}} values in the Naming Template section. These will be explained when we get to the Static Property Sets (SPS)

       

    2. We kept the Naming Sequence very generic for this test (Maximum Length is 3, Padding Character is 0, etc). You can also see the same template values in the Unique Key section (minus the sequence) which will be explained in the next section.

      This was just an overview of the Naming Policy and Naming Sequence. More information can be found in the Additional Information section at the bottom.

       

  2. Now that we’ve verified that our Naming Policy/Sequence looks good, we can move to the Static Property Sets (SPS).

Validate Static Property Sets (SPS)

  1. Log into OneFuse and go to the Static Property Sets section under Templating

  2. Locate the Static Property Set that you’re going to be using and view/edit it

    1. In the example below, we’re using a Static Property Set (SPS) that is specific for GCP. In the Static Property Set section is where we’re going to be passing in the values for those properties that we templated in the Naming Policy/Sequence.

      More information can be found in the Additional Information section at the bottom.

  3. Now that we’ve verified our Static Property sets, we’re good to continue on to the Terraform configuration


Terraform Configuration/Validation

TF File Verification/Modification

  1. Open up your Terraform files in the text editor of your choice and verify that you have the correct settings. If any settings need to be changed, feel free to do so at this time (You can use my examples below for reference)

  2. We’re setting the OneFuse and GCP Providers in the main.tf file and populating the data and resource objects for Naming and Static Property Sets.

    main.tf file

    terraform {
      required_providers {
        onefuse = {
          source  = "CloudBoltSoftware/onefuse"
          version = ">= 1.20.0"
        }
        google = {
          source = "hashicorp/google"
          version = "3.5.0"
        }
      }
      required_version = ">= 0.13"
    }
    
    
    // Inititalize OneFuse Provider
    provider "onefuse" {
      scheme     = var.onefuse_scheme
      address    = var.onefuse_address
      port       = var.onefuse_port
      user       = var.onefuse_user
      password   = var.onefuse_password
      verify_ssl = var.onefuse_verify_ssl
    }
    
    // OneFuse Static Property Set
    data "onefuse_static_property_set" "gcp" {
      name = "GCP"
    }
    
    // Naming Policy data source
    data "onefuse_naming_policy" "machine" {
      name = "NamingPolicy"
    }
    
    resource "onefuse_naming" "machine-name" {
      naming_policy_id        = data.onefuse_naming_policy.machine.id // Refers to onefuse_naming_policy data source to retrieve ID
      dns_suffix              = ""
      template_properties = {
        name              = data.onefuse_static_property_set.gcp.properties.name
        cloud             = data.onefuse_static_property_set.gcp.properties.cloud
      }
    }

  3. In the gcp.tf file below, we’re passing in the credentials from our variables.tf file, but you can pass them with other methods. We are hardcoding the project and region values which can be moved to your Variables file or passed in. In the resource object, we’re hardcoding the machine_type, zone, image, etc which can also be put into your variables file
    Note: Please see link(s) in Additional Information section on other ways to pass the values

    gcp.tf file (GCP Provider)

    # Configure the Microsoft Azure Provider
    provider "google" {
      credentials = file(var.gcp_cred)
      project = "onefuse"
      region  = "us-east1"
    }
    
    # Create virtual machine
    resource "google_compute_instance" "default" {
     name         = onefuse_naming.machine-name.name
     machine_type = "f1-micro"
     zone         = "us-east1-b"
    
    boot_disk {
    initialize_params {
        image = "debian-cloud/debian-9"
        }   
    }
    metadata_startup_script = "sudo apt-get update; sudo apt-get install -yq build-essential python-pip rsync; pip install flask"
    
    network_interface {
    network = "default"
    }
    }

  4. I have not included the variables.tf file because it includes a lot of information that is valid for my environment.

  5. Once we have verified everything, we can continue on to the next step of building the server with Terraform

Building Server with Terraform

  1. Open your console window (we will be using Visual Studio Code in this example) and go to the directory of your TF files

  2. Once you’re there, we will run terraform init

    1. Terraform init will initialize all of the providers and you can see in the below screenshot that OneFuse 1.2 and Google 3.5.0 were initialized.

  3. If you see a message that says Terraform has been successfully initialized!, then we’re good to continue to the next step.

    1. If you see Error: Invalid version constraint, make sure the version for Google is right and try again

  4. Now that everything is initialized, we’ll run terraform plan to make sure everything looks good with the build before we do the apply

    1. The terraform plan output is very long so it was not included here

  5. Take a look through the output from the terraform plan and make sure that everything looks correct

  6. If all looks good with the terraform plan, we’ll do a terraform apply now to build the server

  7. If the terraform apply completed successfully, you should see a message saying “Apply Complete!

    1. If you receive Error: Error creating instance: googleapi: Error 400: Invalid value for field ‘resource.name': 'xxxxxxxx'. Must be a match of regex '(?:a-z?)', invalid, verify that your naming policy meets GCP’s naming requirements

AWS/OneFuse Validation

  1. Log into GCP and verify that the server is there and that you can connect to it

  2. Log back into OneFuse and go to the Naming section under Modules. Scroll down to the bottom and look for the Managed Names section. Verify that the Managed Object (MO) exists for your new server

    1. We can see the correct name is listed and that it used the correct Naming Policy that we passed in

Additional information

GCP - Terraform

HashiCorp - Terraform

OneFuse - Terraform Provider

OneFuse - Naming

OneFuse - Static Property Sets

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.