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

时间:2018-12-12 19:10:27

标签: amazon-web-services templates amazon-ec2 terraform terraform-provider-aws

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

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

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

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

欢迎堆栈溢出!

您可以创建一个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"
  }
}

答案 1 :(得分:2)

这是我用来使用启动模板构建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"
    }
}

非常感谢Adil!