【问题标题】:How can I initialice an instance with cloud-init on Terraform如何在 Terraform 上使用 cloud-init 初始化实例
【发布时间】:2017-09-01 08:31:27
【问题描述】:

我正在尝试使用 cloud-init 初始化 AWS 实例,我使用下一个 terraform 代码进行测试:

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

但它使“引导”资源内的触发器失败。那么如何使用我定义的云配置来配置此实例?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 1970-01-01
      • 2022-07-21
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多