
Infrastructure as Code (IaC) is the provisioning and managing of infrastructure defined through code instead of manually. It eliminates the need to manually deploy the infrastructure and manage cloud resources like virtual instances, bare metal servers, load balancers, and other resources whenever you want to develop, test or deploy an application.
Terraform is an open-source infrastructure provisioning tool. It allows you to write, plan, and apply changes to deliver infrastructure as code. It uses declarative language, where you don't write steps to perform a task. Instead, you write the result and Terraform figures out how to achieve that result.
This article demonstrates how to deploy an instance and related resources using Terraform.
Terraform uses declarative language, where you write the intended action rather than the steps to perform that action. You can segment the elements of your infrastructure into multiple different configuration files. How you organize resources in files or the order of configuration blocks does not matter. Instead, Terraform considers the relation between resources to determine the provisioning order. It combines the collection of .tf configuration files while performing operations.
Terraform Language Syntax:
Syntax Elements:
{ and } are delimiters of a block body.The following are the main components of Terraform.
Terraform can manage cloud resources available in a provider plugin, allowing it to interact with a cloud service provider. The Vultr provider plugin allows creating, modifying, and destroying infrastructure objects like cloud instances, bare metal servers, load balancers, and so on.
Provider Block Example:
Individual resources are the building blocks in a cloud infrastructure. A resource in Terraform describes a single individual infrastructure object, such as an instance, block storage, or DNS record. The primary purpose of Terraform language is to declare resources, and a majority of other language features are present to make the definition of resources more flexible.
Resource Block Example:
Terraform maintains the state of your infrastructure as a terraform.tfstate file in your workspace. This file contains the present state of your infrastructure, like the defined resources and their configuration. When you update your configuration files and apply the changes, Terraform checks the current state and decides the required steps to achieve your desired result.
The Vultr provider plugin uses the Vultr API to let Terraform interact with the cloud infrastructure. Following are the steps to enable Vultr API access and get an API Key.
Add the GPG key to the sources keyring.
Add the official Terraform repository.
Install Terraform.
Generally, a workspace contains all the configuration files required for a single project. Set up a new directory to store your Terraform configuration files.
Create a new directory.
Enter the directory.
Terraform uses the Vultr provider plugin for interacting with Vultr API to manage the cloud resources. To install the plugin, you need to add the plugin source and version into required_providers inside a .tf configuration file.
Make a separate configuration file to contain the provider plugin configuration.
Using a text editor, create a new provider configuration file.
Add the following content to the file.
Using a text editor, create a new variable container file.
Add the following content to the file and replace api_key with your API key.
Install the provider plugin.
The vultr_instance resource available in the provider plugin allows Terraform to create, read, update or delete a Vultr instance. Find the available arguments and attributes in the Vultr Provider documentation.
Common Arguments:
Common Attributes:
Please Note: You must set os_id, image_id, snapshot_id, iso_id, or app_id to deploy an instance.
Make a new configuration file with a vultr_instance resource block inside it to define the provisioning arguments.
Using a text editor, create a new resource configuration file.
Add the following content to the file.
The above code block provisions a server with 1 vCPU and 1GB RAM in the New Jersey region with Ubuntu 20.04.
Preview the changes.
Apply the changes.
Retrieve the details.
The vultr_block_storage resource available in the provider plugin allows Terraform to create, read, update or delete a block storage volume. Find the available arguments and attributes in the Vultr Provider documentation.
Update the configuration file by adding the vultr_block_storage resource block inside it to define the provisioning arguments.
Using a text editor, edit the resource configuration file.
Add the following content to the file.
The above code block provisions a block storage volume with 10GB storage in the New Jersey region and attaches it to the previously provisioned instance using its ID attribute (vultr_instance.example_instance.id).
Preview the changes.
Apply the changes.
Retrieve the details.
The vultr_reserved_ip resource available in the provider plugin allows Terraform to create, read, update or delete a reserved IP address. Find the available arguments and attributes in the Vultr Provider documentation.
Update the configuration file by adding the vultr_reserved_ip resource block inside it to define the provisioning arguments.
Using a text editor, edit the resource configuration file.
Add the following content to the file.
The above code block provisions a reserved IPv4 address in the New Jersey region and attaches it to the previously provisioned instance using its ID attribute (vultr_instance.example_instance.id). You can deploy an IPv6 address by setting the ip_type attribute to v6.
Preview the changes.
Apply the changes.
Retrieve the details.
Automate the initialization of your Vultr instance using cloud-init by providing a cloud-config file as the user-data attribute while provisioning the instance.
The cloud-config file uses YAML language with the #cloud-config shebang, and the cloud-init process executes it at the first boot of your instance. You may refer to the official cloud-init documentation to know more about its syntax and features.
Using a text editor, create a new resource configuration file.
Add the content below to the cloud-init configuration file.
The demonstrated configuration tells cloud-init to install the Nginx package, overwrite the default index page with a heading (H1) element, and allow incoming HTTP connections.
Using a text editor, create a new resource configuration file.
Add the following content to the file.
The above code block provisions a server with 1 vCPU and 1GB RAM in the New Jersey region with Ubuntu 20.04 and the provided cloud-config file.
Preview changes.
Apply changes.
Retrieve details.
Verify the deployment by opening your instance IP in a web browser or using curl. It takes around 10 mins for cloud-init to complete processing after the instance creation. You can track the progress by opening the web console of the newly deployed instance using Vultr Console.
You can change certain configurations of resources provisioned by Terraform, such as changing instance subscription plans, block storage size, or reserved IP instance pointers.
This section demonstrates the steps to update the configuration of individual resources using Terraform. You can refer to these examples and adapt them to your use case.
Update attributes in the vultr_instance resource block to change the instance subscription plan.
Using a text editor, edit the resource configuration file.
Change the plan attribute in example_instance resource block from vc2-1c-1gb to vc2-1c-2gb. This change upgrades the subscription plan of the instance from 1 vCPU 1GB RAM to 1 vCPU 2GB RAM.
Preview changes.
Output:
Apply changes.
Output:
Retrieve details.
Update attributes in the vultr_blockstorage resource block to change block storage size.
Using a text editor, edit the resource configuration file.
Change the size_gb in example_blockstorage resource block from 10 to 20. This change increases the size of the block storage volume from 10GB to 20GB.
Preview changes.
Output:
Apply changes.
Output:
Retrieve details.
Update attributes in the vultr_reserved_ip resource block to change the target instance.
Using a text editor, edit the resource configuration file.
Change the instance_id in example_reserved_ip resource block from ${vultr_instance.example_instance.id} to ${vultr_instance.instance_cloudinit.id}. This change sets the target instance from example_instance to instance_cloudinit.
Preview changes.
Output:
Apply changes.
Output:
Retrieve details.
Unlike the terraform show command, output blocks allow you to get output containing specific attributes. For example, return only the IP address of an instance.
Update the resource configuration to add an output block to fetch the IP address of the example_instance resource.
Using a text editor, edit the resource configuration file.
Add the following content to the file.
Apply changes.
Retrieve the instance IP address.
Use this example to adapt it to your case regardless of the resource or attribute you may want as an output.
Warning: Applying the demonstrated infrastructure deploys actual cloud resources on your Vultr account. Be sure to delete unused services after testing. Use the
terraform destroycommand to delete all resources provisioned in the demo Terraform workspace.
You used Terraform to create, read, update or delete instances, block storage volumes & additional IP addresses on Vultr. You can deploy infrastructure for your applications using Terraform. These basic operations are a good starting for integrating Terraform into your workflow.
0 Comments
Be the first to comment and share your perspective with the community.