简体   繁体   English

使用Terraform重复基础架构创建

[英]Repeat infrastructure creation with Terraform

I'm exploring Terraform as a tool to manage reusable groups of AWS EC2 instances. 我正在探索Terraform作为管理可重用的AWS EC2实例组的工具。 I'm not very familiar with infrastructure tooling and looking for advice on what to do in this usecase. 我对基础结构工具并不十分熟悉,并且在此用例中寻求有关操作的建议。

  1. I want to repeatedly create multiple EC2 instances - say the first time I call terraform apply my infrastructure needs 3 instances. 我想重复创建多个EC2实例-说我第一次调用terraform apply我的基础架构需要3个实例。 After a while I want to create 100 instances - maybe without destroying the 3 instances I created earlier. 一段时间后,我想创建100个实例-也许不破坏我之前创建的3个实例。 How do I do this in Terraform? 如何在Terraform中执行此操作? Should I even be doing it? 我应该做吗? If I should not use Terraform to repeat provisioning, what is a good tool that can do this? 如果我不应该使用Terraform重复配置,那么有什么好的工具可以做到这一点?

  2. What are some tools that allow for remote execution of bash or Python scripts on the created Terraform infrastructure? 有哪些工具可以在创建的Terraform基础架构上远程执行bash或Python脚本? I know Terraform has remote-exec but the commands I need to run on these instances take a long time to run (3-5 hours) and I'd rather not have resources in that state where they're still initializing because I can't monitor them. 我知道Terraform具有remote-exec但是我需要在这些实例上运行的命令需要花费很长时间(3-5小时)才能运行,并且我宁愿没有处于这种状态的资源仍在进行初始化,因为我可以监视他们。

Custom AMI with pre-installed software will help you reduce boot-time. 带有预装软件的自定义AMI将帮助您减少启动时间。 Hashicorp packer https://www.packer.io/intro/ is good tool to create AMI. Hashicorp Packer https://www.packer.io/intro/是创建AMI的好工具。

  1. Create one ec2-instance using terraform. 使用terraform创建一个ec2-instance。
  2. Either user-data script or run remote-exec to run script which installs required packages/softwares. 用户数据脚本或运行remote-exec来运行安装所需软件包/软件的脚本。
  3. Create an AMI from the above ec2-instance. 从上面的ec2-instance创建一个AMI。
  4. Launch as many ec2-instances as you need to using newly created AMI. 使用新创建的AMI需要启动任意数量的ec2实例。

By Ansible also offers very good features to manage infrastructure as code. By Ansible还提供了非常好的功能,可以将基础结构作为代码进行管理。

This is very much use case of terraform and It would be good if you will do it via terraform. 这是很多terraform的用例,如果您要通过terraform来做,那将是很好的。

You can follow below code which you can use to spin up instance as many as you want, you have to apply again after you change count value. 您可以按照下面的代码使用,您可以使用该代码来旋转任意数量的实例,更改数值后必须再次应用。 it won't affect any currently running instances and match your value. 它不会影响任何当前正在运行的实例并与您的值匹配。

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}"
  }
}

Also, If you wanted to execute some command at the time of instance boot up. 另外,如果您想在实例启动时执行某些命令。 You may use the user-data script to do that. 您可以使用用户数据脚本来执行此操作。

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

For repeatability, You can use terraform module. 为了实现可重复性,可以使用terraform模块。 For example: If you wanted to use the code for multiple infra say dev, staging, production. 例如:如果您想将代码用于多个基础设施,请说dev,staging,production。 In the case of modules, you don't have to write the same code, again and again, to spin up ec2 instance. 对于模块,您不必一次又一次编写相同的代码来启动ec2实例。 You can pass the different variable for different infrastructure. 您可以为不同的基础结构传递不同的变量。

Example: 例:

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

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

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

If you don't have to delete old instances and decrease the size of your number of running instances. 如果您不必删除旧实例并减小正在运行的实例的数量。 That is not what terraform will take care off. 但这并不是地形会引起注意的。 You have to mention that strategy while creating your auto-scaling policy. 在创建自动扩展策略时必须提及该策略。

There are many termination policies listed below. 下面列出了许多终止策略。

Amazon EC2 Auto Scaling supports the following custom termination policies: 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.

You may refer below link for more information. 您可以参考下面的链接以获取更多信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM