Terraform is an open source “Infrastructure as Code” tool, created by HashiCorp. You can use Terraform to build, change, and version infrastructure deployed on proprietary cloud providers or your own infrastructure on premises. Terraform supports cloud service providers like AWS, Google Cloud Platform, Azure, and many others.
In this article, we will take you through the steps to install the latest version of Terraform on Centos / rhel servers. Also, we will show you some basic Terraform Usage like creating an EC2 instance on AWS.
1./ Install Terraform on CentOS Server
1- First, install wget
and unzip
packages if they’re not already installed:
$ sudo yum update && sudo yum install wget unzip -y # CENTOS 7 $ sudo dnf update && sudo dnf install wget unzip -y # CENTOS 8
02- Go to the Terraform official website to download the latest version of Terraform:
$ VERSION=0.12.25 $ sudo wget https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip -P /tmp
03- Now, Unzip the Terraform zip file into the directory /usr/local/bin/
as below:
$ sudo unzip /tmp/terraform_${VERSION}_linux_amd64.zip -d /usr/local/bin/
04- Once you done, to verify that Terraform is installed properly, run the below command:
$ terraform -v Terraform v0.12.25
2./ Basic Terraform Usage
01- Create a directory for your Terraform project:
$ mkdir aws-sample && cd aws-sample
02- Terraform uses a .tf
configuration files. So, Create a file as below:
$ vi test.tf provider "aws" { region = "eu-south-1" access_key = "YOUR_ACCESS_KEY" secret_key = "YOUR_SECRET_KEY" } resource "aws_instance" "example" { ami = "ami-0b74d52f736d963d1" instance_type = "t3.micro" tags = { Name = "terraform-example" } }
03- Now, let’s initialize Terraform by using the below command:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "aws" (hashicorp/aws) 2.62.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.62"
Terraform has been successfully initialized!
04- After, we can execute the following command to check if the configuration is syntactically valid:
$ terraform validate
Success! The configuration is valid.
05- 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 will be created + resource "aws_instance" "example" { + ami = "ami-0b74d52f736d963d1" + 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_core = (known after apply) + get_password_data = false + host_id = (known after apply) + id = (known after apply) + instance_state = (known after apply) + instance_type = "t3.micro" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + network_interface_id = (known after apply) + outpost_arn = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = (known after apply) + tags = { + "Name" = "terraform-example" } + tenancy = (known after apply) + volume_tags = (known after apply) + vpc_security_group_ids = (known after apply) [...] } 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.
05- Finally, let’s apply the Terraform configuration to create the our EC2 instance on AWS:
$ terraform apply [...] 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 aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 13s [id=i-0642a9e23bd084313] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
– you will be asked to approve by typing yes
, After a few moments, an AWS instance will be running. You may check your AWS console and verify the instance
05- Last thing we need to know is, how we can terminate and remove our plan. just run the below command and you will be asked to conform the actions just type yes
:
$ terraform 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-0642a9e23bd084313]
aws_instance.example: Still destroying... [id=i-0642a9e23bd084313, 10s elapsed]
aws_instance.example: Still destroying... [id=i-0642a9e23bd084313, 20s elapsed]
aws_instance.example: Destruction complete after 30s
Destroy complete! Resources: 1 destroyed.
Conclusion
You have successfully installed Terraform on Centos / Rhel servers and have learnt to use terraform to create an EC2 instance on aws. There are a lot of things you might need to know about Terraform, so check the documentation Terraform website.
You might want to check the following guides: