【问题标题】:Repeat infrastructure creation with Terraform使用 Terraform 重复创建基础架构
【发布时间】:2019-03-27 02:20:47
【问题描述】:

我正在探索将 Terraform 作为管理可重用 AWS EC2 实例组的工具。我对基础设施工具不是很熟悉,并且正在寻找关于在这个用例中做什么的建议。

  1. 我想重复创建多个 EC2 实例 - 比如我第一次调用 terraform apply 我的基础设施需要 3 个实例。过了一会儿,我想创建 100 个实例——也许不会破坏我之前创建的 3 个实例。如何在 Terraform 中执行此操作?我应该这样做吗? 如果我不应该使用 Terraform 重复配置,有什么好的工具可以做到这一点?

  2. 有哪些工具可以在创建的 Terraform 基础架构上远程执行 bash 或 Python 脚本?我知道 Terraform 有 remote-exec 但我需要在这些实例上运行的命令需要很长时间才能运行(3-5 小时),我宁愿没有资源处于仍在初始化的状态,因为我可以'监控他们。

【问题讨论】:

  • 长时间运行的任务,它们是引导实例还是这些长时间运行的作业?
  • 他们将引导实例 - 需要下载并安装一些软件来处理数据。

标签: terraform terraform-provider-aws


【解决方案1】:

带有预安装软件的自定义 AMI 将帮助您缩短启动时间。 Hashicorp packer https://www.packer.io/intro/ 是创建 AMI 的好工具。

  1. 使用 terraform 创建一个 ec2 实例。
  2. 用户数据脚本或运行 remote-exec 以运行安装所需软件包/软件的脚本。
  3. 从上述 ec2 实例创建一个 AMI。
  4. 使用新创建的 AMI 启动所需数量的 ec2 实例。

By Ansible 还提供了非常好的功能来管理基础设施即代码。

【讨论】:

  • 这就是我现在正在做的事情(虽然没有 Packer,但我已经制作了 AMI)。但是 - 每个实例的设置步骤都不同 - 它需要一组特定的变量,然后根据这些变量下载一些数据。这是我无法放入 AMI 的一个自定义步骤。除此之外,我通过 remote-exec 使用目录创建和配置来引导实例。
【解决方案2】:

这是 terraform 的非常多的用例,如果您能通过 terraform 来做会很好。

您可以按照下面的代码来启动任意数量的实例,您必须在更改count 值后再次申请。它不会影响任何当前正在运行的实例并匹配您的值。

resource "aws_instance" "web" {
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"
  count = 3
  availability_zone = "${element(var.az, count.index)}"

  tags {
    Name = "${count.index}"
  }
}

另外,如果您想在实例启动时执行某些命令。您可以使用用户数据脚本来执行此操作。

resource "aws_instance" "..." {
  user_data = "${file("../../tmp/aws/userdata.sh")}"
  ...
}

为了可重复性,您可以使用 terraform 模块。例如:如果您想将代码用于多个基础设施,例如开发、登台、生产。对于模块,您不必一次又一次地编写相同的代码来启动 ec2 实例。您可以为不同的基础设施传递不同的变量。

例子:

module "dev" {
  source = "./modules/dev"
  count  = 2
  region = "us-east-1"
}

module "production" {
  source = "./modules/production"
  count  = 5
  region = "us-east-1"
}

参考:https://www.terraform.io/docs/modules/usage.html

如果您不必删除旧实例并减少正在运行的实例数量。这不是 terraform 会处理的问题。 您必须在创建自动扩展策略时提及该策略。

下面列出了许多终止政策。

Amazon EC2 Auto Scaling 支持以下自定义终止策略:

OldestInstance. Terminate the oldest instance in the group. This option is useful when you're upgrading the instances in the Auto Scaling group to a new EC2 instance type. You can gradually replace instances of the old type with instances of the new type.

NewestInstance. Terminate the newest instance in the group. This policy is useful when you're testing a new launch configuration but don't want to keep it in production.

OldestLaunchConfiguration. Terminate instances that have the oldest launch configuration. This policy is useful when you're updating a group and phasing out the instances from a previous configuration.

ClosestToNextInstanceHour. Terminate instances that are closest to the next billing hour. This policy helps you maximize the use of your instances and manage your Amazon EC2 usage costs.

Default. Terminate instances according to the default termination policy. This policy is useful when you have more than one scaling policy for the group.

您可以参考以下链接了解更多信息。

参考: https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html

【讨论】:

  • 这并不能回答我关于在不破坏先前实例的情况下重复部署具有不同计数的基础架构的问题(除非明确要求)。
  • 如果你想为多个基础设施实现相同的场景,你必须使用 terraform 模块。
猜你喜欢
  • 2018-03-15
  • 2016-01-14
  • 2015-08-04
  • 1970-01-01
  • 2021-11-04
  • 2023-03-05
  • 1970-01-01
  • 2021-07-08
  • 2020-07-19
相关资源
最近更新 更多