Discover Terraform’s power, a valuable Infrastructure as Code tool, and learn how to use it to install an nginx server.
In the ever-evolving landscape of IT and DevOps, Infrastructure as Code (IaC) stands out as a transformative practice. It represents a fundamental shift in how organisations manage and provision their infrastructure resources, and has emerged as a key component for effective, scalable, and cost-effective infrastructure management. The primary objective of this article is to familiarise you with Terraform, an important Infrastructure as Code (IaC) tool, and walk you through the procedure of deploying an nginx server utilising its features.
After reading this article, you will have a basic understanding of Terraform and will be able to use it to install an nginx server. You will understand the importance of Infrastructure as Code in simplifying and automating the deployment process, paving the path for effective management of more complex infrastructure setups. So, whether you’re an upcoming DevOps engineer, an experienced developer, or an infrastructure enthusiast, this article will help you understand Terraform’s strength and potential for orchestrating Infrastructure as Code.
Introducing Terraform
HashiCorp’s Terraform is a business-source Infrastructure as Code (IaC) tool. It helps organisations declaratively create, provide, and manage their infrastructure and services, treating infrastructure configurations as code. Terraform is designed to interact smoothly with a variety of cloud providers, on-premises systems, and third-party services, making it a versatile and extensively used solution in the cloud computing and DevOps fields. Some of the key features of Terraform are as follows.
Declarative configuration: Terraform describes infrastructure resources and their desired state using a declarative language (HashiCorp Configuration Language or HCL). Users describe ‘what’ they want rather than ‘how’ to get it.
Multi-cloud and multi-provider support: Its large provider ecosystem supports various cloud providers (AWS, Azure, Google Cloud, and so on) and third-party services. From a single setup, users may manage resources across numerous platforms.
Resource management: Terraform’s resource architecture enables users to define and manage infrastructure components such as virtual machines, networks, databases, and more.
State management: It keeps track of the current state of controlled infrastructure in a state file. This state file assists Terraform in determining which resources were generated, updated, or destroyed, allowing it to make the necessary adjustments.
Plan and apply workflow: Terraform includes a ‘plan’ command that allows you to preview changes before applying them, which improves predictability and safety. Before making the intended changes, users can review them.
Modularity and reusability: Terraform allows users to create reusable modules, which allow them to encapsulate configurations and promote code reuse across several projects and teams.
Installation
Before you begin working, ensure that Terraform is installed on your workstation; it may be installed on any operating system, such as Windows, macOS, Linux, or others. The process is simple and may be completed in a matter of minutes.
Visit Terraform’s official downloads page at https://developer.hashicorp.com/terraform/downloads. Select the Terraform version you wish to install (usually the most recent stable version). Download the binaries for your operating system (Windows, macOS, or Linux, for example). Terraform is accessible on a variety of platforms. For Linux installation we use the following commands.
Import the HashiCorp GPG key: This command retrieves the HashiCorp GPG key and puts it in /usr/share/keyrings/hashicorp-archive-keyring.gpg. This key is used to validate HashiCorp’s software packages.
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg.
Insert the HashiCorp repository here: The HashiCorp repository is added to the list of software sources using this command. The repository URL is determined by your Linux distribution. $(lsb_release -cs) dynamically finds your distribution’s code name, such as ‘bionic’ for Ubuntu.
echo “deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main” | sudo tee /etc/apt/sources.list.d/hashicorp.list
Refresh the package list and Terraform installation: This command refreshes your system’s package list, ensuring that the newly installed HashiCorp repository is recognised by package management. Subsequently upgrading the package list, you may install Terraform using apt install. The package manager will download and install the most recent Terraform version from the HashiCorp repository.
sudo apt update && sudo apt install terraform.
We may use the terraform cli interface to verify the installation once it has been done successfully. You can verify the installation by running:
terraform --version
Terraform configuration files
Terraform uses the HashiCorp Configuration Language, which is easy to understand, modify, update, and implement by the developer. Configuration files, which have the file extensions .tf and .tf.json, are used to specify Terraform defined infrastructure using a declarative approach. Configuration files allow you to specify your intended state by writing a configuration. These files are collections of resources that contain settings and values that describe the desired state of your infrastructure.
Configuration file (*.tf): In this file, users define the provider and resources to be deployed, as well as the resource type and all resource-specific parameters.
Variable declaration file (variables.tf/variables.tf.json): Here users declare the input variables required to provision resources.
Terraform state file (terraform.tfstate): Terraform creates a state file just once. It saves information about users’ managed infrastructure.
Now let’s run and set up the nginx Docker container.
Running nginx Docker container using Terraform
Create a file name that ends with an extension .tf <filename>.tf. This configuration file is used to configure the cloud provider, network configuration, ports, image, keys, variables, etc, as shown in Figure 1.
Once the file is created and saved, run the command terraform init to apply the changes that install the mentioned plugins, and create a lock file on success. In case of any modification to the .tf file we need to run the command again, as shown in Figure 2.
terraform init
After terraform init, execute the command terraform apply to check the settings and apply; now the instance is ready to begin (Figure 3).
On running the command docker ps, which lists the running Docker container, we see the nginx instance running.
Once the instance is up and running, we can use terraform destroy to terminate the process and its instance.
We hope this initial encounter with Terraform leads you to explore community-driven modules, real-world case studies, and best practices in further detail to fully realise its infrastructure automation potential.
Further exploration opportunities
We have covered server configuration basics in this article, but nginx offers a lot more settings for users to make sure their server runs as smoothly and safely as possible. From load balancers to caching, SSL certificates, and more, you can check out these settings to make sure everything is running smoothly.
Terraform isn’t just for nginx — you can use it to deploy other services too! From databases to cloud storage to virtual machines, Terraform can help you expand your knowledge and expertise in infrastructure automation. Try out different cloud services and providers from Terraform. Find out how to make sure your infrastructure is secure and follow the best practices for controlling access and staying compliant. See how Terraform works in real-life situations.
By adopting Terraform, projects may be made more innovative and agile by effectively creating, managing, and scaling complex cloud architectures. The field of infrastructure automation is still in its infancy. Becoming proficient with Terraform and understanding how it shapes the contemporary infrastructure scene can be highly rewarding. With the constant development of technology and the growing need for efficient and scalable infrastructures, learning and understanding Terraform is a must for any developer and system administrator.