Getting Started with Terraform for Cloud Automation

Getting Started with Terraform for Cloud Automation

Lately I've been spending more time working with Terraform. This is an Infrastructure as Code (IaC) open source tool by Hashi Corp. Terraform gives you the ability to build, change, and version control infrastructure as if it were code. Terraform is vendor agnostic tool, so it can be used to maintain infrastructure in the Public clouds such as Azure and AWS as well as Private clouds such as VMware. Terraform is written in HCL which is a JSON based variant that is very human friendly and easy to read. I'd like to walk you through the basics of Terraform just to get you started.

Terraform Installation

In order to install Terraform you first need to acquire the correct package. Use this link to find the one suitable for your system. You will need to unzip the file to a directory of your choice. Terraform runs a single executable file which makes it easy to get up and running. Note: Make sure you path has the location of terraform.exe so that regardless of where you run the Terraform commands they can execute. Last but not least, lets verify that Terraform is installed correctly and ready to go! I'll be running terraform from PowerShell on a Windows laptop but this will be the same commands on any system the only difference will be the shell. You can do a "terraform --version" to see the version of the installation and any providers that are installed. We haven't added providers yet, so you shouldn't see any.

No alt text provided for this image

Adding Providers to Terraform

Now we need to add at least one provider, so that Terraform can execute against the your environment. I will use the example of Azure in this case. For Azure Cloud the provider is called "azurerm". The Azurerm provider can be found at this link. Once you have it downloaded and unzip simply place the file in the same directory as you terraform.exe file. Note: we still have one more step before the provider can be initialized with Terraform. Go to the directory where you plan to create you Terraform scripts and carry out the following command: "terraform init". Output will look like this:

No alt text provided for this image

Note: If you already have Terraform configuration files it will look different but still successful such as this:

No alt text provided for this image

Logging into Azure

Since are you just getting started the easiest way by far to login to Azure for use with Terraform is to do a "az login" This will open up a browser window and have you login manually. Note: you need to have Azure CLI installed for this to work. Instructions can be found here. Also, for learning purposes you will need an account with Azure but the nice part is you can obtain a free 1 year account for some services and a $200 credit in the first 30 days for any service. Even after that many basic services are free or as long as you destroy what you build right away cost will be very low.

No alt text provided for this image

Create your first Terraform configuration file

Terraform is very well documented, so its easy to get stared with some sample code and just modify it to your needs. Each resource has a web page with info and how to use it in very good detail. We are going to start with the most basic of resources. We are going to build a resource group in Azure. What you can do is go to this link for this resource and just cut and paste the "Example Usage" into a new file called main.tf. As long as your path is setup correctly you can pick any directory to start building your configuration, but always remember to do a "terraform init" in that new directory. I use Visual Studios Code as my editor of choice, but you can do this in any editor even just notepad or nano. Your file should look like this:

No alt text provided for this image

Plan, Apply and Destroy Commands

Now you are ready to start using Terraform. To get you started, we will use the 3 most basic yet important commands you will use over and over again. First, we want to check that have correctly configured our configuration file and see what it will build in Azure but we actually don't want to build it yet in case there are any problems. So what we do is run a "terraform plan" which will run through the motions of building the resource(s) but not actually execute on that plan. To do this run this command in the same directory where you main.tf file is located and you should see this output:

No alt text provided for this image

You can see that it plans to add 1 resource with the listed parameters. No resource will be changed and none to be destroyed. Now we are ready to actually build this resource group in Azure. To do this now run "terraform apply" and you should see this output:

No alt text provided for this image

Note: It will prompt you to confirm you indeed want to build this. You must type in "yes" and hit enter for the build to continue. Anything else will stop it. Once the apply completes, head over to your Azure Portal and check to make sure you can see it. Now time to destroy it. You don't get charged for resource groups but its good practice while you are learning to destroy it in case other resource start costing you money. ( I won't get into it but let's just say I learned the hard way!) To tear this build down you simply do a "terraform destroy". You will be asked for confirmation again. Don't worry about the red it is just to make sure you want to destroy this resource.

I hope this was helpful in getting you started using Terraform. Stay tuned for more articles on Terraform, Ansible, and other items such as Docker coming soon!


To view or add a comment, sign in

More articles by David Rojas

Others also viewed

Explore content categories