2. 基本 Terraform 使用

01 为 Terraform 项目创建一个目录:

$mkdir aws-sample && cd aws-sample

02 Terraform 使用 .tf配置文件。
因此,创建一个文件如下:

$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"
         }
}

在上面的 Terraform 示例中,我们将在 AWS 上创建一个 Ubuntu EC2 实例。
因此,将 region 、 access_key 和 secret_key 替换为我们自己的 AWS 区域和密钥。

03 现在,让我们使用以下命令初始化 Terraform:

$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 之后,我们可以执行以下命令来检查配置是否在语法上有效:

$terraform validate
Success! The configuration is valid.

05 我们现在基本上准备好了。
使用 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 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 最后,让我们应用 Terraform 配置在 AWS 上创建我们的 EC2 实例:

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

我们将被要求通过输入“是”来批准,片刻之后,一个 AWS 实例将运行。
我们可以检查 AWS 控制台并验证实例

05 我们需要知道的最后一件事是,我们如何终止和删除我们的计划。
只需运行以下命令,我们将被要求符合操作,只需键入“是”:

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

1. 在 CentOS 服务器上安装 Terraform

1 首先,如果尚未安装 wgetunzip包,请安装它们:

$sudo yum update && sudo yum install wget unzip -y # CENTOS 7
$sudo dnf update && sudo dnf install wget unzip -y # CENTOS 8

02 到Terraform官网下载最新版本的Terraform:

$VERSION=0.12.25
$sudo wget https://releases.hashicorp.com/terraform/${VERSION}/terraform_${VERSION}_linux_amd64.zip -P /tmp

03 现在,将 Terraform zip 文件解压缩到目录 /usr/local/bin/中,如下所示:

$sudo unzip /tmp/terraform_${VERSION}_linux_amd64.zip -d /usr/local/bin/

04 完成后,要验证 Terraform 是否已正确安装,请运行以下命令:

$terraform -v
Terraform v0.12.25
如何在 Centos 上安装 Terraform

Terraform 是一个开源的“基础设施即代码”工具,由 HashiCorp 创建。
我们可以使用 Terraform 构建、更改和版本化部署在专有云提供商或者我们自己的内部基础设施上的基础设施。
Terraform 支持 AWS、Google Cloud Platform、Azure 等云服务提供商。

在本文中,我们将带我们完成在 Centos/rhel 服务器上安装最新版本 Terraform 的步骤。
此外,我们将展示一些基本的 Terraform 用法,例如在 AWS 上创建 EC2 实例。

日期:2020-06-02 22:18:49 来源:oir作者:oir