Getting Started with Terraform Windows and Terraform EC2

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

This tutorial will show you how to get started with Terraform for Windows and EC2. By the end, you’ll be able to create a configuration that handles basic provisioning of infrastructure in AWS.,

Terraform is a tool that allows you to manage infrastructure. Terraform Windows and Terraform EC2 are two ways in which you can use it. The “terraform ec2 example” will help you get started with Terraform Windows and Terraform EC2.

Getting Started with Terraform Windows and Terraform EC2

HashiCorp’s Terraform Windows is one of the most useful infrastructure as code (IaC) solutions released in the previous several years. For any DevOps or automation process, the ability to version infrastructure, automate resource provisioning, and execute across multiple cloud suppliers is critical.

CI/CD is one of Terraform’s applications. Terraform enables you to do the following:

  • set up a test environment
  • an application to be deployed
  • carry out integration testing
  • the experimental environment is destroyed

All of the processes are also carried out automatically. And there’s no need for a data center!

Prerequisites

This post will show you how to set up and run Terraform Windows. Please make sure you have the following requirements in place if you want to follow along.

  • a computer running Windows 10
  • An Amazon Web Services account
  • On that device, the AWS CLI was installed and setup. You may find instructions for installing and configuring it here.
  • Visual Studio Code using the Terraform plugin (optional)

Terraform Windows Application Installation

To take use of Terraform’s capabilities, you must first install it on your preferred operating system. I’m going to concentrate on Windows in this essay. Terraform, on the other hand, may operate on a variety of operating systems. Take a look at the installation instructions if you’re not using Windows.

The Tough Way

You may install Terraform manually if you don’t have access to a package management or if you want to learn more about how the installation process works. Terraform may be installed by downloading the binary and adding it to the environment variable system path. If this seems daunting, rest assured that there is a simpler method to accomplish it, which you will discover in the following section.

  1. Terraform may be downloaded from HashiCorp’s download page in the relevant version. It’s the Windows 64-bit version in my instance.
  2. Create a folder on your C: drive where the Terraform executable may be saved. Installers should be placed in a subdirectory (e.g. C:tools) where binaries may be found.
  3. When the download is complete, look for it in File Explorer. Step 2: Extract the zip file to the folder you made earlier.
  4. When you put “environment” into your Start Menu, the first item that appears should be Edit the System Environment Variables. When you click on it, you should see this popup.

System ParametersSystem Parameters

5. Scroll down to Environment Variables… and you’ll see this:

Variables in the Windows environmentVariables in the Windows environment

6. Find one named Path under the System Variables section at the bottom and click modify. You’ll then see a list of locations where Windows could require binaries for various reasons.

7. Add the folder location where terraform.exe is placed to the bottom of the list by clicking New. When you’re done, it should look like this.

Making changes to the system routeMaking changes to the system route

8. Click OK on each of the menus you’ve accessed until you’ve exhausted your options.

9. Open a new CMD/PowerShell prompt and type refreshenv to ensure that Windows recognizes the updated path.

10. Run terraform —version to see whether the installation was successful. You’re fine to go if it returns a version.

The Simple Approach

That was a lot of effort! It would be a nightmare to have to do that every time you needed to update your device’s software. Instead, we’ll utilize a package management. You may install Terraform on Windows using a number of different package managers. Chocolatey is my fave for Windows. Terraform is no exception, since it makes installing, deleting, and upgrading software as easy as typing a single command.

Follow these steps to install Terraform with Chocolatey:

  1. Install Chocolatey using the command from their install page by opening a CMD/PowerShell prompt as an administrator.
  2. Run choco install terraform once that’s done. You may also add -y at the end to have it automatically agree to install on your device.

When the command completes, you should see something similar to this:

v0.10.13 of Chocolatey There were two validations carried out. 1 success(es), 1 warning(s), and 0 error(s) were recorded (s). The following packages should be installed: terraform By installing, you agree to the package licensing. Progress: Terraform 0.12.6 is now available for download… 100% Terraform v0.12.6 [Approved] Terraform 64 bit is being downloaded from ‘https://releases.hashicorp.com/terraform/0.12.6/terraform 0.12.6 windows/terraform 0.12.6 windows/terraform 0.12.6 windows/terraform 0.12.6 windows/terraform 0.12.6 windows/terraform_ Terraform 0.12.6 windows amd64.zip (15.32 MB) has been downloaded. —SNIP —

