How to Install and Setup Terraform on Windows Machine step by step

There are lots of automation tools and scripts available for this and one of the finest tool to automate your infrastructure is Terraform which is also known as Infrastructure as code.

Learn how to Install and Setup Terraform on Windows Machine step by step.

Table of Content

  1. What is Terraform ?
  2. Prerequisites
  3. How to Install Terraform on Windows 10 machine
  4. Creating an IAM user in AWS account with programmatic access
  5. Configuring the IAM user Credentials on Windows Machine
  6. Run Terraform commands from Windows machine
  7. Launch a EC2 instance using Terraform
  8. Conclusion

What is Terraform ?

Terraform is a tool for building , versioning and changing the Cloud infrastructure. Terraform is Written in GO Language and the syntax language of configuration files is hcl which stands for HashiCorp configuration language which is much easier than yaml or json.

Terraform has been in use for quite a while now . I would say its an amazing tool to build , change the infrastructure in very effective and simpler way. It’s used with variety of cloud provider such as Amazon AWS, Oracle, Microsoft Azure , Google cloud and many more. I hope you would love to learn it and utilize it.

Prerequisites

How to Install Terraform on Windows machine

  • Open your favorite browser and download the appropriate version of Terraform from HashiCorp’s download Page. This tutorial will download terraform 0.13.0 version
  • Make a folder on your C:\ drive where you can put the Terraform executable something Like  C:\tools where you can put binaries.
  • Extract the zip file to the folder C:\tools
  • Now Open your Start Menu and type in “environment” and the first thing that comes up should be Edit the System Environment Variables option. Click on that and you should see this window.
  • Now Under System Variables and look for Path and edit it
  • Click New and add the folder path where terraform.exe is located to the bottom of the list
  • Click OK on each of the menus.
  • Now, Open Command Prompt or PowerShell to check if terraform is properly added in PATH by running the command terraform from any location.
On Windows Machine command Prompt
On Windows Machine PowerShell
  • Verify the installation was successful by entering terraform --version. If it returns a version, you’re good to go.

Creating an IAM user in AWS account with programmatic access

For Terraform to connect to AWS Service, you should have an IAM user with an Access key ID and secret keys in the AWS account that you will configure on your local machine to connect to AWS account from your local machine.

There are two ways to connect to an AWS account, the first is providing a username and password on the AWS login page on the browser and the other way is to configure Access key ID and secret keys on your machine and then use command-line tools to connect programmatically.

  1. Open your favorite web browser and navigate to the AWS Management Console and log in.
  2. While in the Console, click on the search bar at the top, search for ‘IAM’, and click on the IAM menu item.
  1. To Create a user click on Users→ Add user and provide the name of the user myuser and make sure to tick the Programmatic access checkbox in Access type which enables an access key ID and secret access key and then hit the Permissions button.
  1. Now select the “Attach existing policies directly” option in the set permissions and look for the “Administrator” policy using filter policies in the search box. This policy will allow myuser to have full access to AWS services.
  1. Finally click on Create user.
  2. Now, the user is created successfully and you will see an option to download a .csv file. Download this file which contains IAM users i.e. myuser Access key ID and Secret access key which you will use later in the tutorial to connect to AWS service from your local machine.

Configuring the IAM user Credentials on Windows Machine

Now, you have an IAM user myuser created. The next, step is to set the download myuser credentials on the local machine which you will use to connect AWS service via API calls.

  1. Create a new file, C:\Users\your_profile\.aws\credentials on your local machine.
  2. Next, Enter the Access key ID and Secret access key from the downloaded csv file into the credentials file in the same format and save the file.
[default]     # Profile Name
aws_access_key_id = AKIAXXXXXXXXXXXXXXXX
aws_secret_access_key = vIaGXXXXXXXXXXXXXXXXXXXX

credentials files help you to set your profile. By this way, it helps you to create multiple profiles and avoid confusion while connecting to specific AWS accounts.

  1. Similarly, create another file C:\Users\your_profile\.aws\config in the same directory
  2. Next, add the “region” into the config file and make sure to add the name of the profile which you provided in the credentials file, and save the file. This file allows you to work with a specific region.
[default]   # Profile Name
region = us-east-2

Run Terraform commands from Windows machine

By Now , you have already installed Terraform on your windows Machine, Configured IAM user (myuser) credentials so that Terraform can use it and connect to AWS services in Amazon account.

Let us first understand terraform configuration files before running Terraform commands.

  • main.tf : This file contains code that create or import other AWS resources.
  • vars.tf : This file defines variable types and optionally set the values.
  • output.tf: This file helps in generating of the output of AWS resources .The output is generated after the terraform apply command is executed.
  • terraform.tfvars: This file contains the actual values of variables which we created in vars.tf
  • provider.tf: This file is very important . You need to provide the details of providers such as AWS , Oracle or Google etc. so that terraform can make the communication with the same provider and then work with resources.

Launch a EC2 Instance Using Terraform

In this demonstration we will create a simple Amazon Web Service (AWS) EC2 instance and run Terraform commands on Windows machine.

  • Create a folder on your desktop or any location on windows Machine ( I prefer it on Desktop)
  • Now create a file main.tf inside the folder you’re in and paste the below content
