How-To Deploy Servers to Amazon Web Services (AWS) with OneFuse and Terraform

Overview

In this article we will go over how to deploy Servers to AWS 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 AWS and have the keys that you need. 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 AWS. 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 AWS 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"
        }
        aws = {
          source  = "hashicorp/aws"
          version = "~> 3.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" "aws" {
      name = "aws"
    }
    
    // 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.aws.properties.name
        cloud             = data.onefuse_static_property_set.aws.properties.cloud
      }
    }

  3. In the aws.tf file below, we’re using the Credential File option to pass in our credentials (AWS Credential File). Please see link in Additional Information section on other ways to pass in the credentials. We are also hardcoding the AMI, Instance Type, and Key Name values which can be moved to your Variables file or passed in. The Provisioner method inside of here is used to connect into the EC2 instance and set the hostname.

    aws.tf file (AWS Provider)

    provider "aws" {
      region                  = "us-east-1"
      shared_credentials_file = "C:\\Users\\Terraform\\.aws\\credentials"
    }
    
    ###  AWS Machine Deployment ###
    #Virtual Machine Resource
    resource "aws_instance" "web" {
      ami           = "ami-0742b4e673072066f"
      instance_type = "t2.micro"
      key_name  = "rich"
      tags = {
        Name      = onefuse_naming.machine-name.name
      }
      provisioner "remote-exec" {
        inline = [
            "sudo hostnamectl set-hostname aws_instance.web.tags.Name"
          ]
        connection {
          type        = "ssh"
          host        = self.public_ip
          user        = "ec2-user"
          private_key = file(var.private_key_path)
        }
      }
    }

  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 AWS 3.38.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 a Registry service unreachable message, please make sure you can connect out to the internet and try the terraform init 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: Failed to read ssh private key: no key found, make sure that the private_key is pointing to a valid file (usually .pem)

AWS/OneFuse Validation

  1. Log into AWS 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

AWS - Credential File

HashiCorp - AWS Provider

OneFuse - Terraform Provider

OneFuse - Naming

OneFuse - Static Property Sets

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.