【问题标题】:How do I launch an AWS EC2 instance using an AWS launch template with Terraform?如何使用带有 Terraform 的 AWS 启动模板启动 AWS EC2 实例?
【发布时间】:2023-03-31 11:30:01
【问题描述】:

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

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

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

非常感谢您的帮助!

【问题讨论】:

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


【解决方案1】:

欢迎来到 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"
  }
}

【讨论】:

  • 非常感谢您如何使用启动模板。会试一试,并报告结果!
  • 谢谢!能够使用 aws_autoscaling_group 启动映像。只好把image_id改成ami-0080e4c5bc078760e,运行完美!
  • $$Latest 版本有什么作用?
  • 您可以使用 $Latest$Default 或版本号来指定要在 ASG 中使用的启动模板。见这里:terraform.io/docs/providers/aws/r/… - 额外的 $ 被预先添加以逃避 Terraform 的插值。
  • 通过 Terraform 的启动模板启动非动态分配的 EC2 实例仍然会很好。
【解决方案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"
    }
}

非常感谢阿迪尔!

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 2021-11-15
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多