July 2, 2021 by Faheem Memon6 minutes
Cloud vendors provide their own scripting languages for infrastructure management, such as AWS’s Cloud Formation or Azure’s Resource Manager Templates, but scripting with those can easily get complicated, brittle, and hard to maintain. Terraform uses the simple yet powerful HashiCorp Configuration Language (HCL) which can be scaled easily to an entire enterprise without the side-effects. Terraform has been embraced across the industry and has also been gaining favor with major cloud providers. Check out Azure’s Bicep project or Google Cloud’s recent announcement to support Terraform for Marketplace Private Catalogs.
Terraform not only works across cloud, but its vast ecosystem of plugins also covers on-premises virtualization systems or private clouds such as vSphere, Nutanix, Cisco ACI, HPE OneView and others. You get more milage out of Terraform compared to other tooling. It also works with common shared services such as F5 BigIP load-balancers, Infoblox Grid, Microsoft Active Directory, and others. Additionally, it can be extended through its plugin SDK if needed. Remeber to check out the Terraform Registry and colloborate with existing open-source community before you build your own plugins.
In essence, Terraform manages the entire life-cycle of your infrastructure. It does not, however, replace your VM configuration management tooling. Once your infrastructure is up and running, you can switch over to your configuration management software and apply your configurations, recipes, or playbooks. It integrates nicely with push or pull-based configuration tools such as Ansible, Chef, Puppet, or others. Alternatively, you don’t need any configuration management if you design your infrastructure to be immutable and be replaced instead of in-place updates.
Let’s take Terraform for a spin and write some HCL.
Terraform comes as a single binary that can be installed on a variety of operating systems and CPU architectures. Since I work on a Mac, I am downloading the darwin
binary.
Terraform HFCL files have .tf
file extension. Let’s create our first file, main.tf
:
And we are done! The code above will create a random password. We use the following three commands to run this code.
Before we run our script, we have to initialize the “backend” and download the referenced plugins. Let’s look into these quickly:
Backend is where Terraform stores it state. After every code run, Terraform stores the configuration and the result in a state file. State can be stored locally or in remote backends. When working with a real environment, you have to share, version, backup, and secure your Terraform state and remote backends are ideal. However, since we have not configured any remote backend, the Terraform will create the state locally.
We are using the random_password
plugin to create our password. This plugin is provided by HashiCorp and is available through their public registry.
The logs from the init
command are pretty descriptive. Let’s see what files are created in our directory.
.terraform
is the folder where the plugins are downloaded. This folder is auto-generated and contains the binary files, it should not be included in the version control system.
The second step is to list the changes the script is going to make. Since this is our first time running this code, this step is not as helpful. However, it will be beneficial when you make changes to any existing infrastructure.
We are adding one new resource, perfec! So let’s apply these changes now.
Terraform apply
command displays the planned changes one more time and prompts before applying these changes. We have successfully created the random password.
But wait, where is the password we just created? The password is created and stored in the terraform state. You wouldn’t want to print the password to the screen, would you? Now, you can safely reference the password when creating another resource, such as a Virtual Machine, or store it in HashiCorp Vault by extending the HCL script.
For our exercise, however, let’s look at the state file terraform created for us.
You can see that the password is stored in the state file as plain text. That is why you need to keep this file in a secure place. For added security, Terraform supports several remote backends that provide encryption at rest.
That’s it for this post, in our next post we will create a virtual machine in AWS with some basic configuration. Stay tuned.