3. Run terraform —version to see whether the installation was successful.

The Linux Way of Life

I nearly get a sense of what you’re thinking. Please wait a moment. Chris, didn’t you mention this was going to be about Terraform installation on Windows?

Yes, and it is still the case. The Windows for Linux Subsystem, or WSL, is one of Windows 10’s features. This enables you to execute Linux commands from inside Windows.

  1. WSL should be installed. I won’t go through how to install and set up WSL in detail here, but if you want to follow along and don’t already have it set up, go here. Below is a TechSnips snip I made on this subject.

 

2. Run apt-get install unzip in your WSL shell. You’ll need this later to extract the Terraform binaries.

3. Run wget https://releases.hashicorp.com/terraform/0.12.6/terraform 0.12.6 linux amd64.zip to download Terraform. Replace the version and architecture with the ones that are most appropriate for your device. The complete list of Terraform Releases may be seen here.

4. Unzip the contents of the zip file into a folder named terraform using the command unzip terraform 0.12.6 linux amd64.zip terraform.

5. After you’ve uncompressed the ZIP file, you’ll need to relocate it to a location where the system path can access it. Fortunately, Linux has a default folder to which users may add binaries. mv terraform /usr/local/bin/ will move the Terraform binary there. Your system path already includes the /usr/local/bin folder.

6. Run terraform —version to see whether the installation was successful.

AWS Terraform EC2 Instance Automation

You may start using Terraform now that you’ve installed it. In this piece, I’ll show you how to create a small AWS Terraform EC2 instance. Terraform, on the other hand, can create hundreds of distinct kinds of infrastructure.

To begin, you’ll need a directory from which to execute the Terraform EC2 scripts. It’s critical to keep each environment in its own directory. Terraform does not have a means to filter what it runs at the moment I’m writing this. Every script in your current working directory will be executed.

Constructing a TF Script

  1. Run mkdir hello-terraform in your cmd or PowerShell prompt as administrator, then cd./hello-terraform.
  2. You’ll need a Terraform script once you’ve created a directory. You may see an example of one below. This is a typical Terraform script that creates an AWS instance resource using the AWS provider.

Copy the complete script below if you want it. Continue reading if you want to know what this script is doing.

# Terraform HCL provider “aws” region = “us-east-1” shared credentials file = “/.aws/credentials” profile = “default” # Terraform HCL provider “aws” region = “us-east-1” shared credentials file = “/.aws/credentials” “aws instance” “ubuntu” resource instance type = “t2.micro” ami = “ami-07d0cf3af28718ef8” tagging = “HelloTerraform” tagging = “HelloTerraform” tagging = “HelloTerraform” tagging = “HelloT

3. In your working directory, save the script above as main.tf.

TF Scripts: An Overview

Look at the block below to see what it declares before continuing.

#Terraform HCL provider “aws” region = “us-east-1” shared credentials file = “/.aws/credentials” profile = “default” #Terraform HCL provider “aws” region = “us-east-1” shared credentials file = “/.aws/credentials”

This block instructs Terraform EC2 to utilize a certain provider. All of the main cloud companies, as well as certain on-premise vendors, have providers. You can even create your own provider if you’re more sophisticated and know how to code Golang.

This block instructs Terraform to utilize the AWS provider and access keys in the default profile’s /.aws/credentials file. This file is produced when you use the aws config command to set up the AWS CLI as described in the requirements.

It’s also worth mentioning that / refers to the current user’s home directory. This is equivalent to $env:USERPROFILE or percent USERPROFILE percent if you’re coming from a Windows background. However, at the time of writing, Terraform does not support such notation.

The following section explains how to create a Terraform EC2 Instance and what you’ll need to do so. Some options are necessary, while others are optional, according to the Terraform documentation for aws instance. The needed parameters in this case are the AMI ID (ami-07d0cf3af28718ef8) for Ubuntu 18.04 LTS and the instance type t2.micro.

This tag will be added to the instance produced if you supply a tag block with the key Name and the value HelloTerraform. This is an optional step.

“aws instance” “ubuntu” is a Terraform HCL resource. ami = “ami-07d0cf3af28718ef8” instance type = “t2.micro” tags = “HelloTerraform” ami = “ami-07d0cf3af28718ef8” ami = “ami-07d0cf3af28718ef8” ami = “ami-07d0cf3af28718ef8” ami =

