Terraform 是一種軟件,可讓您有效地從代碼管理雲基礎設施資源。 您可以使用 Terraform 構建、更改和版本化部署在專有云提供商或您自己的本地基礎設施上的基礎設施。
它支持 AWS、Google 等雲服務提供商 Cloud Platform、Azure 等。
Terraform 以二進制包的形式分發給所有支持的平台和架構。 Terraform 作為名為 terraform 的單個二進製文件運行。 包中的任何其他文件都可以安全刪除。
在本教程中,我們將學習如何:
– 在 CentOS 7 上安裝 terraform
– 在 Ubuntu 18.04 上安裝 terraform
– 為 AWS 編寫 terraform 模板腳本 Cloud (亞馬遜網絡服務)
– 執行 terraform 並構建功能性雲基礎設施
在 Centos 7 上安裝 Terraform
接下來,我們將看到如何在 CentOS 7 發行版上安裝 Terraform。
首先,您需要將系統和軟件包升級到當前版本:
$ sudo yum update
接下來,如果尚未安裝 wget 和解壓縮包,我們將安裝它們:
$ sudo yum install wget unzip
現在我們準備好了 下載地形 從官方網站下載適用於 Linux 的 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 和解壓縮包,我們將安裝它們:
$ 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 軟件後,我們可以創建模板腳本文件並設置基礎架構,用於 example, 在 AWS 上 Cloud (亞馬遜網絡服務)。
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"
}
Save 文件並繼續進行 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 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.
我們現在基本上準備好了。 使用 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 apply
在 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.
這就是本教程的內容。 我們學習瞭如何在 CentOS 7 和 Ubuntu 18.04 上安裝 Terraform、製作模板文件、在 AWS 上創建和銷毀實例。
我希望這些信息對你們中的一些人有所幫助,也可以隨時在下面發表您的評論和問題。
另請閱讀:
- 如何安裝 Terraform 和配置 AWS EC2 Cloud 實例