Installing Required Software

A quick word: I am using Windows, and so installation steps will of course vary depending on your OS of choice.

Also, this tutorial assumes you:

1: have an AWS account set up with functional user access codes to use alongside the AWS CLI command aws configure. 2: have installed VS Code and enabled its useage in the Windows CLI.

To install Terraform on your Windows CLI, here to Terraform’s download section. Here, install whichever version you want (I chose Amd64), unpack the .zip folder into a directory of your choice, and extract the file.

Copy-paste the .exe file from this zip file into a directory that is easy to access, such as your C:\ drive as shown below. Copy the file path as shown, as well. pictureofterraformdirectory

Next, to ensure that this executable is considered a usable environment variable, type env in your Windows search bar and click Edit the system environment variables. Click environment variables and in the bottom section labled System variables, click Path, Edit, New, and paste the C:\Terraform directory (if that’s where you made your Terraform folder like I did ) into the box that appears at the bottom. pictureOfEditENV

Click OK on all the boxes that you just opened to finalize these changes, then type terraform -v to display your terraform version to make sure that you set up everything correctly. If it doesn’t show the version, you might need to check that you setup the environment variables correctly one more time.

Thankfully setting up the Amazon CLI is much easier. In your command prompt window, copy-paste this command:

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

to run the install wizard that will do the dirty work for you. First CLOSE ALL of your command prompts, and then open a new command prompt and type aws –version to ensure that the wizard installed AWS correctly.

In a Windows command prompt window, type aws confgiure and enter the access key and secret key information for your AWS user account that you wish to manage EC2 Instances from. For security concerns, I will not be sharing screenshots of what this looks like from the CLI, as these keys are sensitive information!

(Note: unless you configure it to work with VS Code’s terminal, which is outside the scope of this tutorial, AWS commands will not function inside VS Code, so use a Windows command prompt for simplicity’s sake.)

Moving to Terraform

Now that you have configured your AWS account and installed the AWS CLI and Terraform, we can begin creating instances with Terraform. Create a folder to hold your terraform project inside of, and change your working directory to be inside of that folder. From this folder we can begin to use VS Code to make our main.tf file, which for now, will be the only file we need to create our very first instance on AWS.

Let’s start with a pretty standard setup here and break it down piece by piece. standardsetup.

There are a few crucial aspects of the picture above that I want to make clear. This main.tf file resides inside our C drive in a folder called TerraformProj1, which I created off-screen. I used the command code main.tf to create a file in this folder called main.tf.

Inside the main.tf file is the code required by Terraform to create an EC2 instance. It is all encapsulated inside the terraform block, which has a few subcategories.

required_providers

This block is actually not necessary, but according to hashicorp’s own documentation, its creation is recommended. Let’s say you did not specify a version for AWS in this block. While setting up your infrastructure, it is possible that Terraform/AWS version mismatches could occur, and these could be nightmares to troubleshoot if you are a humble Terraform beginner. (like myself!) Thus, we set the version to 3.27 here in the required_providers block.

Provider

The provider block includes the providers we wish to provision resources from / within. Providers are simply plugins that Terraform uses to provision and manage your specified resources. The profile attribute in the aws provider block being default means that when trying to connect to an AWS account to provision resources, the AWS credentials stored in your AWS configuration files on this machine will be used. Remember back when we used the console command aws configure to enter our access key and secret key? That is coming in handy now!

This is important: never store your AWS access keys directly inside (never hard code) your terraform files. If you share these configuration files with others one day, you are opening your AWS account up for easy access to all kinds of vulnerabilities. Better to avoid that by never storing secret information inside the main file to begin with!

Resource

The resource block is where the magic happens. Resource blocks have two values enclosed in double quotes: the first being the resource TYPE, and the second being the resource NAME. In my example I am making an aws EC2 instance (“aws instance”) which I am referring to as teamred app server (“teamred app server”) which in AWS combine into an EC2 instance of aws_instance.teamred_app_server. Simple, right?

Well as it turns out, you need to make sure of a few things. When you are creating instances in AWS, you MUST ensure that the region you select matches the region you specified in the provider block when creating AMIs. In my provider block our region is set to us-west-2, and in AWS, as you an see blow, us-west-2 is Oregon. So, when grabbing AMI ID’s from AWS to provision EC2 instances, you MUST set your region to Oregon if you plan to Terraform using the us-west-2 region. The AMI ID’s change depending on what region you have selected while in AWS! itChanges imageOfInstances

See the AMI highlighted in orange above? That is the ubuntu server 20.04. It is CRUCIAL that you make sure the region in your account lines up with the Terraform region you coded. ami-0892d3c7ee96c0bf7 is the only acceptable AMI ID to create an Ubuntu Server 20.04 in a Terraform that has its provider region set to us-west-2. And of course you can set the instance type and name in the resource block. (to determine which instance type is faster or slower depending on your needs, check AWS for your own use case!)

Wrapping up and Running Terraform

Whenever you create a new configuration or modify a pre-existing configuration to update your resources using Terraform, you MUST use terraform init in your command prompt to initialize your configuration directory. It will download and install the providers defined in the provider block once more, which was the AWS provider.

Terraform also includes formatting tools if you installed the VS Code extension for Terraform correctly. Type terraform fmt to format your file in a consistent and Terraform-approved spacing + syntax, and if you’re really paranoid, type terraform validate to ensure that everything you just typed is in the correct Terraform-approved syntax.

It is also considered a best practice to first run terraform plan before applying Terraform. This is an extremely useful command. It will list any additions, changes or removals (destructions) from your existing instances, if there are any. Very handy to make sure that what you are doing is actually what you want to be doing before you actually commit to changing your infrastructure in any meaningful way.

If all goes well, type terraform apply and Terraform will spin up your instance.

This has already become quite long, so I will continue this Terraform learning in another post in the near future. Happy terraform…ing?