使用Terraform构建EC2实例(安装步骤)
首先
我正在使用AWS,每次从控制台构建环境都很麻烦。
据说使用Terraform这个工具可以快速搭建环境并且轻松清理,所以我尝试了一下。
首先,我们将尝试构建AWS的EC2实例,以进行试用。
在这个安装指南中,我们将解释从安装到初始设置的步骤。
有关实际构建的详细说明,请参考实践指南。
Terraform是什么?
-
- Terraformとは、HashiCorp社製のAWSやGCPなどのクラウド上のリソース管理をする構成管理ツールです。
- HashiCorp社のツールはTerraformの他にもVargrantなどがあり、これらはOSSとして公開されています。
如何使用Terraform
-
- インフラの構成情報を.tfという拡張子のファイルに記述します。
-
- terraformの各コマンドを実行することで、記述したインフラの構成を管理することができます。
- .tfファイルは複数に分かれていても、同じディレクトリに存在すればまとめて適用することが可能。
Terraform的开发流程
使用Terraform开发基础架构的流程如下所示。
-
- 在.tf文件中编写基础设施代码
-
- 使用terraform plan命令与当前状态进行对比
-
- 使用terraform apply命令应用基础设施配置
- 使用terraform show命令确认状态
Terraform安装
请提供下载链接
Terraform 可以从以下链接中下载。
适用于 MacOS 版 (AMD64)、Linux 版 (i386, AMD64)、Windows 版 (i386)。
安装
创建安装目录。
$ mkdir terraform
$ cd terraform
从URL下载并解压缩
$ wget -O terraform_0.11.13_linux_amd64.zip https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip
$ unzip terraform_0.11.13_linux_amd64.zip
请查看目录。
$ tree .
.
├── 0.1.0_linux_amd64.zip
├── terraform
└── terraform_0.11.13_linux_amd64.zip
请尝试执行版本确认的命令。
$ ./terraform --version
Terraform v0.11.13
安装已经完成。如果需要的话,可以将其复制到/usr/bin/等路径下,以方便使用。
初始设置
AWS的預先準備工作
使用Terraform构建AWS环境之前,需要完成以下两个步骤。
创建IAM用户
在使用Terraform管理AWS环境时,需要创建IAM用户并获得Access Key ID和Secret Key,
② 安装 AWS CLI
事先安装好AWSCLI。
可以使用pip install awscli进行安装。
Terraform的初始设置
将IAM等进行注册
如果使用Terraform来管理AWS,就需要注册访问密钥。
在安装了Terraform的目录下创建一个.tf文件,并写入访问密钥。
文件名可以随意。
还要提前指定区域。
provider "aws" {
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
region = "ap-northeast-1"
}
② 进行Terraform初始化
运行terraform init命令来初始化IAM访问密钥。
初始化Terraform的工作目录,并安装用于操作AWS的插件。
运行此命令后,可以执行terraform plan和terraform apply命令。
$ ./terraform init
Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "aws" (2.6.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.6"
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.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
使用terraform plan命令来查看当前状态。
我们执行terraform plan命令来确认当前状态。
显示“没有更改,保持最新状态”的消息。
由于Terraform的配置文件中尚未写入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.
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
结束
由于本次是安装部分,所以到此为止。
下次我打算亲自使用Terraform来创建EC2实例。