Creating an AWS Terraform EC2 Instance: Experimenting

Now that the script is ready, execute init terraform from your working directory to invoke it. This will download all of the requirements and providers into a.terraform folder. If everything goes properly, you should end up with something similar to this:

init terraforminit terraform

Terraform EC2 enables you to examine what will be built before making any modifications by executing terraform plan. This is what you’ll get as a result:

Prior to the plan, I’m refreshing the Terraform state in memory… This plan will be calculated using the refreshed state, but it will not be saved to local or remote state storage. ———————————————————————————————————— The following is a copy of the execution plan that was created. The following symbols are used to signify resource actions: + invent The following activities will be carried out by Terraform: # construct aws instance.ubuntu + resource “aws instance” “ubuntu” + ami = “ami-07d0cf3af28718ef8” + arn = (known after apply) + associate public ip address = (known after apply) + availability zone = (known after apply) + cpu core count = (known after apply) + cpu threads per_ —SNIP— Plan to add 1 item, alter 0 items, and destroy 0 items. ———————————————————————————————————— Note that since you didn’t use the “-out” argument to save this plan, Terraform can’t ensure that these activities will be carried out precisely when “terraform apply” is executed later.

Creating an Amazon Web Services (AWS) EC2 Instance:

You may now try it out for yourself by typing terraform apply. You’ll see results similar to terraform plan below, but with an additional question to indicate that you wish to implement these modifications.

Do you wish to carry out these tasks? The operations stated here will be carried out by Terraform. To approve, just ‘yes’ will be accepted. Fill in the blanks:

When you hit ‘yes,’ Terraform EC2 will begin creating the Terraform EC2 instance by using the access key from your credentials file to contact the AWS APIs. This might take a while. When you’re done, you’ll see something like this:

aws instance.ubuntu: Creating aws instance.ubuntu aws instance.ubuntu: [10s elapsed] Still building… aws instance.ubuntu: [20s elapsed] Still building… aws instance.ubuntu: [30s elapsed] Still building… aws instance.ubuntu: After 33s, the creation of aws instance.ubuntu is complete. [id=i-07cefd2a426a179b5] Complete the application! 1 resource was added, 0 was altered, and 0 was deleted.

Taking down an AWS EC2 instance

You may delete the instance after you’ve finished with the test environment.

Type terraform destroy in your cmd/PowerShell prompt. You’ll see a list of resources Terraform will destroy, followed by a prompt before they’re destroyed, just as with the apply command.

Plan to add 0 items, alter 0 items, and destroy 1 item. Do you truly want to deplete all of the world’s resources? Terraform will, as demonstrated above, destroy all of your controlled infrastructure. There is no going back. To confirm, only ‘yes’ will be accepted. Fill in the blanks:

When you choose yes, Terraform EC2 will begin killing the instance and will notify you when it is complete.

Destroying aws instance.ubuntu [id=i-07cefd2a426a179b5] aws instance.ubuntu: aws instance.ubuntu is still deleting… [id=i-07cefd2a426a179b5, 10s elapsed] [id=i-07cefd2a426a179b5, 10s elapsed] aws instance.ubuntu: aws instance.ubuntu is still deleting… [id=i-07cefd2a426a179b5, 20s elapsed] [id=i-07cefd2a426a179b5, 20s elapsed] aws instance.ubuntu: aws instance.ubuntu is still deleting… [id=i-07cefd2a426a179b5, 30s] [id=i-07cefd2a426a179b5, 30s] [id=i-07cefd2a426a aws instance.ubuntu: After 30s, the destruction is complete. Complete destruction! 1 resource was depleted.

Summary

You now know how to install and run Terraform on Windows in a variety of methods. I explored a single-use example of setting an AWS Terraform EC2 instance in this article. You learned how to read and comprehend a Terraform file, as well as what output Terraform produces when it runs, from that demonstration. You then used your cmd/PowerShell terminal to create and delete the instance.

You should now have a better knowledge of Terraform’s operation and how to get started using it in your own environment. Good luck with your terraforming!

“Add terraform to path windows” is a step-by-step guide on how to get started with Terraform. It includes instructions on how to install and run the tool, as well as information on what Terraform can help you do. Reference: add terraform to path windows.

Related Tags

  • terraform windows provider
  • terraform aws tutorial pdf
  • terraform windows install
  • terraform tutorial
  • terraform example

Table of Content