amazon-web-services - 如何在 Terraform 上使用 cloud-init 初始化实例

标签 amazon-web-services amazon-ec2 terraform cloud-init

我正在尝试使用 cloud-init 初始化 AWS 实例,我用 地形代码:

variable "hostname"    {}
variable "domain_name" {}


variable "filename" {
  default = "cloud-config.cfg"
}

data "template_file" "test" {
  template = <<EOF
#cloud-config
hostname: $${hostname}
fqdn: $${fqdn}
mounts:
  - [ ephemeral, null ]
output:
  all: '| tee -a /var/log/cloud-init-output.log'
EOF

  vars {
    hostname = "${var.hostname}"
    fqdn     = "${format("%s.%s", var.hostname, var.domain_name)}"
  }
}

data "template_cloudinit_config" "test" {
  gzip          = false
  base64_encode = false

  part {
    filename     = "${var.filename}"
    content_type = "text/cloud-config"
    content      = "${data.template_file.test.rendered}"
  }
}


resource "aws_instance" "bootstrap2" {
    ami = "${var.aws_centos_ami}"
    availability_zone = "eu-west-1b"
    instance_type = "t2.micro"
    key_name = "${var.aws_key_name}"
    security_groups = ["${aws_security_group.bastion.id}"]
    associate_public_ip_address = true
    private_ip = "10.0.0.12"
    source_dest_check = false
    subnet_id = "${aws_subnet.eu-west-1b-public.id}"
    triggers {
      template = "${data.template_file.test.rendered}"
    }

    tags {
            Name = "bootstrap2"
        }
}

但是“ Bootstrap ”资源中的触发器失败了。那么我如何使用我定义的云配置来配置这个实例呢?

最佳答案

triggers 不是 aws_instance 资源的有效参数。将配置传递给 cloud-init 的常用方法是通过 user_data 参数,如下所示:

resource "aws_instance" "bootstrap2" {
  ami = "${var.aws_centos_ami}"
  availability_zone = "eu-west-1b"
  instance_type = "t2.micro"
  key_name = "${var.aws_key_name}"
  security_groups = ["${aws_security_group.bastion.id}"]
  associate_public_ip_address = true
  private_ip = "10.0.0.12"
  source_dest_check = false
  subnet_id = "${aws_subnet.eu-west-1b-public.id}"

  # Pass templated configuration to cloud-init
  user_data = "${data.template_file.test.rendered}"

  tags {
    Name = "bootstrap2"
  }
}

关于amazon-web-services - 如何在 Terraform 上使用 cloud-init 初始化实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43252996/

相关文章:

amazon-ec2 - 自动缩放 EC2 或其他云计算基础设施的实际限制是什么?

azure - 地形 Azure : using resource name automatically on the display name groupe creation

ubuntu - 如何让用户数据在自定义 Ubuntu 18.04 AMI 上执行?

terraform - 如何在不破坏现有客户端的情况下扩展 terraform 模块输入变量模式?

amazon-web-services - 必须允许哪些策略操作才能将文件上传到 s3?

python - cc1plus : out of memory when building dlib

amazon-web-services - 我不完全理解容器化如何从一开始就不会导致过度配置实例

go - 如何在多个记录处理器之间平衡运动碎片?

amazon-s3 - 如何将 AMI 保存到 S3 存储桶?

apache - 使用EBS在Amazon Web Services上设置Hadoop集群