Terraform Basics 101: Part 1

Infrastructure as Code (IaC)

Rather than manually set coding environments and needs, we can code that in shell

In this way, we can code, manage and even destroy our resources. As you can see in the left part, there are lots of lines of codes

Therefore, we have Ansible and Terraform to code that easily

On ansible we use yaml file and on terraform we use a configuration file

There are other tools like docker, cloudformation, packer etc used for IaC.

They are classified into Configuration management, server template, provisioning tools

Configuration management

Used to install and manage software on existing infrastructure resources like servers. databases, networking devices etc. These are designed to run on multiple remote resources at once. Also they are idempotent meaning every time we run it, it will make changes that are necessary to bring the environment into a defined state.

Server templating tools

These are used to create custom image of a virtual machine or a container. These images already contain required software dependencies installed on them.Most common examples for server templated images are VM Images such as Amazon AWS, Docker Images, DockerHub etc.

Provisioning Tools

They deploy immutable Infrastructure resources. Can work with multiple providers using plugins. Mostly used tool is Terraform

Terraform

It can easily deploy resources across multiple private and public cloud.

Terraform does through providers and that enables terraform to manage

Cloud platforms like AWS, GCP, Azure etc;

Network infrastructure like BIgIP, CLoudFlare, DNS, PO ALto , INfoBlox;

Monitoring and data management tools like DataDog, Grafana, Auth0, wavefront etc.

Databases like influxDB, MongoDB etc.

Version control systems like GitHub, Gitlab, bitbucket etc.

Terraform uses HCL whcih is simple, declarative language to define the infrastructure resources to be provisioned as blocks of code

Then the code in initialized, plans what to do and apply changes to make those resources in cloud provider

HCL (Hashicord Configuration Language)

HCL follows this format

Assume that, we want to create a pets.txt file within /root folder. So, we can first create /root/terraform-local-file and get in.

once done, we can create a file called local.tf where we mention that, we want to create a resource named pet on the local system (local_file) and the file name would be pet.txt and the content is “We love pets!”

In the same way, we can provision an AWS EC2 instance

In the same way, we can provision AWS S3 bucket

But what to do after the file creation?

First write the init command . During the process the local provider is created . We surely mention to use local provider and file resource type.

You cam see hashicorp/local of version v1.4.0 has been created.

and then we need to review the plan

and finally apply the changes.

We can also use terraform show command to see the details of what we created

In the last example we used local provider (local_file). But other than this, we have hundreds of providers specially AWS, GCP, Azure etc. Here is an example showing AWS and GCP as a provider and resource type and arguments below

Checkout the list of providers

Update and destroy resources using Terraform

Let’s update the last file

Then check terraform plan

So, here we see that local_file .pet will be replaced, -/+ means that the file will be deleted and then created again with forced replacement.

Once confirmed, we can apply changes

You can see the file was deleted and then was created.

Note: We previously had file permission set by default 0777 (read, write, & execute for owner, group and others). But we have changed it to 700 which means you can do anything with the file or directory and other users have no access to it at all.

So, if we check the file permission, we can see that we have read,write and execution access but others can’t access it

Just for a reference, check this

To delete all of the resource in the current configuration directory, use this command

Let’s do some hands-on:

Let’s create a main.tf file

Then initialized it

Then we checked the plan

Then we applied the changes

and wrote yes

So, this is our text file which was created

Let’s check it’s permission

The current permission is -rwxrwxr-x (0777)

Let’s change it to 0700

I have changed the tf file

Then I have initialized it

Checked the plan

Here, the file is replaced, and it’s firstly deleted and created again. Also the part that’s changing is file permission from 0777 to 0700.

Then I have applied the changes

Here you can see the local_file.games was deleted first and then created again with new permissions

Let’s check the permission of the file.

Now the permission is set to -rwx------ (0700)

Great!!

Let’s destroy the resource now

The games.txt file is gone now!!

Check out the repository for the code