我用Terraform创建了一个带有负载均衡器(LB)和两个虚拟机(VM)的设置

首先

我打算尽快掌握Terraform,所以上网查资料…结果发现,其实查看官方网站是获得最新参考信息的最佳途径,于是我留下了当时的笔记。

以下是我参考的网站

参考资料,没有它就进退两难

 

试着运行了tf.

 

操作步骤

提前假设Terraform已被安装

打开VS Code并使用git clone命令进行克隆!

因为尝试获取特定的源代码没有成功,所以干脆直接下载整个官方公开的代码库。

 

请转到示范目录。

cd terraform-provider-azurerm/examples/virtual-machines/linux/load-balanced

修正main.tf

根据terraform plan执行时输出的错误消息和参考资料,我发现了几个错误并进行了修正。

image.png
image.png

执行

我按照以下步骤顺利完成了。

首先,因为需要输入,所以我把这部分也放在一起。

$ terraform init
$ terraform plan -out plan.tfplan
var.location
  The Azure Region in which all resources in this example should be created.

  Enter a value: japaneast

var.prefix
  The prefix which should be used for all resources in this example

  Enter a value: dev
$terraform apply -input plan.tfplan

执行结果

image.png

VMサイズの変更方法を教えて下さい。

经过上述的 “terraform apply”,我们成功部署了。不过,之后我稍微查了一下如何调整已部署的虚拟机大小,结果只需要简单修改 tf 文件的一部分就可以了,真厉害!

image.png

简而言之,只要再做一次同样的事情就可以了。

$ terraform init
$ terraform plan -out plan.tfplan
var.location
  The Azure Region in which all resources in this example should be created.

  Enter a value: japaneast

var.prefix
  The prefix which should be used for all resources in this example

  Enter a value: dev
$terraform apply -input plan.tfplan

真的非常简单!IaC是最棒的。
顺便说一下…花费的时间与使用az cli进行模型更改时并没有差别。
当然是这样,这是当然的事情。

清理

执行以下命令

terraform plan -destroy -out main.destroy.tfplan
terraform apply main.destroy.tfplan

顺便提一下,今次只是试一试,随便修改了一下模型部分,但我稍微查了一下,好像可以通过使用”depend on”来调整部署顺序。以后我会试试看这个。

广告
将在 10 秒后关闭
bannerAds