如何使用带有 Terraform 的 AWS 启动模板启动 AWS EC2 实例?

How do I launch an AWS EC2 instance using an AWS launch template with Terraform?

我正在尝试使用带有 Terraform 的 AWS 启动模板构建 AWS EC2 redhat 实例。

我可以通过调用 Terraform 的资源 aws_launch_template 创建启动模板。我的问题是如何使用 Terraform 使用创建的启动模板构建 EC2 服务器?

我调用什么 Terraform aws 提供程序资源?

非常感谢您的帮助!

欢迎来到 Stack Overflow!

您可以创建一个 aws_autoscaling_group 资源来使用您的新启动模板。 Please see the example here for more details.

代码:

resource "aws_launch_template" "foobar" {
  name_prefix   = "foobar"
  image_id      = "ami-1a2b3c"
  instance_type = "t2.micro"
}

resource "aws_autoscaling_group" "bar" {
  availability_zones = ["us-east-1a"]
  desired_capacity   = 1
  max_size           = 1
  min_size           = 1

  launch_template = {
    id      = "${aws_launch_template.foobar.id}"
    version = "$$Latest"
  }
}

这是我用来构建带有启动模板的 EC2 映像的代码。

variable "aws_access_key" {}
variable "aws_secret_key" {}

provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region     = "us-east-1"
}

resource "aws_launch_template" "foobar" {
    name_prefix   = "foobar"
    image_id      = "ami-0080e4c5bc078760e"
    instance_type = "t2.micro"
}

resource "aws_autoscaling_group" "bar" {
    availability_zones = ["us-east-1a"]
    desired_capacity   = 1
    max_size           = 1
    min_size           = 1

    launch_template = {
      id      = "${aws_launch_template.foobar.id}"
      version = "$$Latest"
    }
}

非常感谢阿迪尔!