
Terraform is an open-source Infrastructure as Code (IaC) tool designed to automate the provisioning and management of cloud infrastructure resources. With Terraform, users can define infrastructure resources using a domain-specific language, enabling the creation, modification, and version control of infrastructure components across multiple cloud providers.
Terragrunt is a thin abstraction layer that runs on top of Terraform as an improved deployment platform with extra tools to deploy multiple infrastructure resources and services with a Don't Repeat Yourself (DRY) file structure. The Vultr Terraform provider is compatible with Terragrunt, and you can define configurations to create infrastructure resources such as databases, storage, compute instances, and VKE clusters.
This article explains how to provision cloud infrastructure resources on Vultr using Terragrunt. You will set up a DRY configuration file structure consisting of a root Terragrunt directory and parent-child directories to deploy resources using your Vultr API key.
Before you begin:
Deploy a Vultr Ubuntu server to use as the management workstation.
Enable your Vultr API Key and allow the workstation public IP address within the Access Control section to access your Vultr account with the API key.
Access the server using SSH as a non-root user with sudo privileges.
Install Terraform:
Install the JSON processor jq utility to extract the latest Terragrunt version.
Extract the latest Terragrunt version using jq and store it in a new VERSION environment variable.
Download the latest Terragrunt release package using the wget utility and the VERSION variable value.
Enable execute privileges on the terragrunt binary.
Move the Terragrunt binary to a system-wide directory within your user $PATH. For example, /usr/local/bin to activate Terragrunt as a system-wide command.
View the Terragrunt version to verify that the command runs correctly
Output:
Terragrunt uses a hierarchical directory structure to access and execute your configuration files. To deploy resources on your Vultr account, Terragrunt uses the Terraform provider and resource definition configurations in your project directory with a parent-child module structure similar to the one below:
Follow the steps below to set up a root Terragrunt project structure to replicate with multiple parent-child modules and deploy cloud resources to your Vultr account.
Switch to your user home directory.
Create a new directory to store your Terragrunt configurations.
Switch to the new project directory.
Create a new Terraform configuration file provider.tf to define your provider settings.
Add the following contents to the file.
Save and close the file.
The above Terraform provider configuration enables the Vultr Terraform provider version 2.18.0 as the default infrastructure source. The provider block authenticates using your Vultr API key stored in the VULTR_API_KEY variable to access the provider and deploy resources to your Vultr account.
Create a new Terragrunt-specific configuration file terragrunt.hcl to store your Vultr API key.
Add the following contents to the file. Replace example-api-key with your actual Vultr API key.
Save and close the file.
The above configuration references the VULTR_API_KEY variable in your provider.tf configuration with an API key value to connect to your Vultr account.
List the Terragrunt directory files to verify that all necessary parent configurations are available.
Output:
The above Terragrunt file structure uses the provider.tf configuration to invoke the Vultr API and execute the defined operations on your Vultr account. To deploy resources using Terragrunt, you should create child modules to store terragrunt.hc1 configurations that include your infrastructure specifications.
To create Vultr Cloud Compute instances using Terragrunt, set up a new standalone parent directory structure to create child modules that match your target environment or instance type. Depending on your desired instance specifications, Vultr supports the following cloud compute tags:
vc2: Regular Cloud Computevdc: Dedicated Cloudvhf: High-Frequency Computevhp: High Performancevoc: Optimized Cloud Computevcg: Cloud GPUvbm: Bare MetalFor more information, run the following requests using the Curl utility to verify all supported values:
List available plans:
List Vultr regions:
List operating system IDs:
In this section, create a regular Cloud Compute vc2 instance with the vc2-1c-1gb plan which includes 1 vCPU core and 1 GB RAM in the Singapore sgp Vultr location as described in the steps below.
Create a new compute directory to use as a parent directory with underlying child modules.
Copy the parent configuration files provider.tf, terragrunt.hc1 files to the compute directory.
Terragrunt uses the DRY configuration file scheme, but requires parent configurations at the root of each directory. The above command copies root configurations to enable the compute directory as a standalone parent directory referenced by child modules.
Switch to the compute directory.
Create a new Terraform configuration file vultr_instance.tf to use as the base cloud compute instance definition file.
Add the following contents to the file.
Save and close the file.
The above instance definition file creates the necessary variables to use with configuration files in the child modules for deployment to your Vultr account. Within the file:
vultr_instance: Defines a Vultr instance resource with the custom label vultr_compute_instance.label: Sets the compute instance using your LABEL variable as the descriptive value.plan: Specifies the compute instance specification using the PLAN variable value.region Specifies the compute instance region using your REGION variable to deploy the instance.os_id Sets the operating system to deploy with the compute instance using your OS variable value.Create a new child directory regular to set up a test regular Cloud Compute instance.
Switch to the directory.
Create a new resource definition file terragrunt.hcl.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new Vultr Cloud Compute instance with the following specifications:
vc21c1gbsgpUbuntu 20.04Replace the above instance values to match your desired specifications. Within the configuration:
path = find_in_parent_folders(): Scans the parent directory for configurations referenced by the inputs={} values within the file.source = "../": Defines the parent compute directory to use the necessary Terraform provider configuration. ../ defines one step back from the configuration's working directory.inputs ={: Sets the Vultr Cloud Compute instance specifications to match and activate with the vultr_instance.tf file variables.View your compute directory structure to verify the available configuration files.
Output:
Within the above output, Terragrunt runs the regular child module specifications as variable values to the parent directory compute instance configuration. Then, it authenticates and deploys the defined Cloud Compute resource to your Vultr account using the parent Terraform provider configuration.
Test the Terragrunt configuration for errors.
Output:
Initialize the Terragrunt configuration.
When successful, your output should be similar to the one below:
View the Terragrunt project summary to verify your configuration changes.
Your output should be similar to the one below:
Apply your Terragrunt configurations to your Vultr account.
When prompted, enter yes and press Enter to approve the Terraform actions.
When the deployment is successful, your output should be similar to the one below:
Open the Vultr Console.
Navigate to Products -> Compute and verify that the new Cloud Compute instance is available.
To create Vultr Managed Databases with Terragrunt, set up a new parent directory to use with your provider configurations and specify your database resource configurations to deploy on your Vultr account. Depending on your target database engine and plan, Vultr supports the following values:
Database Engines: MySQL, PostgreSQL, Valkey
View the list of available plans per Vultr location:
Follow the steps below to create a sample Vultr Managed Database for Caching using the vultr-dbaas-hobbyist-cc-hp-intel-1-11-1, 1 GB RAM and 1 vCPU plan in the Singapore sgp Vultr location. When deploying a different database engine, replace the valkey configuration values with your target Vultr Managed Database specifications.
Switch to the root Terragrunt project directory.
Create a new parent directory Databases to store your resource configurations.
Copy the provider.tf and terragrunt.hc1 files to the new parent directory.
Switch to the Databases directory.
Create a new configuration file vultr_valkey.tf to define the database specification values.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new database resource that uses variable values from the child directory Terragrunt.hc1 file. Within the configuration:
resource "vultr_database": Sets Vultr Managed Database as the resource type with a custom descriptive label vultr_db.database_engine: Specifies the Vultr Managed Database engine using the DB_Engine variable value.database_engine_version: Sets the target Vultr Managed Database version using the DB_VERSION variable value.region: Sets the target Vultr location to deploy a Vultr Managed Database using the DB_LOCATION variable value.plan: Sets the Vultr Managed Database plan to use as the backend infrastructure.label: Assigns a descriptive label to the Vultr Managed Database using the DB_LABEL variable value.variable: Declares the configuration variables for use within the child specification values.Create a new child directory to store the Vultr Managed Database resource values. For example, valkey.
Switch to the directory.
Create a new resource configuration file terragrunt.hcl.
Add the following contents to the file.
Save and close the file.
The above configuration creates a new Vultr Managed Database for Caching with the following values:
Database Type: valkeyVultr location: Singapore sgpDatabase Label: prod-valkey-dbDatabase infrastructure plan: 1 vCPU, 1 GB RAM, HP Intel-powered backend serverWithin the configuration:
path = find_in_parent_folders(): Scans the specified source path for all Terragrunt configurations referenced from the file.source = "../": Sets the location of the Terraform provider and other configurations.input = {: Sets all variable values to match the Terragrunt instance configuration defined in the parent directory.Test the Terragrunt configuration for errors.
Output:
View the Terragrunt plan to verify all changes to apply.
Output:
Apply your Terragrunt configurations.
When prompted, enter yes and press Enter to approve the changes to your Vultr account.
Wait for a few minutes for the database creation process to complete, when successful, your output should be similar to the one below:
Access the Vultr Console and verify that your new Vultr Managed Database for Caching is available.
To create additional Vultr Managed Databases using Terragrunt, create new child directories with your target database specifications to apply to your Vultr account. To destroy a resource, run terragrunt destroy within the target directory and verify the changes to apply to your Vultr account.
To create a Vultr Kubernetes Engine (VKE) cluster with Terragrunt, set up a new child directory to store the cluster configurations. Depending on your cluster specification, each resource configuration file includes a node pools section that specifies your backend VKE cluster structure. Follow the steps below to create a 3-node Vultr Kubernetes Engine (VKE) cluster using the regular Cloud Compute vc2, 2 vCPUs, 4 GB RAM plan per node.
Navigate to your root Terragrunt project directory.
Create a new parent directory vke to store your cluster definition files.
Copy the provider.tf and terragrunt.hc1 files to the parent directory.
Switch to the directory.
Create a new Terraform configuration file vke.tf to define the cluster specifications.
Add the following contents to the file.
Save and close the file.
The above configuration defines a new Vultr Kubernetes Engine (VKE) cluster configuration with the following specifications:
region: Sets the Vultr location to deploy the VKE cluster using the REGION variable value.label: Specifies the VKE cluster descriptive label using the NAME variable value.version: Sets the target Kubernetes version to deploy with the cluster using the VERSION variable value.node_pools: Defines the VKE worker node specifications. Within the section:node_quantity: Sets the number of nodes to attach to the VKE cluster.plan: Defines the compute plan to deploy with the cluster nodes. To view the list of available Vultr Cloud Compute plans, visit the API plans list.label: Sets a descriptive label for all VKE nodes.auto-scaler = true: Enables auto-scaling of VKE cluster nodes.min_nodes: Sets the minimum number of nodes to support in the VKE node pool.max_nodes Sets the maximum number of nodes the VKE node pool can scale to when auto-scaling is enabled.Create a new directory prod to store the cluster configuration values.
Switch to the directory.
Create a new configuration file terragrunt.hcl to set the VKE cluster values.
Add the following contents to the file.
Save and close the file.
The above configuration defines a VKE cluster with the following values:
ewr)vc2, 2 vCPUs, 4 GB memory (vc2-2c-4gb) per nodeWithin the configuration:
path = find_in_parent_folders(): Scans the parent directory to use the referenced configuration values.source = "../": Sets the parent directory to use the defined provider configuration. The value ../ instructs Terragrunt to scan up one level in the directory tree. In the configuration, ../ points to the previous directory vke as the parent directory to scan for all required configurations.inputs = {: Defines the values to match each variable referenced in the parent directory resource specification file.Test the Terragrunt configuration for errors.
Output:
View the Terragrrunt plan to verify the changes to apply to your Vultr account.
Output:
Apply the Terragrunt configurations to your Vultr account.
When prompted, enter yes and press Enter to approve the deployment actions.
Wait for at least 3 minutes for the cluster creation process to complete, when successful, your output should be similar to the one below:
Access the Vultr Console and verify that your VKE cluster is available.
You have set up Terragrunt and deployed resources on your Vultr account. Depending on your cloud infrastructure needs, create a standardized parent-child directory structure to correctly use Terragrunt with a DRY (Don't Repeat Yourself) format to organize and deploy resources using your Vultr API Key. To destroy any resources with Terragrunt, run terragrunt destroy within the target child directory. For more information, visit the official Terragrunt documentation.
0 Comments
Be the first to comment and share your perspective with the community.