Introduction
Terraform is a tool that creates a single provisioned interface for several different cloud-based services. It is sometimes referred to as an “Infrastructure as Code” tool because it uses a configuration file to manage resources.
In this tutorial, learn how to install Terraform on an Ubuntu 18.04 or CentOS 7 system.
Prerequisites
- A user account with sudo privileges
- A terminal window/command line (Ctrl-Alt-T or Ctrl-Alt-F2)
How to Install Terraform on Ubuntu 18.04
Before you download Terraform, update the repository lists:
sudo apt-get update
If your system does not have the wget and unzip utilities, use the following to install them:
sudo apt-get install wget unzip
Run the wget utility to download Terraform:
sudo wget https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip
This is an example of the output:
Once the download finishes, extract the files:
sudo unzip ./ terraform_0.12.2_linux_amd64.zip –d /usr/local/bin
Next, verify that Terraform accepts commands:
terraform –v
The output should display Terraform v.0.12.2.
How to Install Terraform on CentOS 7
To install Terraform on CentOS, start by updating the repository lists:
sudo yum update
You’ll need wget and unzip – if you don’t have them, install them by entering:
sudo yum install wget unzip
Download Terraform from the developer’s website:
sudo wget https://releases.hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip
Extract the downloaded file:
sudo unzip ./terraform_0.12.2_linux_amd64.zip –d /usr/local/bin
The output confirms that the files are now located in the /usr/local/bin
directory.
Lastly, verify that Terraform accepts commands:
terraform –v
The system should display Terraform v.0.12.2.
Note: At the time this article was written, the latest version was Terraform 0.12.2. For a later version or a 32-bit version, please see the developer’s download page.
Basic Terraform Usage Tutorial
Terraform is a tool used to manage datacenter infrastructure. That typically means providing access to cloud services like Azure, Amazon Web Services, and so forth. This tutorial will use AWS as an example.
Create a Directory and Configuration File
Enter the following:
mkdir sample
cd sample
Terraform uses a .tf configuration file. Create and edit one by entering:
sudo nano test.tf
Enter the following:
provider "aws" {
region = "us-west-2"
access_key = "access_key"
secret_key = "secret_key"
}
Replace access_key and secret_key with your own AWS keys.
How to Initialize Terraform
To initialize Terraform, enter the following:
terraform init
Terraform then creates its working directory. Because we specified AWS, it automatically downloaded the AWS provider information to the .terraform directory.
At this point, if you have an actual AWS account, you can add those instructions to Terraform. Use a text editor (like nano) to edit the test.tf file and configure your system.
Create a Terraform Plan
You can create a test plan without actually connecting to the AWS service. This is helpful to get a feel for how Terraform works. Enter the following:
terraform plan
The system displays an overview of the provisioning it intends to create based on the data in the test.tf file.
Please refer to the documentation for specific entries in this file.
Note: Terraform can be configured to grant access to multiple cloud providers all at once. Just use your terraform .tf
file to specify each one you want to include. This creates a simple and centralized method for granting a user access to a custom corporate infrastructure.
Connect Terraform to AWS Services
This step implements the plan we created in the previous step. With the proper data, we will connect to Amazon Web Services using the information in your test.tf file. Enter the following:
terraform apply
The system tells you what will be created, then prompt you to type yes.
After a few moments, an AWS instance will be running. You may check your AWS console and verify the instance. You can also change the plan by editing the test.tf file. When you run the terraform apply
function, it updates the cloud infrastructure.
Destroy the AWS Instance
To remove all the provisioning that was created, enter the following:
terraform destroy
The system will list the actions taken, and prompt you to enter yes to confirm.
Conclusion
In this guide, we covered how to install Terraform on CentOS 7 and Ubuntu 18.04. We also covered making a template file, connecting to AWS services, and destroying an instance on AWS.
Check out our article comparing Ansible vs Terraform vs Puppet.
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/223960.html