【Terraform】从编写代码到应用于云环境的过程
首先
我想写一篇关于如何使用Terraform编写代码并将其应用于云环境(AWS环境)的流程的文章。
本次将省略有关Terraform代码编写和环境设置的准备工作。我们将专注于在云环境中应用Terraform代码的流程。
将代码应用于云环境的流程
我們將從準備Terraform代碼開始到應用代碼內容的整個過程整理為一個流程。

操作流程
代码准备
准备Terraform代码。
※本次使用的代码如下。
ec2创建代码
以下是使用此代码构建的AWS配置。
(此代码是为了连接AmazonLinux2而创建的)

工作区初始化
执行以下命令以初始化包含Terraform配置文件的工作目录。
terraform init
这个命令在以下情况下执行。(可重复执行)
-
- 新しいTerraformの設定を作成する場合
- GitHub等のバージョン管理ツールからTerraformのコードをcloneする場合
实际操作的例子如下。
% terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed hashicorp/aws v4.22.0
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配置文件的格式,执行以下命令。
terraform fmt
执行此命令后,您可以使代码整洁且易于阅读。
假设有一段被弄乱的Terraform代码,如下所示。
resource "aws_key_pair" "keypair" {
key_name = "${var.project}-${var.environment}-keypeir"
public_key = file("./src/id_test_key.pub")
tags = {
Name = "${var.project}-${var.environment}-keypair"
Project = var.project
Env = var.environment
}
}
运行terraform fmt会自动为您格式化,如下所示。
resource "aws_key_pair" "keypair" {
key_name = "${var.project}-${var.environment}-keypeir"
public_key = file("./src/id_test_key.pub")
tags = {
Name = "${var.project}-${var.environment}-keypair"
Project = var.project
Env = var.environment
}
}
语法检查
为了对Terraform代码进行语法检查,请执行以下命令。
terraform validate
成功进行语法检查后,会如下显示。
% terraform validate
Success! The configuration is valid.
%
如果语法检查失败,则会显示如下内容。
$ terraform validate
╷
│ Error: Reference to undeclared input variable
│
│ on ec2.tf line 9, in resource "aws_key_pair" "keypair":
│ 9: Name = "${var.project}-${var.enviroent}-keypair"
│
│ An input variable with the name "enviroent" has not been declared. Did you mean "environment"?
╵
$
在应用云环境之前的确认
在应用Terraform代码到云环境之前,可以通过执行以下命令来预先确认基础设施配置的更改点。
terraform plan
% terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.server will be created
+ resource "aws_instance" "server" {
+ ami = "ami-02c3627b04781eada"
+ arn = (known after apply)
〜〜〜長いので省略〜〜〜
将其应用到云环境中。
通过执行以下命令,将Terraform代码应用到云环境中。
terraform apply
% terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_instance.server will be created
+ resource "aws_instance" "server" {
〜〜〜省略〜〜〜
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 ← applyを実行する場合は「yes」、実行しない場合は「No」を入力
aws_key_pair.keypair: Creating...
aws_vpc.vpc: Creating...
aws_key_pair.keypair: Creation complete after 0s [id=Test-dev-keypeir]
〜〜〜省略〜〜〜
aws_route_table_association.public_rt: Creation complete after 0s [id=rtbassoc-0dcfc3f95cf89bd84]
aws_instance.server: Still creating... [10s elapsed]
aws_instance.server: Still creating... [20s elapsed]
aws_instance.server: Still creating... [30s elapsed]
aws_instance.server: Creation complete after 32s [id=i-052a91631cc5274c0]
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
%
请按照以下方式进行参考。
-
- Command: init
-
- Command: fmt
-
- Command: validate
-
- Command: plan
- Command: apply