Install Terraform on Windows 10 / Windows Server 2019

Posted on 282 views

Q: How can I install Terraform on Windows 10 / Windows Server 2019?. This guide will walk you through the steps to install Terraform on Windows 10 and Windows Server 2019. Terraform is a cloud-agnostic Infrastructure automation tool used to manage cloud and on-premise resources in code. Terraform can build, change, and version infrastructure deployed on popular service providers.

With Terraform you can manage Compute, Networking, DNS, Database resources and many others using simple Declarative Programming Language. See the complete list of Terraform Providers.

Install Terraform on Windows Desktop / Windows Server.

In this guide, we will use Scoop command line installer for Windows to setup terraform on Windows. Before you proceed, you’ll need scoop installed on your Windows machine, you can use our guide below.

Once Scoop is installed, use it to install terraform.

PS C:\Users\Administrator> scoop install terraform which vim touch
 WARN  Scoop uses 'aria2c' for multi-connection downloads.
 WARN  Should it cause issues, run 'scoop config aria2-enabled false' to disable it.
 Installing 'terraform' (0.11.12) [64bit]
 Starting download with aria2 …
 Download: [#3633c5 96KiB/20MiB(0%) CN:4 DL:245KiB ETA:1m24s]
 Download: [#3633c5 1.0MiB/20MiB(5%) CN:4 DL:698KiB ETA:28s]
 Download: [#3633c5 1.7MiB/20MiB(8%) CN:4 DL:688KiB ETA:27s]
 Download: [#3633c5 2.0MiB/20MiB(10%) CN:4 DL:577KiB ETA:32s]
 Download: [#3633c5 2.1MiB/20MiB(10%) CN:4 DL:393KiB ETA:47s]
 Download: [#3633c5 2.4MiB/20MiB(12%) CN:4 DL:384KiB ETA:47s]
 Download: [#3633c5 2.5MiB/20MiB(12%) CN:4 DL:352KiB ETA:51s]
 Download: [#3633c5 2.7MiB/20MiB(13%) CN:4 DL:325KiB ETA:55s]
.....

terraform-windows-01

The terraform exe file will be located inside ~/scoop/ directory.

PS C:\Users\Administrator> which terraform
 C:\Users\Administrator\scoop\shims\terraform.EXE

Configure Terraform on Windows Desktop / Windows Server

Now that terraform is installed, let’s create a test project.

> mkdir projects
 Directory: C:\Users\Administrator Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 3/9/2019 1:57 AM projects PS C:\Users\Administrator> mkdir projects

Create terraform folder inside projects directory.

PS C:\Users\Administrator> cd .\projects\
 PS C:\Users\Administrator\projects> mkdir terraform
 Directory: C:\Users\Administrator\projects Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 3/9/2019 1:58 AM terraform PS C:\Users\Administrator\projects> cd .\terraform\

Create Terraform main configuration file.

touch main.tf

I’m doing a Test with AWS Provider but you can use other Providers for your projects. My terraform configuration provider section is as below

PS C:\Users\Administrator\projects\terraform> cat .\main.tf
# Provider
 provider "aws" 
   access_key = ""
   secret_key = ""
   region = "us-west-1"
 

Paste your AWS Access Key and Secret Key inside the access_key and secret_key sections respectively. When done, run terraform init to initialize a Terraform working directory.

PS C:\Users\Administrator\projects\terraform> terraform init
 Initializing provider plugins…
 Checking for available provider plugins on https://releases.hashicorp.com…
 Downloading plugin for provider "aws" (2.1.0)… 
 The following providers do not have any version constraints in configuration,
 so the latest version was installed.
 To prevent automatic upgrades to new major versions that may contain breaking
 changes, it is recommended to add version = "…" constraints to the
 corresponding provider blocks in configuration, with the constraint strings
 suggested below.
 provider.aws: version = "~> 2.1" 
 Terraform has been successfully initialized!
 You may now begin working with Terraform. Try running "terraform plan" to see
 any changes that are required for your infrastructure. All Terraform commands
 should now work.
 If you ever set or change modules or backend configuration for Terraform,
 rerun this command to reinitialize your working directory. If you forget, other
 commands will detect it and remind you to do so if necessary.

Terraform will automatically download provider configured to .terraform directory.

PS C:\Users\Administrator\projects\terraform> ls
Directory: C:\Users\Administrator\projects\terraform
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 3/9/2019 2:06 AM .terraform
-a---- 3/9/2019 2:04 AM 42358 .main.tf.un~
-a---- 3/9/2019 2:04 AM 99 main.tf
-a---- 3/9/2019 2:03 AM 59 main.tf~

Inside there is another folder which store plugins downloaded by Terraform.

terraform-windows-02

Let’s now add resource section to create AWS VPC and Subnet resources by editing the main.tf file.

# Provider
 provider "aws" 
   access_key = ""
   secret_key = ""
   region = ""
 

# Retrieve the AZ where we want to create network resources
data "aws_availability_zones" "available" 

# VPC Resource
resource "aws_vpc" "main" 
  cidr_block = "10.11.0.0/16"
  enable_dns_support = true
  enable_dns_hostnames = true
  tags 
    Name = "Test-VPC"
  
  tags 
    Environment = "Test"
  


# AWS subnet resource
resource "aws_subnet" "test" 
 vpc_id = "$aws_vpc.main.id"
 cidr_block = "10.11.1.0/24"
 availability_zone = "$data.aws_availability_zones.available.names[0]"
 map_public_ip_on_launch = "false"
 tags 
   Name = "Test_subnet1"
 

Save the file after adding resource definitions and setting AWS variables then generate and show an execution plan.

PS C:\Users\Administrator\projects\terraform> terraform plan

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

data.aws_availability_zones.available: Refreshing state...

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_subnet.test
      id:                               
      arn:                              
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "us-east-1a"
      availability_zone_id:             
      cidr_block:                       "10.11.1.0/24"
      ipv6_cidr_block:                  
      ipv6_cidr_block_association_id:   
      map_public_ip_on_launch:          "false"
      owner_id:                         
      tags.%:                           "1"
      tags.Name:                        "Test_subnet1"
      vpc_id:                           "$aws_vpc.main.id"

  + aws_vpc.main
      id:                               
      arn:                              
      assign_generated_ipv6_cidr_block: "false"
      cidr_block:                       "10.11.0.0/16"
      default_network_acl_id:           
      default_route_table_id:           
      default_security_group_id:        
      dhcp_options_id:                  
      enable_classiclink:               
      enable_classiclink_dns_support:   
      enable_dns_hostnames:             "true"
      enable_dns_support:               "true"
      instance_tenancy:                 "default"
      ipv6_association_id:              
      ipv6_cidr_block:                  
      main_route_table_id:              
      owner_id:                         
      tags.%:                           "2"
      tags.Environment:                 "Test"
      tags.Name:                        "Test-VPC"


Plan: 2 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

PS C:\Users\Administrator\projects\terraform>

Finally build your Infrastructure with Terraform using terraform apply.

PS C:\Users\Administrator\projects\terraform> terraform apply

data.aws_availability_zones.available: Refreshing state...

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_subnet.test
      id:                               
      arn:                              
      assign_ipv6_address_on_creation:  "false"
      availability_zone:                "us-east-1a"
      availability_zone_id:             
      cidr_block:                       "10.11.1.0/24"
      ipv6_cidr_block:                  
      ipv6_cidr_block_association_id:   
      map_public_ip_on_launch:          "false"
      owner_id:                         
      tags.%:                           "1"
      tags.Name:                        "Test_subnet1"
      vpc_id:                           "$aws_vpc.main.id"
...........................

Confirm changes to be made and type “yes” to initiate modifications.

Plan: 2 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.
Enter a value: yes

A successful terraform run should print success message at the end.

terraform-windows-03

Terraform state is saved to .\terraform.tfstate but the backend can be changed.

You can confirm Infrastructure changes from AWS console.

terraform-windows-04-1024x179 (1)

terraform-windows-05-1024x129

Destroying Terraform Infrastructure

We have confirmed that our Terraform installation on Windows is working as expected. destroy Terraform-managed infrastructure by running terraform destroy command.

PS C:\Users\Administrator\projects\terraform> terraform destroy

aws_vpc.main: Refreshing state... (ID: vpc-0e94a7d72c02dab2b)
data.aws_availability_zones.available: Refreshing state...
aws_subnet.test: Refreshing state... (ID: subnet-0ad06c2e86542ddc1)

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  - aws_subnet.test

  - aws_vpc.main


Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

If you don’t want confirmation prompt, use:

terraform destroy -auto-approve

See below output:

terraform-windows-06 (1)

Login to AWS console and affirm resources deletion.

terraform-windows-07-1024x180 (1)

Installing terraform on other systems:

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.