Terraform infrastructure provisioning -Infra as Code Part 1

ComputerCarriage > Posts > Automation > Terraform infrastructure provisioning -Infra as Code Part 1
Terraform infrastructure provisioning

Terraform Introduction

Automating infrastructure as a code is ease by using terraform infrastructure provisioning, we can use terraform to build servers as Infrastructure as a code, We can build easily using terraform in AWS, Azure, VMWare environments easily with simple codes. This will help us to reduce work load.

There are lot of tools in market like general purpose continuous integration , but terraform allows us to make more efficient and convenient and powerful. create resources on-demand is the main feature.

Terraform binary is free to download, using terraform cli to provision the infrastructure. Terraform runs on cloud and on virtual environments, some time referred as Remote operations.

Terraform stores configuration in local or present working directory , which contains state data and variables. remote execution has advantages to developers to write their code and test before executing the actual environment

For example terraform plan will check interactively check their plan and in case of any issues we can correct the errors before applying the actual environment up and running, integrate terraform with simple steps to use.

Terraform Workflow

  • Scope – Decide what are requirements to bring up the infrastructure
  • Author – Create config based on resources that are required, like CPU, Memory, HDD Size, Network
  • Initialize – Execute terraform init to initialize the project
  • Plan – After creating the configuration execute terraform plan for a dry run
  • Apply – Execute terraform apply for the final execution

Terraform advantages

  • Platform Agnostic – Support various platforms and applications. creating config according to ur needs
  • State Management – Create plans and dry run before the actual execution compare the changes with previous state of the resources, suppose earlier we have two servers now we are adding one more it will show the changes that going to affect with the current changes
  • Operator Confidence – while executing any plan or changes by using terraform apply it will prompt for the proposed changes to apply.

Terraform Opensoruce

Terraform is open source means we can use it for all, terraform supports most of the operating system like Linux, BSD, Windows, Solaris, OSX

Terraform infrastructure provisioning is useful for bringing up any kind of virtual or cloud environments

Download Terraform

macOS Version

https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_darwin_amd64.zip

Linux Version

32-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_386.zip

64-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_amd64.zip

Arm — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_linux_arm.zip

Solaris

64-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_solaris_amd64.zip

Free BSD

32–bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_freebsd_386.zip

64-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_freebsd_amd64.zip

Arm — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_freebsd_arm.zip

Open BSD

32-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_openbsd_386.zip

64-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_openbsd_amd64.zip

Windows

32-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_windows_386.zip

64-bit — https://releases.hashicorp.com/terraform/0.13.2/terraform_0.13.2_windows_amd64.zip

How to Install terraform

On UNIX based operating system

mv ~/Downloads/terraform /usr/local/bin/

Verify the installation

$ terraform
Usage: terraform [-version] [-help] [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management

Simple terraform Code

Simple code to check the terraform execution, Lets consider we have installed docker on the local machine, Just copy and paste the code and save the file as main.tf

terraform {
required_providers {
docker = {
source = "terraform-providers/docker"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}

Initialize the terraform and make a dry run

$ terraform init
$ terraform plan

in the above code we are provisioning the nginx container, execute terraform apply and prompted yes to continue

Execute the code by apply

$ terraform apply

We could see the nginx is downloaded and installed on the conainter engine, Verify by executing docker ps

$ docker ps

Test the service by curl command or any browser

$ curl http://localhost:8080

To stop the container, just execute terraform destroy

$ terraform destroy

Now we can see the container is destroyed

Conclusion

In this chapter we see basic features of terraform and advantages, in upcoming posts we will go in detail like how we can bring up using terraform in VMWare, AWS, Azure and Google Cloud, hope you had some basic idea of using the Terraform infrastructure provisioning. let us know your comments. Thank you

Refer: Docker container platform — https://computercarriage.com/2020/09/03/docker-container-platform/

Leave a Reply