Terraform setup on Windows: A comprehensive guide for beginners
A beginner's guide to installing and configuring Terraform on Windows for automating infrastructure management with AWS.
Introduction
Infrastructure-as-code (IaC) is a software engineering practice developers use to define and manage infrastructure using a code-based approach. IaC allows developers to define infrastructure resources using code rather than manual configuration.
Terraform is an IaC tool that enables DevOps engineers to manage their resources in the cloud or on-premise environment in a human-readable format. With Terraform, you can automate your infrastructure using code written in configuration files.
In this article, I'll guide you through setting up Terraform on Windows OS so you can automate your infrastructure management. You'll learn how to install Terraform and configure it with AWS.
Prerequisites
The following requirements are necessary for utilizing this article to install Terraform:
An AWS account and an IAM user with the least privileges.
AWS CLI. You can get started using this AWS documentation.
Chocolatey package manager.
WinRAR or any other extraction tool.
Installing Terraform on Windows
There are two ways of installing Terraform on your PC. If you want to install it manually, you can download the Terraform binary file or run it from the command line.
Manual installation
This method of installation downloads the Terraform binary file in a zip format. Unzip it and add this file to your system's environmental variables.
You can use the following steps below to download the Terraform binary file:
Open your browser and navigate to the Terraform installation console.
Figure 1. Terraform installation console.
Select the Windows operating system.
Figure 2. Terraform windows option.
Download the binary file that fits your system's processor architecture.
Figure 3. Select the binary file.
If you're not sure of your processor architecture, follow the steps below to confirm:
From the command line, run the command below:
wmic OS get OSArchitecture
Once the download is complete, extract the file. You can use WinRAR for this extraction.
Figure 4. Extract terraform.exe from zip.
With this, Terraform has been installed on your PC. However, when you try to use Terraform, you get an error.
Figure 5. Terraform not found.
This error occurs because your system doesn't understand the terraform
command. Therefore, you must add the executable file to your system's PATH environmental variable.
Below are the steps to add a .exe
file to your system variables:
On your Windows search, type environmental variables.
From the System Properties console, click on the Environment Variables button.
Figure 6. Windows systems properties console.
On the System variables section, double-click on the Path variable.
Figure 7. Path environmental variable.
Click on the New button and add the location to the Terraform folder.
Figure 8. Add environmental variable.
You can confirm Terraform works by running the command below:
terraform -version
Figure 9. Terraform version output.
Hurray! It worked, but if you observed, the response was slow. The reason is that Terraform Path has only been added to the system variables. Therefore, running a Terraform command would search the entire system to find that variable value and execute the command.
Adding the Terraform executable path to the user variables is also advised to make this process faster.
You can use the following steps to add Terraform to your system's user Path variable.
From your Environmental Variables console, navigate to the User variable section and double-click the path variable.
Figure 10. User variables section.
You can add the path to the downloaded executable file by clicking the New button.
Figure 11. Add path to user variables.
Close the console, and rerun the command in a new terminal.
If you noticed, the command should execute faster.
Command line installation
This method is an easier way of installing Terraform. It requires the Chocolatey package manager and Powershell. An advantage of this method is that you don't have to add the path to environment variables explicitly.
You can use the following steps to install Terraform using Chocolatey:
From the search tab, type Powershell and run it as an administrator.
Figure 12. Run PowerShell as administrator.
Run the command below:
choco install terraform
Figure 13. Execute choco command to install terraform.
With this, you have successfully installed Terraform on your system. But that's not where it ends. You need to authenticate Terraform with AWS.
Configuring Terraform
Terraform is an IaC tool that works efficiently with multiple cloud providers, so it is important to configure this tool with a popular cloud provider such as AWS.
As an IaC tool, configuring it with a cloud provider makes allows it to manage any infrastructure in the cloud since it can communicate with the provider through its command line interface.
You can use the following steps to configure Terraform with AWS:
Confirm that you have the AWS CLI installed by checking the version of AWS CLI on your PC;
aws --version
Configure the AWS CLI using this command:
aws configure
You will be prompted to add your AWS access key ID, secret access key, default region name, and default output format.
You can get most of this information from the .csv
file you got after setting up your IAM user. After setup, you can view all your configuration by running the command below;
aws configure list
That's it! You have now completed the authentication of AWS CLI with your AWS account. Terraform can communicate with your account using these configurations.
Terraform commands
Having installed Terraform on your PC, it's time to run some Terraform commands. Terraform has a very basic workflow; you can see it in the sample project.
You will automate the setup of an EC2 instance on AWS using Terraform modules.
You can use the following steps to complete this task:
Make a new directory and create a file called main.tf.
mkdir terraform cd terraform/ touch main.tf
Open the
main.tf
file and copy the code below;provider "aws" { region = "us-east-1" } module "ec2_instance" { source = "terraform-aws-modules/ec2-instance/aws" version = "~> 3.0" name = "single-instance" ami = "ami-ebd02392" instance_type = "t2.micro" key_name = "user1" monitoring = true vpc_security_group_ids = ["sg-12345678"] subnet_id = "subnet-eddcdzz4" tags = { Terraform = "true" Environment = "dev" } }
You can get the
vpc_security_group_ids
,ami
, andsubnet_id
from your AWS console.Terraform init: This command prompts Terraform to download the provider set in the configuration file, which in this case is AWS. Also, it initializes the project directory.
To initialize a directory, use the command below:
Terraform init
Figure 14. Terraform initialization.
Terraform fmt and Terraform validate: As with all code, adhering to correct syntax and consistent formatting is essential, which is made easy with
terraform fmt
.terraform validate
, as the name implies, will check if your code is valid.Apply both commands to ensure your code is properly written before making changes to your infrastructure.
Below is how to use the format and validate commands;
terraform fmt
Figure 15. Terraform format.
Figure 15 displays the file name, indicating proper formatting. If the file name isn't displayed, there may be some issues with your configuration file.
terraform validate
Figure 16. Terraform validate.
Figure 16 displays a success message, indicating the configuration above is valid!
Terraform plan: With this command, Terraform creates an execution plan for your infrastructure. Terraform will compare the current state of your infrastructure with the desired state in the configuration file and create a detailed report.
terraform plan
Figure 17. Terraform plan.
Figure 17 displays the report indicating the changes Terraform will make to the infrastructure.
Terraform apply: The
terraform apply
command prompts Terraform to apply all the changes specified whenterraform plan
is run. This command will change your infrastructure, so it is advisable to read through all the changes to ensure everything is as desired.Due to the importance of this command, Terraform will prompt you to confirm the changes before executing this process.
To apply this configuration to your infrastructure, run the command below.
terraform apply
Figure 18. Terraform apply.
Figure 18 shows that all changes have been made. To confirm, head over to your AWS EC2 console and observe the newly created instance running.
Figure 19. AWS EC2 console.
Great! This instance was provisioned by Terraform using code from your configuration file.
Note: A tip to bypass the Terraform request to confirm all changes is by adding the option below to the
terraform apply
command;terraform apply --auto-approve
You can use this tip only if you know the changes to be made and read through the
terraform plan
report.Terraform destroy: Terraform doesn't only create infrastructure but also deletes all resources. It provides an easy way of automatically deleting all resources created without doing them one after the other.
As it is commonly known, it is easier to destroy something than build it. Therefore, to destroy all the resources created, you can use the command below:
terraform destroy --auto-approve
Figure 20. Terraform destroy.
Conclusion
In conclusion, Terraform is a powerful tool for infrastructure as code. It can help DevOps engineers streamline their workflow and efficiently manage their infrastructure.
This comprehensive guide discussed the basics of Terraform setup on Windows, including installation, configuration, and authentication with AWS. This guide also explored some basic Terraform commands like terraform plan and terraform apply, allowing you to preview and apply changes to your infrastructure.
By following the steps outlined in this guide, you can get started with Terraform and take advantage of its many benefits for your infrastructure management needs. Whether you're just becoming familiar with or looking to expand your knowledge, this guide has provided the information you need to get started with Terraform on Windows.