resource "aws_instance" "my-machine" {  # Resource block to define what to create
  ami = var.ami         # ami is required as we need ami in order to create an instance
  instance_type = var.instance_type             # Similarly we need instance_type
}
  • Create one more file vars.tf inside the same folder and paste the below content
variable "ami" {         # Declare the variable ami which you used in main.tf
  type = string      
}

variable "instance_type" {        # Declare the variable instance_type used in main.tf
  type = string 
}

Next, selecting the instance type is important. Click here to see a list of different instance types. To find the image ID ( ami ) , navigate to the LaunchInstanceWizard and search for ubuntu in the search box to get all the ubuntu image IDs. This tutorial will use Ubuntu Server 18.04.LTS image.

  • Create one more file output.tf inside the same folder and paste the below content
output "ec2_arn" {
  value = aws_instance.my-machine.arn     # Value depends on resource name and type ( same as that of main.tf)
}  
  • Create one more file provider.tf inside the same folder and paste the below content:
provider "aws" {      # Defining the Provider Amazon  as we need to run this on AWS   
  region = "us-east-1"
}
  • Create one more file terraform.tfvars inside the same folder and paste the below content
ami = "ami-013f17f36f8b1fefb" 
instance_type = "t2.micro"
  • Now your files and code are ready for execution .
  • Initialize the terraform using below command.
terraform init
  • Terraform initialized successfully , now its time to see the plan which is kind of blueprint before deployment. We generally use plan to confirm if correct resources is going to provisioned or deleted.
terraform plan
  • After verification , now its time to actually deploy the code using apply.
terraform apply
  • After verification , now its time to actually deploy the code using apply.
terraform apply

Great Job, terraform commands execution was done successfully. Now we should have ec2 instance launched in AWS.

It generally take a minute or so to launch a instance and yes we can see that the instance is successfully launched now in us-east-1 region as expected.

Conclusion

In this tutorial you learnt What is terraform , how to Install and Setup Terraform on Windows Machine and launched an ec2 instance on AWS account using terraform.

Keep Terraforming !!

Hope this tutorial will helps you in understanding and setting up Terraform on Windows machine. Please share with your friends.

How to Install and Setup Visual studio code on Windows machine

Are you a developer ? If you work with various languages and you need a code editor that to make lots of different programs you need a platform that is code editor. You have come to right place to learn about one of the world most widely used code editor that is Visual Studio Code.

In this tutorial we will learn what is visual studio code and how to install and work with visual studio code. Also we will go through the installation of extensions and run a program. Please follow along .

Table of content

  • What is Visual studio code?
  • Prerequisites
  • Install and setup Visual studio code
  • Setup Visual studio code with extension’s and plugins.
  • Conclusion

What is Visual studio Code?

Visual Studio Code is a very light weighted code editor which can be installed in multiple operating systems such as windows operating system, Linux and macOS . The beautify about this code editor is it can work with various languages together. It has a very high support for multiple languages. Also it has strong plugin installation capabilities.

Let us see various benefits of Visual Studio Code.

  • It can automatically complete the your code variables , methods and module because of its intelligent nature.
  • it comes with variety of in built themes which can be used while running the code.
  • It works with top extensions in industry which provides ease to use code inside the editor.

Prerequisites

  • You must have windows 7 + machine
  • .NET Framework 4.5.2 or higher is required

If you dont have .NET Framework 4.5.2 or higher , please install from here

Install and setup Visual studio code

  • Browse to google chrome and click on the link here
  • Now Click on the windows zip installer as below.
  • Now zip file will start downloading on your machine. Extract the zip file and run the code file.
  • Now your visual studio code main screen show pop up.
  • By now we are done with Visual studio code installation , now we can move with setup part where we will install some extensions which are very useful and being widely used by developers.

Setup Visual studio code with extension’s and plugins.

Visual studio code installation is pretty straightforward and is a simple installation which covers very basic features to get you started. But as you learn more languages and tools then you might require some more plugins and extensions. With more extensions you make editor more better and useful.

Let us do a installation of python extension in Visual studio code:

  • Firstly install python from official site of python on your machine. We have another post for how to install python on windows machine , you can refer it .
  • Now, run the command on command prompt to verify if python is successfully installed on your machine.
  • Now come back to Visual studio code and click on extension’s and search for python
  • Now click on Install
  • It takes few seconds to install the extension and as soon as it gets installed , it will reflect in installed
  • As soon as python extension is installed you will notice at the bottom status bar will show you Python interpreter details.
  • Now create a folder on desktop and name it as python or whatever you like and create a file inside it and name it main.py
  • Come back to Visual studio and click on the File on the top and click on Add folder to workspace and select python folder which yoy created.
  • Now add a simple python code in main.py
print("I just installed visual studio and its working fine")
print("Thanks automateinfra.com for taking through this tutorial")
  • Now click on Run file in terminal
  • Verify the result of code.

Conclusion:

This tutorial is prepared with lots of efforts and with complete practical demonstration. In this tutorial we demonstrated what is visual studio code is and how to work with visual studio code. Also we learnt how to install python extension and executed a successful python code. This would give you as a developer or devops engineer a very good start if you are new to Visual Studio code or development.

I would request you to please share the word with others and go through tutorial if you find it useful.

Please share with your Friends and Groups