如何在CentOS 7/Ubuntu 18.04上安装Terraform

时间:2020-03-05 15:24:40  来源:igfitidea点击:

Terraform是一种软件,允许我们有效地管理代码的云基础架构资源。
我们可以使用Terraform来构建,更改和版本在专业云提供商或者我们自己的基础架构上部署的基础架构。

它支持像AWS,Google Cloud Platform,Azure等许多其他云服务提供商。

Terraform被分发为所有支持的平台和架构的二进制包。
Terraform作为单个二进制名为Terraform运行。
包中的任何其他文件都可以安全地删除。

在CentOS 7上安装Terraform

接下来,我们将看到我们如何在CentOS 7分发上安装Terraform。

首先,我们需要将系统和包升级到当前版本:

$sudo yum update

接下来,如果他们尚未安装,我们将安装WGET和UNZIP软件包:

$sudo yum install wget unzip

现在我们已准备好向官方下载用于Linux的Terraform Zip文件。

在撰写本文时,当前版本的Terraform为0.11.13.

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

接下来,我们将解压缩存档到/usr/local/bin /

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

全部做完。
现在唯一留下的是检查TerraForm是否已成功安装,以下命令:

$terraform -v

在Ubuntu 18.04上安装Terraform

要更新系统和包,我们可以使用内置软件更新程序,或者手动更新系统:

$sudo apt-get update

再次,如果它们尚未安装,我们将安装WGET和UNZIP软件包:

$sudo apt-get install wget unzip

另外,我们将使用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/

最后,要测试我们的安装是否成功:

$terraform -v

为Terraform编写模板脚本文件

现在,当我们学习了如何在CentOS 7和Ubuntu 18.04上成功安装Terraform软件时,我们可以创建模板脚本文件和设置基础结构,例如AWS Cloud(Amazon Web Services)。

Terraform使用.tf模板文件来设置所需的云基础架构,并使用以名为HCL的语言编写的代码。
在本教程中,我们将创建名为'terraform.tf'的模板文件。

$nano terraform.tf

现在,我们必须将凭据添加到Terraform.tf文件,设置提供商名称和关于Terraform的说明。
输入AWS公共和密钥,使其如下所示:

provider "aws" {
  region     = "us-west-2"
  access_key = "accesskey"
  secret_key = "secretkey"
}
resource "aws_instance" "example" {
  ami = "ami-8803e0f0"
  instance_type = "t2.micro"
}

保存文件并继续执行Terraform初始化。

接下来,我们必须初始化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 Jan 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 Jan 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.

我们现在基本上准备好了。
使用"Terraform Plan"命令我们可以模拟此过程而无需实际在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.

执行Terraform以在AWS上创建实例

如果我们对当前测试计划感到满意,我们会执行"Terraform"适用于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:  
...

将要求我们通过键入"是"来确认当前操作:

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

几分钟后,将创建和运行实例:

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

正如我们在打开AWS控制台时,我们可以看到我们的实例和运行:

请注意,Terraform记住基础架构状态,因此如果我们更改计划并再次应用,它将更新我们现有的云基础架构。

我们剩下的最后一件事是看看我们如何终止和删除我们的计划。
这是一个非常简单的行动与terraform destroy命令(我们将再次被要求确认行为):

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