hashicorp terraform

This document calls out getting started with Terraform

1: Ensure we install all the pre-reqs on your station.

Pre-Reqs

  • Terraform

  • Azure CLI

  • psql

  • Kubectl

  • helm

  • consul

  • vault

  • openssl

Installation of Terraform & Azure CLI

Make sure you install terraform from here: https://www.terraform.io/downloads.html.

On Windows

I like chococately, so you can install using:

# install choco
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Powershell
choco install pwsh -y

# Aspnetcore
choco install dotnetcore-windowshosting -y
choco install dotnetcore -y
choco install dotnetcore-2.0-sdk -y
choco install dotnetcore-2.1-sdk -y
choco install dotnetcore-2.2-sdk -y
choco install dotnetcore-3.0-sdk -y
choco install dotnetcore-3.1-sdk -y
choco install dotnetcore3-desktop-runtime -y
choco install dotnetcore-3.1-runtime -y
choco install dotnet-5.0-runtime -y
choco install dotnet-5.0-sdk -y
choco install netfx-4.8 -y
choco install dotnetfx -y

# install Azure CLI via choco
choco install azure-cli -y
az extension add --name azure-cli-iot-ext

# PSQL:
choco install postgresql12 -y

# Hashi Consul/Vault/Terraform
choco install consul -y
choco install choco install vault -y
choco install terraform -y

# docker/Kubernetes/helm
choco install docker-desktop -y
choco install kubernetes-cli -y
choco install kubernetes-helm -y

# Python
choco install python -y

# node
choco install nodejs -y

On Ubuntu

Setup your Env Variables with SPN for Azure

In order to use terraform in a secure way, the spn id/secrets should not be stored in source code, so each time you want to setup an env, you'll want to change your env variables to the necessary accounts that have access. Here is an example way of using powershell to create the evn settings

Windows

Ubunut

Install openssl

2: Setup your environment variables:

In order to use terraform in a secure way, the spn id/secrets should not be stored in source code, so each time you want to setup an env, you'll want to change your env variables to the necessary accounts that have access. Here is an example way of using powershell to create the evn settings

Windows

Ubuntu

3: Generate a ssh key to be used for securing the deployment:

‌Once this is done, then add the public key‌

Convert root cert token for API gateway to base64

Note: This only needs to be done once, adn then update vnet_gw_vpn_cert_name & vnet_gw_vpn_cert_data in the /terraform/base-env-setup/main.tf

​4: Setup your Workspace

Your terraform workspace is important, as it tells the terraform which variables to use for an associated environment, as well as maintaining the state for what is deployed. For the most part, the default settings can be the same across all environments - but you'll want to differentiate the name/location of the resources that are deployed. This is accomplished using the Terraform Workspace.

Reference: Terraform Workspaces

Once you have done this, you'll want to ensure that you update the terraform/base-env-setup/main.tf to include new environment variables. The idea is we'll have a few "placeholders" for transient environments such as:

  • Dev

  • Alpha

  • Bravo

  • Charlie

If you wanted to add say "Delta", you'd go to the terraform/base-env-setup/main.tf file and add a new section after Charlie -- ensuring you add whatever customization you need for variables to make your new env unique to you.

Setup your workspace

To ensure you connect to the right state store in the blob, make sure you are configured to the proper env

5: Setting up Remote State for Terrafrom

In order to save state to a remote storage location (azure blob), we'll need to setup a storage account and some containers. You'll do that by executing the terraform script under /terraform/1-tf-remotestate.

NOTE: If this has already been executed, you don't need to re-run this 1-tf-remotestate. This exists for the new appdz1 env!

6: Run the initial Terraform deploy of shared stuffs:

By now, we should have the following deployed:

  • Shared Module:

    • Core RG

    • Vnet & subnets

    • AKS Cluster

    • App Gateway & PIP

    • Traffic Manager (For DNS name)

    • Log Analytics

Another Example output:

7. Run the Terraform DB Schema

Now we will need to manually add the firewall rules to allow the aks cluster to reach the pgsql.

  1. Open the https://portal.azure.com, and select that resource group for appdz1-core-rg.

  2. Select the appdz1-core-hyperscale server group

  3. Then select the Networking from the Settings category on the bottom left

  4. There is an item that says "Allow Azure services and resources to access this server group". Change this selection from no to YES

  5. You'll need to also add the public ip of your machine to the firewall rule, otherwise your schema scripts will fail. you can find out your ip address by typing "whats my ip" in google search.

Now that we have configured the access firewalls, we can configure run the schema scripts.

8: Export the k8s credentials:

in order to connect to the cluster, you'll need to credentials of what you just deployed. To do that, lets use AKS creds.

Note: Executing this command is using the environment variables we loaded under step 2 as the user ak cli is running under

9: Generate Secrets used for ingress

10: Deploy the API

11. Initialize the Hashi Vault in a VM

The output of the shared terraform script should include the ip address of the pip of the vm.

12: Install default values into Vault

For this step, you'll need:

  • The Following settings:

    • The IP Address of the Vault ()

    • The Secret of the Vault ()

    • The json blob of settings

An example json file might look like this:

Execute the following commands to upload the settings from a given app

Last updated

Was this helpful?