How to Install Terraform on CentOS 7/Ubuntu 18.04

How to install terraform on centos and ubuntu

Terraform is a software which allows you to manage cloud infrastructure resources from code efficiently. You can use Terraform to build, change, and version infrastructure deployed on proprietary cloud providers or your own infrastructure on premises.

It supports cloud service providers like AWS, Google Cloud Platform, Azure, and many others.

Terraform is distributed as a binary package for all supported platforms and architectures. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed.

In this tutorial we will learn how to:

- install terraform on CentOS 7
- install terraform on Ubuntu 18.04
- write a terraform template script for AWS Cloud (Amazon Web Services)
- execute terraform and build functional cloud infrastructure

Install Terraform on Centos 7

Next, we will see how we can install Terraform on CentOS 7 distribution.

First up, you will need to upgrade your system and packages to current version:

$ sudo yum update

Next, we will install wget and unzip packages if they’re not already installed:

$ sudo yum install wget unzip

Now we are ready to download Terraform zip file for Linux from the official website. At the time of writing this article, the current version of Terraform was 0.11.13.

$ wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip

Next, we will unpack the archive to /usr/local/bin/

$ sudo unzip ./terraform_0.11.13_linux_amd64.zip -d /usr/local/bin/

All done. The only thing that is left now, is to check if terraform is successfully installed, with the following command:

$ terraform -v

Install Terraform on Ubuntu 18.04

To update the system and packages, you can use the built-in software updater, or manually update the system with:

$ sudo apt-get update

Again, we will install wget and unzip packages if they’re not already installed:

$ sudo apt-get install wget unzip

Also next, we will run the same commands as we did with CentOS 7:

$ wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
$ sudo unzip ./terraform_0.11.13_linux_amd64.zip -d /usr/local/bin/

And finally, to test if our installation was successful:

$ terraform -v

Writing a template script file for Terraform

Now, when we have learned how to successfully install Terraform software on both CentOS 7 and Ubuntu 18.04, we can create a template script file and setup infrastructure, for example, on AWS Cloud (Amazon Web Services).

Terraform uses .tf template files to setup desired cloud infrastructure and uses code written in a language called HCL. In this tutorial we will create template file called 'terraform.tf'.

$ nano terraform.tf

Now we must add our credentials to terraform.tf file, setup provider name and instructions on what should terraform do. Input your AWS public and secret key so that it looks like this:

provider "aws" {
  region     = "us-west-2"
  access_key = "accesskey"
  secret_key = "secretkey"
}

resource "aws_instance" "example" {
  ami = "ami-8803e0f0"
  instance_type = "t2.micro"
}

Save the file and proceed to the Terraform initialization.

Next, we must initialize 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.

We are now basically ready to go. Using the terraform plan command we can simulate the process without actually creating anything on AWS:

$ 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.


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

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_instance.example
      id:                           
      ami:                          "ami-8803e0f0"
      arn:                          
      associate_public_ip_address:  
      availability_zone:            
      cpu_core_count:               
      cpu_threads_per_core:         
      ebs_block_device.#:           
      ephemeral_block_device.#:     
      get_password_data:            "false"
      host_id:                      
      instance_state:               
      instance_type:                "t2.micro"
      ipv6_address_count:           
      ipv6_addresses.#:             
      key_name:                     
      network_interface.#:          
      network_interface_id:         
      password_data:                
      placement_group:              
      primary_network_interface_id: 
      private_dns:                  
      private_ip:                   
      public_dns:                   
      public_ip:                    
      root_block_device.#:          
      security_groups.#:            
      source_dest_check:            "true"
      subnet_id:                    
      tenancy:                      
      volume_tags.%:                
      vpc_security_group_ids.#:     


Plan: 1 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.

Executing Terraform to create instance on AWS

If we are satisfied with the current tested plan, we execute terraform apply to actually create our infrastructure on AWS:

$ terraform apply

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_instance.example
      id:                           
      ami:                          "ami-8803e0f0"
      arn:                          
      associate_public_ip_address:  
...

You will be asked to confirm the current action by typing “yes”:

Plan: 1 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

After a few minutes the instance will be created and running:

...
  source_dest_check:            "" => "true"
  subnet_id:                    "" => ""
  tenancy:                      "" => ""
  volume_tags.%:                "" => ""
  vpc_security_group_ids.#:     "" => ""
aws_instance.example: Still creating... (10s elapsed)
aws_instance.example: Still creating... (20s elapsed)
aws_instance.example: Still creating... (30s elapsed)
aws_instance.example: Still creating... (40s elapsed)
aws_instance.example: Creation complete after 43s (ID: i-07977d913a7264459)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

As you can see when you open AWS console there’s our instance up and running:

aws instance terrraform

 

Note that Terraform remembers the state of your infrastructure, so that if you change the plan and apply again it will update your existing cloud infrastructure.

The last thing we are left to do, is to see how we can terminate and remove our plan. It is a very simple action with terraform destroy command (you will be asked again to confirm your action):

$ terraform destroy
aws_instance.example: Refreshing state... (ID: i-07977d913a7264459)

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_instance.example


Plan: 0 to add, 0 to change, 1 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

aws_instance.example: Destroying... (ID: i-07977d913a7264459)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 10s elapsed)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 20s elapsed)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 30s elapsed)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 40s elapsed)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 50s elapsed)
aws_instance.example: Still destroying... (ID: i-07977d913a7264459, 1m0s elapsed)
aws_instance.example: Destruction complete after 1m6s

Destroy complete! Resources: 1 destroyed.

And that is it for this tutorial. We learned how to install Terraform on CentOS 7 and Ubuntu 18.04, make a template file, create and destroy instance on AWS.

I hope this info helps some of you, and also feel free to post your comments and questions below.

Read Also:

Slavisa Milojkovic 12:05 am

About Slavisa Milojkovic

Slaviša is a long time Linux user and system administrator. He was PCLinuxOS Gnome distribution developer and maintainer for several years and active opensource supporter.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.